Showing posts with label aws lambda. Show all posts
Showing posts with label aws lambda. Show all posts

Friday, February 26, 2016

alexa, tell mom i'm home

I got my Alexa for Christmas from my kids this year and had set it up to use IFTTT. You can read more about that set up here. Mostly texting recipes for notifying them when dinner is done, etc. I really hated using "unnatural" language though.

"Alexa - trigger dinner is ready."

I set up a IFTTT recipe for my daughter to notify me when she gets home from school.

"Alexa - trigger mom I'm home."

See? Unnatural.

So I've been learning how to use AWS Lambda function using an Alexa Skill event. Here's the GitHub link to the completed source code. 

Here's what I struggled the most with ...

Testing - It's not the greatest trying to debug issues.


Sometimes you'll get an invalid response and have no clue what's wrong. You can also use CloudWatch along with some strategically placed console.logs to eventually figure out what happened.

I really struggled with SNS. The function would complete successfully but the text message was never sent. What I found was because the calls are asynchronous - the context object was being marked as done before the method completed. Moving the callback inside the publish method worked great. Like this ...


sns.publish(params, function (err, data) {
            if (err) console.log(err, err.stack); // an error occurred
            else
            {  
                console.log("Success");
               
                 if ("good" === day)
                    {
                        speechOutput = "I'm glad to know you had a good day! " +
                        "I will inform your mom so she can raise the roof with her hands like your dad does. ";
                    }
                    else if ("bad" === day)
                    {
                        speechOutput = "I'm sorry you had a bad day. " +
                        "I will inform your mom so she can sympathize with you when she gets home. ";
                    }
       
                var sessionAttributes = {};
                var speechOutput = speechOutput + "Ok, the message was sent.";
               
                var repromptText = "";
                var shouldEndSession = true;
               
                callback(sessionAttributes,
            buildSpeechletResponse(speechOutput, repromptText, shouldEndSession));
               
            }
        });

So now .... it's.

"Alexa, tell Mom I'm home."

This project really helped me too.  Mihai Galos was really helpful in getting me to this point - so thanks Mihai! It demonstrates a http request for Twilio, which will be really helpful for future integration with a rest api. But .... I didn't want to use Twilio - I wanted to implement SNS.

Again, check out my Github for the entire project. Already looking for my next project. Maybe adding a database entry to record the time of day she gets off the bus. :) Maybe we won't tell Holly about that part. hehe



Thursday, February 4, 2016

first look at the sdk for amazon alexa

Yesterday was all about the existing capabilities of the Amazon Alexa and I ended with an enthusiastic task of reviewing the sdk (software development kit).



It wasn't half bad - and by the end of the day I had delivered the code to my Alexa and had her asking us all what our favorite color was.

Here's how I accomplished that ...

Here's the getting started guide. I started poking around the information.

First, I signed up to watch the on-demand webinar on the ASK (Alexa Skills Kit). It was about an hour long and well worth it. Each capability is called a skill - and you are really just giving Alexa a new skill.

The information is transmitted via REST so either you have you own secured web service (which I do not) then it's best to use Amazon web service. I was able to sign up and get a whole year of free usage. AWS Lambda made it easy to get up and running even providing some templates (or blueprints) giving you sample code with work with.


It's really just javascript functions that parses json data. You have help methods such as onSessionStarted, onSessionEnded, onIntent and onLaunch.

Next you need an account on the developer portal for Amazon. You can set this up here. This is where you'll define the skill and point it to the REST api (where it sits in the cloud). This is also where the utterances and intents (schemas and possible key phrases) are stored.



So in the sample of favorite color - the color schema is defined with an Intent:
 "intent": "MyColorIsIntent"
"slots": [
        {
          "name": "Color",
          "type": "LIST_OF_COLORS"
        }
      ]

The slot would be defined LIST_OF_COLORS: green, red, blue, orange, gold, silver, yellow, black, white. So, the array of colors is a slot.



And Utterances would be anything the user could say ...

WhatsMyColorIntent what's my favorite color
WhatsMyColorIntent what is my favorite color
WhatsMyColorIntent what's my color
WhatsMyColorIntent what is my color
WhatsMyColorIntent my color
WhatsMyColorIntent my favorite color
WhatsMyColorIntent get my color
WhatsMyColorIntent get my favorite color
WhatsMyColorIntent give me my favorite color
WhatsMyColorIntent give me my color
WhatsMyColorIntent what my color is
WhatsMyColorIntent what my favorite color is

Things I struggled with ...

I was only interested in getting my sample code to test properly. Right now I'm only interested in publishing to my Alexa - and not to the entire Alexa world. That was one thing not immediately apparent to me - how to "test" on my own device. 

Turns out as long as you have the same login on the developer console in Amazon as what you configured your Alexa works - it's magical when you "test". It's one of the steps identified in the Alexa app console (skill information, interaction model, and test).

I also struggled in AWS Lambda was picking the right test event. Initially I just picked the default - of course would fail. So pick the correct Sample Event Template.

Also, make sure you pick US East (N. Virigina) has your region to even see the Lambda functions.

This article was really helpful too. http://tobuildsomething.com/2015/08/14/Amazon-Echo-Alexa-Tutorial-The-Definitive-Guide-to-Coding-an-Alexa-Skill/

My next move ...

Here's what I want to do now. Take my "Mom I'm home" idea and expand it. Currently it's using IFTTT to send me a text when Holly triggers her.

I would like Holly to be able to walk in the door and say "Alexa? Tell Mom I'm home. Alexa would respond with "Will do Holly. How was your day?" Holly would say "Good" or "Bad". Alexa would text me with "Holly is home and had a good (or bad) day.

Never something I would put into the App Services - only used by us. So now I'm looking at integrating with Amazons SNS service which will allow the text messaging.