Diving Deeper Into Laziness With Nodlex

So If you have read my last blog and created Lazybot, Then you too are a victim of Lazybot’s laziness. So why not work a little bit more and make our bot more intelligently lazy?

In this blog, we will:

  1. Add few more intents to Lazybot,
  2. We will work on the feature where Lazybot can ask you few questions (prompts) and,
  3. Also, it can track context with the help of session.

And all of this we will be doing with the help of Nodlex and alexa-nodejs-boilerplates, so it’s gonna be real quick.

As I was thinking what could be the sample feature that I can choose for the Lazybot. Then I got this idea what about Lazybot is a Weather Bot that hardly tells you weather (because it’s too lazy of-course).

So let’s begin,

For our Lazybot to understand that, the user is asking about the weather, we need to add weather intent and few utterances (for the skill Lazybot on Amazon Developer Portal). I have added following intent and utterances for weather

Now we need to add an intent match and a response for the intent that we have created. In the normal case, we have to add a check for weather intent, a method that will get called and finally a response. I am pretty sure that this would have taken a significant amount of time. But don’t worry, you are rescued by Nodlex. So let’s do it in Nodlex way.

Come to the root of your project directory, open console window and type-

This will run Nodlex and will ask you a confirmation to add an intent named weather, press enter to add intent.

And that’s all, now Nodlex will automatically add an intent to intent-matcher file and create a function that will be called once the request for weather will come. You can see both things in the intent-matcher.js file.

This new function should return a Promise resolving into datastore object containing our response.

Let’s say, Our Lazybot did not reply the weather instead it asks another question, then we can build the response with the help of ResponseBuilder (containing a simple speech followed by prompt) in following way —

Little Explanation: This function is returning a Promise. This promise is resolving a datastore object. This datastore object should contain response object of type ResponseBuilder(). Here we building a response that contains a simple speech and a prompt.

Now Lazybot is waiting for user’s response in Yes or No. But here is a problem, Lazybot can ask different questions at different point of time in a conversation flow, Our Lazybot needs something to remember like what it has asked and for what user is replying Yes/No. To solve that we use a technique called as in-session context. And let’s see how easily we can use this awesome technique for our Lazybot with the help of alexa-nodejs-boilerplate’s dataStore.

To do that we just need to add an identifier (for asked the question) whenever we send our response back. When the next request will come, we will look for the identifier and then we can create an appropriate response. Just add the following line before resolving

dataStore.sessionData.context = ‘WeatherLookUpConfirmation’;

So our final function looks something like this —

As you can see here I have used WeatherLookUpConfirmation as an identifier to identify the context and stored it in dataStore.sessionData.context. we can use dataStore.sessionData for many different tasks and the data that we store, can be retrieved in the next request if the session is continued. But do remember that dataStore.sessionData should be an object. So just add key and values to sessionData.

Now we will add two more intents (AMAZON.YesIntent and AMAZON.NoIntent) to the developer portal and to our code. To add these two intents to the code we can use Nodlex’s addintent feature. It will automatically add check and method body for both these intents.

Once user will say Yes/No to our skill, we need to fetch the information like — For what user is saying Yes/No? As you remember while asking the question we have saved an identifier in our context memory. Now we can use the same object to fetch the context from. To fetch the context use —

let sessionContext = dataStore.sessionData.context;

Once we have this context information, we can write a simple check to see in which context user has said Yes/No.

And that’s all. Just deploy the above code and enjoy your first ever Nodlex Alexa Skill.

…, Over and Out.

Clap to recommend this article to others!

Chatbots Magazine

Chatbots, AI, NLP, Facebook Messenger, Slack, Telegram, and…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store