Build Customized Alexa Skill

I : Alexa , When is the next bus ?

Alexa : The next bus 130 is at 22 hours 38 minutes , which is Due 5 minutes from now.

Recently I cam across a situation where I needed to build this skill. I am staying in Dublin and the bus stop near my home has bus 130 every 10-15 minutes but not always at time . Just before starting , I always open to check Dublin bus app . But opening the app and searching the bus number again takes time , especially if I am running late (which is 90 % of the time). I thought Alexa can save some of my time .

Note : I have not made this skill globally available hence I am not going to document any steps that needs certification and publishing . I am still adding more skills in it . However this below document is only to create a skill in development environment and work with personal Alexa . I have used Dublin Bus API for the skills .

Go to :

Login with the same credentials that you have used for Alexa app .

After logging in , click “Create Skill”

While creating skill , make a note of the language. The language should be exactly the same where your Alexa is running . Else your Alexa will not be able to find the skill.

Create a skill

Next you have to provide an Invocation name . This is the name of the custom skill . You have to utter this name every time you tell Alexa to perform this skill . I gave Dublin Transport , as I intend to add more functionality in future .

Next is creating required Intents . Intents in Alexa or Lex is the user’s spoken request . Intents are made with a group of sample utterances. Alexa will learn from these sample utterances or rather more the utterances , greater is the chance for Alexa to understand your spoken request . Go to Intent –> Add and give the Intent name .

Below are the sample utterances I have used :

Slots are the variable arguments to an Intent . In my utterances , stop and bus number(routeid) are the 2 slots required . If they are not present in user utterance , I have hard coded my bus number and stop id as per my need .

Here is the slot types:

Once done , save the model and build the model .

You will get a notification about the success or failure of the build .

You can expose this skills as a lambda function ARN or as a service endpoint. The Endpoint will receive POST requests when a user interacts with your Alexa Skill.

Next go to the code section . I have written lambda function in node js . You will get a Hello world template in the developer portal . You can edit it there or download AWS SDK for Node . It is also available in Java and Python .

You will also be able to see the logs in cloud watch :

The complete code is present in

Here is the snippet of the important section :

function getDublinBusInfo(url)  {
 return new Promise((resolve, reject) => {
   https.get(url, (resp) => {
          let data = '';
          // A chunk of data has been recieved.
          resp.on('data', (chunk) => {
            data += chunk;
          // The whole response has been received. Print out the result.
          resp.on('end', () => {
              try {
                const parsedData=JSON.parse(data);

        }).on("error", (err) => {
const BusTimeIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'Bustime';
    async handle(handlerInput) {
        let stop_id= handlerInput.requestEnvelope.request.intent.slots.stop.value;
        let route_id= handlerInput.requestEnvelope.request.intent.slots.routeid.value;
        if(stop_id === undefined){
            stop_id ='1755';
        if(route_id === undefined){
            route_id= '130';
        let url= '' +  stop_id + '&routeid=' + route_id + '&format=json'  ;
        var response;
            response = await getDublinBusInfo(url);
            return handlerInput.responseBuilder
                .speak("Sorry , Not able to connect to Dublin bus now")
                //.reprompt('add a reprompt if you want to keep the session open for the user to respond')

        if(response.errorcode === '1') {
           return handlerInput.responseBuilder
            .speak("Sorry , there is no bus available now")
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
        let  speakOutput='The next bus' +route_id+ ' is at '
	    var time;
        if(response.results[0].arrivaldatetime !==null) { 
            time=response.results[0].arrivaldatetime.split(" ")[1].split(":");
        let parsedArrivalhours=time[0] ;
        let parsedArrivalminutes=time[1] ;
        speakOutput =speakOutput+ parsedArrivalhours + ' hours ' +parsedArrivalminutes + ' minutes , which is ' +  response.results[0].duetime + 'minutes from now.'  ;
        return handlerInput.responseBuilder
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')

In the snippet above getDublinBusInfo() returns a Promise for http get calls. BusTimeIntentHandler has an async handle function as it waits for the response from the getDublinBusInfo () . After receiving the response , i split the times and pass it on to speakoutput variable which Alexa speaks out .

Once the code is complete , save and deploy .

Now go to the Test section :

If you ask Alexa device now , it should be able to respond .

Now Go ahead and try a custom skill with Alexa 🙂

Digiprove sealCopyright secured by Digiprove © 2019 Geeks 18

Be the first to comment

Leave a Reply

Your email address will not be published.