Wednesday, 26 January 2022

Half Term Fev 2022 - Python e Alexa

https://unsplash.com/photos/LJ9KY8pIH3E



Alexa skills
Williams, Mark
Thu 13/01/2022 13:01
Brandao-Neto, Jose
 
1: Make an Amazon developer account at https://developer.amazon.com/alexa/console/ask
2: Click Create Skill.
3: Give your skill a name, eg Liam's Jokes. Select the language of your Alexa device.
4: Choose Custom model, and Alexa Hosted (Python), and click Create Skill.
5: Choose Start From Scratch, then Continue.
6: When its finished, click Invocation Name, and change it to Liam's Jokes if it is not already that, click Save Model, then Build Model.
7: Go to Code at the top.
8: Delete all the stuff that is already there, its way too complicated.

The whole thing is to take json in, and spit json out. The json comes into a function called like this:

def lambda_handler(event, context):

Dont worry about event and context yet. To get a joke, do something like:

    import requests
    r = requests.get("https://icanhazdadjoke.com/", headers={"Accept": "application/json"})
    joke = r.json()['joke']

The function needs to return something like:

    return {
        'version': '1.0',
        'sessionAttributes': {},
        'response': {
            'outputSpeech': {
                'type': 'PlainText',
                'text': joke
            },
        'shouldEndSession': True
        }
    }


Then you should be able to put joke as the

9: When you have finished the python, click Save then Deploy.
10: Because we have used the requests package, we need to make sure it is available. Go to the requirements.txt file, and replace the contents with

requests==2.27.1

11: Click Save, then Deploy
12: Click Test at the top. Change the dropdown so it says "Development"
13: Type "Alexa, launch Liam's Jokes" in the box

Debugging is very hard, took me an hour to figure out I needed to do step 10, and I've done it before. But hopefully that gets you started.

Mark