question

RD avatar image
RD asked

Example for calling REST API from Alexa?

This seems like a typical use case, but I haven't been able to find instructions or examples for doing this.

I have an existing REST API and I would like to wrap it with an Alexa Skill.

A few posts suggest I need to set up something like this, but I haven't found any examples or instructions:

new Alexa Skill ---calls---> new Lambda function ----calls----> REST API

Can anyone point me to a quickstart or instructions?

alexa skills kitlambdaapi
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sluggercz avatar image
sluggercz answered

Hey RD, try looking at the following examples:

AlexaSkillsKit_HttpGetExample

1 comment
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thanks I will take a look and see if this is usable.

0 Likes 0 ·
DB avatar image
DB answered

Thanks sluggercz! It was very useful for me, I managed to adapt it for what I needed it.

Hey RD, I pasted the changed code that did the trick for me, maybe you can cross reference it with the original for additional insights. My intentName is "AskTempIntent", I added a function that reads the temperature from my personal website and sends it to Echo to read it.

"..

/** * HTTP GET example * * Additional Notes * https://developer.amazon.com/appsandservices/solutions/alexa/alexa-skills-kit/getting-started-guide */ // Route the incoming request based on type (LaunchRequest, IntentRequest, // etc.) The JSON body of the request is provided in the event parameter. var temperature; exports.handler = function (event, context) { try { console.log("event.session.application.applicationId=" + event.session.application.applicationId); /** * Uncomment this if statement and populate with your skill's application ID to * prevent someone else from configuring a skill that sends requests to this function. */ /* if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.[unique-value-here]") { context.fail("Invalid Application ID"); } */ if (event.session.new) { onSessionStarted({ requestId: event.request.requestId }, event.session); } if (event.request.type === "LaunchRequest") { onLaunch(event.request, event.session, function callback(sessionAttributes, speechletResponse) { context.succeed(buildResponse(sessionAttributes, speechletResponse)); }); } else if (event.request.type === "IntentRequest") { onIntent(event.request, event.session, function callback(sessionAttributes, speechletResponse) { context.succeed(buildResponse(sessionAttributes, speechletResponse)); }); } else if (event.request.type === "SessionEndedRequest") { onSessionEnded(event.request, event.session); context.succeed(); } } catch (e) { context.fail("Exception: " + e); } }; /** * Called when the session starts. */ function onSessionStarted(sessionStartedRequest, session) { console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId + ", sessionId=" + session.sessionId); } /** * Called when the user launches the skill without specifying what they want. */ function onLaunch(launchRequest, session, callback) { console.log("onLaunch requestId=" + launchRequest.requestId + ", sessionId=" + session.sessionId); // Dispatch to your skill's launch. getWelcomeResponse(callback); } /** * Called when the user specifies an intent for this skill. */ function onIntent(intentRequest, session, callback) { console.log("onIntent requestId=" + intentRequest.requestId + ", sessionId=" + session.sessionId); var intent = intentRequest.intent, intentName = intentRequest.intent.name; // Dispatch to your skill's intent handlers if ("AskTempIntent" === intentName) { getTempResponse(callback); } else { throw "Invalid intent"; } } /** * Called when the user ends the session. * Is not called when the skill returns shouldEndSession=true. */ function onSessionEnded(sessionEndedRequest, session) { console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId + ", sessionId=" + session.sessionId); // Add cleanup logic here } // --------------- Functions that control the skill's behavior ----------------------- function getWelcomeResponse(callback) { // If we wanted to initialize the session to have some attributes we could add those here. var sessionAttributes = {}; var repromptText = null; var cardTitle = "Http GET Example"; //test http get testGet(function (response) { var speechOutput = "Response status is " + response; var shouldEndSession = true; callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); }); } function getTempResponse(callback) { var sessionAttributes = {}; var repromptText = null; // var cardTitle = "Http GET Example"; var cardTitle = "Get Temp"; //test http get gettemp (function (response) { var speechOutput = "Response status is " + response; var shouldEndSession = true; callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); }); } function testGet(response) { var http = require('http'); var options = { host: 'your.host', port: 80, path: '/yourpath', agent: false }; http.get(options, function (res) { console.log("Response: " + res.statusCode); // response(res.statusCode); response(res); }).on('error', function (e) { console.log("Error message: " + e.message); }); } function gettemp (response) { var http = require('http'); var options = { host: 'your.host', port: 80, path: '/yourpath', agent: false }; http.get(options, function (res) { var body = ''; res.on('data', function(d) { body += d; }); res.on('end', function() { temperature = Math.round (body,0); //console.log(temperature); // response (temperature); response (body); }); }); } // --------------- Helpers that build all of the responses ----------------------- function buildSpeechletResponse(title, output, repromptText, shouldEndSession) { return { outputSpeech: { type: "PlainText", text: output }, card: { type: "Simple", // title: "SessionSpeechlet - " + title, // content: "SessionSpeechlet - " + output title: title, content: output }, reprompt: { outputSpeech: { type: "PlainText", text: repromptText } }, shouldEndSession: shouldEndSession } } function buildResponse(sessionAttributes, speechletResponse) { return { version: "1.0", sessionAttributes: sessionAttributes, response: speechletResponse } }

.."

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.