I want to create a skill for Amazon Alexa that - when triggered by voice commands - gets some information from an API via a HTTPS request and uses the result as spoken answer to the Alexa user.
There is a little challenge here (especially for node.js newbies) due to the event-driven concept of node.js and the internals of the Alexa Skills Kit for Node.js. And getting a hand on parameters from the user isn't that easy, either.
Can somebody provide some sample code to start with?
It abstracts the complexities of making requests behind a simple interface, and makes HTTP requests easy and simple. If you're using Alexa Hosted as the backend for your skills, there's an added advantage of the Requests library being built-in, so you don't need to install or upload it manually.
While I created my first Alexa Skill I was new node.js, Lambda and the Alexa Skills SDK. So I ran into a few problems. I'd like to document the solutions here for the next person who runs into the same problem.
I would have easily saved two hours of debugging had I had the following code snippet. :-)
I my sample the lambda script already gets the preformatted text from the API. If you call an XML/JSON or whatever API you need to work with the answer a bit more.
'use strict';
const Alexa = require('alexa-sdk');
var https = require('https');
const APP_ID = ''; // TODO replace with your app ID (OPTIONAL).
const handlers = {
'functionwithoutdata': function() {
var responseString = '';
var mythis = this;
https.get('**YOURURL**?**yourparameters**', (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
responseString += d;
});
res.on('end', function(res) {
const speechOutput = responseString;
console.log('==> Answering: ', speechOutput);
mythis.emit(':tell', 'The answer is'+speechOutput);
});
}).on('error', (e) => {
console.error(e);
});
},
'functionwithdata': function() {
var mydata = this.event.request.intent.slots.mydata.value;
console.log('mydata:', mydata);
var responseString = '';
var mythis = this;
https.get('**YOURURL**?**yourparameters**&mydata=' + mydata, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
responseString += d;
});
res.on('end', function(res) {
const speechOutput = responseString;
console.log('==> Answering: ', speechOutput);
mythis.emit(':tell', 'The answer is'+speechOutput);
});
}).on('error', (e) => {
console.error(e);
});
}
};
exports.handler = (event, context) => {
const alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
If you’re looking for a way to create an Alexa voice enabled bot then you’re on the right place!
Let’s create an Amazon Alexa bot from scratch using node server running on our localhost and tunneled through ngrok.
Sign up for an Amazon developer account, if you don’t have one
Go to Alexa developer page
Go to the Alexa console
Click on Create skill
Give a name to the skill, I have named mine TestSkill and click on Next
Choose a model to add to your skill, I’ve selected custom for my experiments
Click on Create skill
This way you reach to the Alexa skill dashboard
Provide an invocation name, I’ve named it “give me pizza” and click on Save Model
Click on the Endpoint
Now, we need to provide the endpoint to Alexa console but first we need to setup an endpoint.
Create a server which can accept POST requests on default location i.e. “/”.
There are so many techniques for creating a server, I personally prefer node.
I assume that you’ve node, npm and Visual studio code already installed
For the specific requirements of this tutorial, we will create a Hello World node app following the steps below:
npm init
on a terminal and when asked for the package name Alexacd Alexa
package.json
fileRun npm i express http -save
and this will add the following entry in the package.json
file:
“dependencies”: { “express”: “4.16.3”, “http”: “0.0.0” }
Set value to index.js
of the main key in the package.json
file
index.js
on same levelÀdd the following code to the index.js
file:
const express = require('express'); const app = express(); app.post('/', (req, res) => res.send({ version: '1.0', response: { shouldEndSession: false, outputSpeech: { type: 'SSML', text: 'Hello World!', ssml: 'Hello World!' } } })); app.listen(8080, () => console.log('Example app listening on port 8080!'));
Set value of scripts
to { “start”: “node index.js” }
npm start
on the terminalAdd a tunnel to the localhost on PORT 8080
running the node server using ngrok following the below steps:
ngrok http 8080
on a terminalMy development endpoint is a sub-domain of a domain that has a wildcard certificate from a certificate authority
Click Save Endpoints
Click on JSON editor and provide the following model:
{
"interactionModel": {
"languageModel": {
"invocationName": "get me pizza",
"intents": [
{
"name": "PizzaOrderIntent",
"slots": [],
"samples": [
"Give me a pizza"
]
},
{
"name": "AMAZON.FallbackIntent",
"samples": [
"I didn't understand"
]
},
{
"name": "AMAZON.CancelIntent",
"samples": [
"cancel plz"
]
},
{
"name": "AMAZON.HelpIntent",
"samples": [
"help"
]
},
{
"name": "AMAZON.StopIntent",
"samples": [
"stop"
]
}
],
"types": []
}
}
}
Once, the skill model is build - we need to test it. Click on the Test tab and toggle ON the “Test is enabled for this skill”.
That’s it, you’ve created an Alexa bot connected to your locally running node project.
Questions? Comments? Do reach me at [email protected]
Read the full article
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With