Building of the "AskAboutTrump" Alexa Skill

I was involved in the Amazon Echo Beta (and I've just now received my Echo Dot), and when the Alexa Skills Kit came out I immediately wanted to see what fun I could have with it.

With a free AWS Account, and a Free Algolia Account, I quickly whipped up an "AskAboutTrump" skill. It's now approved and live in Amazon Alexa, just go to your Alexa app and search for "Ask About Trump".  (Note that I had to change this from "Ask Trump" to "Ask About Trump" to pass the Alexa Approval process, more on that later).

The process is relatively simple, once you have signed up for both AWS and Algolia, create a new Algolia index. In my case, I called it "trump", but if you wanted to create a different type of q/a system you could make it more appropriate.

Next, I filled in a few records in my index, I just went under "Add Manually" and filled out a few examples, like:

{
  "name": "How will you make america great again?",
  "response": "By building a wall, beating china, and forcing companies to bring business back to america",
  "priority": 22
}

Note the three important parts, the "name" which is what would be searched, the "response" which is how the skill question will respond, and the "priority" which I used in the custom search ranking. Under the "Ranking" tab I aded both "name" and "keywords" (keywords giving me an additional option to add more search keywords if needed). Under "Custom Ranking" I added "priority", mostly as a disambiguation tool if multiple records match pretty equally.

I filled out a few more records later on as I came up with other witty quotes, which I all did via the "Add Manually" option, but it would also be trivial to just create a little site to add in records more easily too.

Next, create Access keys in Algolia to pass to your ASK Skill. I created my access key with both Read and Write access to the index so I could have the Alexa skill also add in support for adding new responses. I never got around to actually implementing this part, but it could be done with enough time.

Next up, create the Lambda function. I have the full code I used available on GitHub: https://github.com/kopertop/AskAboutTrump

The code was relatively simple, the key part is in the "askQuestion" function:

function askQuestion(intent, session, callback) {
  var repromptText = 'Go on, ask';
  var sessionAttributes = {};
  var speechOutput = 'I will make america great again! Ask me how.';
  var closeSession = false;
  var question = intent.slots.Question;
  if(question && question.value){
    question = question.value;
  }
  console.log('SEARCHING FOR', question);
  index.search(question, { hitsPerPage: 1 }, function(err, resp){
    var title = question.value;
    if(!err && resp && resp.hits && resp.hits.length > 0){
      console.log('ALGOLIA RESPONSE', resp.hits);
      closeSession = true;
      speechOutput = resp.hits[0].response;
      title = resp.hits[0].name;
    }

    // Setting repromptText to null signifies that we do not
    // want to reprompt the user.
    // If the user does not respond or says something
    // that is not understood, the session
    // will end.
    callback(sessionAttributes,
      Skill.buildSpeechletResponse(title,
        speechOutput, repromptText, closeSession));
    });
}

Note that this allows for any sort of free-formed question to be asked, and the search is done in Algolia to find the closest match. That means that you could have something with a bunch of questions in the "title" but the same response, or include extra words but if the user leaves them out they still match. Why did I do this instead of just a simple mapping?

Lets take a more complex example: "about what are new york city values how do you like feel about new york city.". This matches:
  • Ask Trump, what are new york city values
  • Ask Trump about new york city
  • Ask Trump how do you feel about new york city
  • Ask trump about new york city values
  • Ask Trump do you like new york city
And many more....

In any event, after the Labmda function is created, you'll need to register it with Alexa Skills Kit. This can be a relatively complex process to get approved, so here's the key points that I missed the first few times:


Include a lot of phrases in your "Interaction Model".


For mine, I had:

GetQuote a quote
GetQuote quote
GetQuote give me a quote
GetQuote get a quote
GetQuote get quote
AMAZON.HelpIntent help
AMAZON.HelpIntent what can i say
AMAZON.HelpIntent what can you answer
AskQuestion {What Will Trump Do|Question}
AskQuestion {Where do you live|Question}
AskQuestion {Where do think about climate change|Question}
AskQuestion {How do you feel about free trade|Question}
AskQuestion {Do you like Hillary Clinton|Question}
AskQuestion {Who do you want counting your money|Question}
AskQuestion {Does mexico have good people|Question}
AskQuestion {How do you feel about Saudi Arabia|Question}
AskQuestion {How do you feel about politics|Question}
AskQuestion {Where do you live|Question}
AskQuestion {How do you feel about China|Question}
AskQuestion {How will you fix the border crisis|Question}
AskQuestion {What are new york values|Question}
AskQuestion {How are you doing in the polls|Question}
AskQuestion {Will you win the election|Question}


Your Interaction Model must include your example phrases.

When you get to your "Publishing Information", make sure the three "Example Phrases" are included in your Interaction Model.

You must include a "Help Intent", "Stop Intent", and "Cancel Intent".


If a user ever wants to stop, cancel, or get help, they need to be able to. Saying "Stop" pretty much at any point in your interaction must quit your app.

You must provide a proper reprompt request, and handle if the user doesn't say anything.


My initial confusion was failing to provide a re-prompt request, and also not handling the basic "Just Start the app". For example, if you just say "Open Trump" it needs to do something. Or, if you ask for "Help", it returns an open prompt after saying "Try asking me....". This is important, and if the user responds after that you need to make sure to close the stream. If you leave the listening stream open, or don't provide a proper reprompt request, you will be rejected.

This was the most confusing thing to me because I thought "But I don't control when Alexa closes the stream!", but yes you do, you control that by just passing the "closeSession" variable. Make sure that is set to "true" only after you have answered a users question, and make sure you pass a "Reprompt request" to anything that is "false".


Go ahead, download my skill.

Let me know what you think, and if you have any other questions you think would be nice to see answers to leave them in the comments!


0