Search This Blog

Wednesday, November 30, 2016

Coding for Amazon Alexa + Amazon Lambda

Bought an Amazon Echo Dot in the Black Friday sales...  having a quick play at developing a test application for it using Amazon Lambda.

It's quite easy once you've wired things up.

This link was mega helpful and I used this as a code template to thanks to the original author: http://moduscreate.com/build-an-alexa-skill-with-python-and-aws-lambda/ 


First I wrote some code in Python on Lamda - excerpts below:

import json
import random

def lambda_handler(event, context):
    
    if event["request"]["type"] == "IntentRequest":
        return on_intent(event["request"], event["session"])


def on_intent(intent_request, session):
    intent = intent_request["intent"]
    intent_name = intent_request["intent"]["name"]

    if intent_name == "GetNumber":
        return get_number()
    elif intent_name == "GetLetter":
        return get_letter()
    elif intent_name == "GetNumberBetween":
        return get_numberbetween(intent)

def get_number():
    session_attributes = {}
    card_title = "RandomNumber"
    r=random.randrange(10) 
    speech_output = 'the number I chose is '+ str(r)
    reprompt_text = "OK"
    should_end_session = True
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

def get_letter():
    session_attributes = {}
    card_title = "RandomNumber"
    r=random.randrange(26) 
    speech_output = 'abcdefghijklmnopqrstuvwyz'[r]
    reprompt_text = "OK"
    should_end_session = True
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

def get_numberbetween(intent):
    if "NumberA" in intent["slots"]:
        num_a = intent["slots"]["NumberA"]["value"]
    if "NumberB" in intent["slots"]:
        num_b = intent["slots"]["NumberB"]["value"]
    
    print (num_a)

    session_attributes = {}
    card_title = "RandomNumber"
    r=random.randrange(int(num_a),int(num_b),1) 
    speech_output = str(r)
    reprompt_text = "OK"
    should_end_session = True
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))



def build_speechlet_response(title, output, reprompt_text, should_end_session):
    return {
        "outputSpeech": {
            "type": "PlainText",
            "text": output
        },
        "card": {
            "type": "Simple",
            "title": title,
            "content": output
        },
        "reprompt": {
            "outputSpeech": {
                "type": "PlainText",
                "text": reprompt_text
            }
        },
        "shouldEndSession": should_end_session
    }

def build_response(session_attributes, speechlet_response):
    return {
        "version": "1.0",
        "sessionAttributes": session_attributes,
        "response": speechlet_response
    }



===========

You will need to copy the Application number which should be in the form  arn:aws:lambda.....
You can this from the AWS Lambda Dev page - click on functions and in the code view it should be top right of the page.


Then head here to set up the link from AWS Lambda to the Alexa skills kit.




NOTE: If in the UK you must set up the ENGLISH (UK) language tab - not the default English (US) tab - otherwise even though things will work in the online dev test environment your Echo won't respond to voice requests.


The Main setup is the intent schema and sample utterances. The intents must link to those in your code (eg python) and to the sample utterances.  You can create slots of various data types (eg dates, lists) and pass those values to your application too.

========



Intent Schema

{
  "intents": [
    {
      "intent": "GetNumber"
    },
    {
      "intent": "GetLetter"
    },
    {
      "intent": "GetNumberBetween",
      "slots":[
        {
          "name": "NumberA",
          "type": "AMAZON.NUMBER"
        },
        {
          "name": "NumberB",
          "type": "AMAZON.NUMBER"
        }
      ]
    }
  ]
}



==========

Sample Utterances


GetNumber for a number
GetNumber random number 
GetNumber for a random number 
GetNumber for random number 
GetNumber give me an integer
GetNumber number please
GetNumber choose a number
GetNumber pick a number
GetNumber another number
GetNumber choose a number
GetNumber give me a number
GetLetter for a letter
GetLetter for a random letter
GetLetter for random letter
GetLetter choose a letter
GetLetter letter please
GetNumberBetween give me a number between {NumberA} and {NumberB}
GetNumberBetween for a number between {NumberA} and {NumberB}
GetNumberBetween for a random number between {NumberA} and {NumberB}
GetNumberBetween select a random number between {NumberA} and {NumberB}
GetNumberBetween give me a number from {NumberA} and {NumberB}
GetNumberBetween select a random number from {NumberA} and {NumberB}
GetNumberBetween choose a number between {NumberA} and {NumberB}
GetNumberBetween pick a number between {NumberA} and {NumberB}




No comments: