Making SUSI Alexa skill as an express app
Previously SUSI Alexa skill was deployed using AWS Lambda service (Refer to this blog). Each SUSI.AI Bot should be deployed on Google cloud using Kubernetes. To accomplish that, we need to remove the dependency of the SUSI Alexa skill from AWS Lambda service. We need to make it an express app, to be able to deploy it to Google cloud. Let’s start with on how to achieve it: SUSI Alexa skill: We require three files to make the skill as an express app. The main entry point for the skill would be server.js file, which will serve the incoming request using two helper files alexa.js and handlers.js. Server.js: This file acts as the main entry point for the incoming request. We handle two type of requests using it, that are: Launch request Intent request Launch request is triggered when a person utters “Alexa, open susi chat” , “Alexa, start susi chat”, “Alexa, launch susi chat” etc. This request is responded with an introductory phrase about SUSI.AI. To catch this request: if (type === "LaunchRequest") { var endpoint = "http://api.susi.ai/susi/chat.json?q="+"Welcome"; // ENDPOINT GOES HERE http.get(endpoint, (response1) => { var body = ""; response1.on("data", (chunk) => { body += chunk; }); response1.on("end", () => { var viewCount; viewCount = JSON.parse(body).answers[0].actions[0].expression; endpoint = "http://api.susi.ai/susi/chat.json?q="+"Get+started"; // ENDPOINT GOES HERE body = ""; http.get(endpoint, (response2) => { response2.on("data", (chunk) => { body += chunk; }); response2.on("end", () => { viewCount += JSON.parse(body);.answers[0].actions[0].expression; response.say(viewCount,false); }); }); }); }); } Intent request gets triggered, when any other phrase is uttered by the user except Launch related phrases. We check if the intent triggered has a corresponding handler to handle the request. If the handler is found in handlers.js file, we call it passing the required arguments to the handler function. Let’s see how handlers make this step possible. Handler.js: This file decides on what function to run when a particular type of intent is triggered. As we have just one intent for our SUSI Alexa skill i.e. callSusiApi, we have just one function in our handlers.js file. During its execution, the first step we do is extract the query value: let query = slots.query.value; Depending upon the query value, we run its corresponding code. For example, in case of a generic query (i.e. any query except stop, cancel and help): var endpoint = "http://api.susi.ai/susi/chat.json?q="+query; // ENDPOINT GOES HERE http.get(endpoint, (response1) => { var body = ""; response1.on("data", (chunk) => { body += chunk; }); response1.on("end", () => { var data = JSON.parse(body); if(data.answers[0].actions[1]){ // handle rss and table type results } else { viewCount = data.answers[0].actions[0].expression; } response.say(viewCount,true); }); }); At the end of the function we respond to the user with an answer to his/her query using: response.say(viewCount,true); Alexa.js: When we get a request from the user, we pass that request and response object to this file. This file helps us wrap the required request properties into an object and return that back to the server file, which was the entry point for the request. Now,…
