Integration of SUSI AI in Twitter

We will be making a Susi messenger bot on Twitter. The messenger bot will tweet back to your tweets and reply instantly when you chat with it. Feel free to tweet to the already made SUSI AI account (mentioning @SusiAI1 in it). Follow it, to have a personal chat. Make a new account, which you want to use as the bot account. You can make one from sign up option from https://www.twitter.com. Prerequisites To create your account on -: 1. Twitter 2. Github 3. Heroku 4. Node js Setup your own Messenger Bot 1. Make a new app here, to know the access token and other properties for our application. These properties will help us communicate with Twitter. Click "modify the app permissions" link, as shown here: Select the Read, Write and Access direct messages option: Don't forget to click the update settings button at the bottom. Click the Generate My Access Token and Token Secret button. 3. Create a new heroku app here. This app will accept the requests from Twitter and Susi api. 4. Create a config variable by switching to settings page of your app.      The name of your first config variable should be HEROKU_URL and its value is the url address of the heroku app created by you.   The other config variables that need to be created will be these:   The corresponding names of these variables in the same order are:   i) Access token   ii) Access token secret   iii) Consumer key   iv) Consumer secret    We need to visit our app from here, the keys and access tokens tab will help us with the values of these variables. Let’s start with the code part of the integration of SUSI AI to Twitter. We will be using Node js to achieve this integration. First we need to require some packages: Now using the Twit module, we need to authenticate our requests, by using our environment variables as set up in step 4: Now let’s make a user stream: var stream = T.stream('user'); We will be using the capabilities of this stream, to catch events of getting tweeted or receiving a direct message by using: stream.on('tweet', functionToBeCalledWhenTweeted); stream.on('follow', functionToBeCalledWhenFollowed); stream.on('direct_message', functionToBeCalledWhenDirectMessaged); So, when a person tweets to our account like this: We can catch it with ‘tweet’ event and execute a set of instructions: stream.on('tweet', tweetEvent); function tweetEvent(eventMsg) { var replyto = eventMsg.in_reply_to_screen_name; // to store the message tweeted excluding '@SusiAI1' substring var text = eventMsg.text.substring(9); // to store the name of the tweeter var from = eventMsg.user.screen_name; if (replyto === 'SusiAI1') { var queryUrl = 'http://api.asksusi.com/susi/chat.json?q=' + encodeURI(text); var message = ''; request({ url: queryUrl, json: true }, function (err, response, data) { if (!err && response.statusCode === 200) { // fetching the answer from the data object returned message = data.answers[0].actions[0].expression + data; } else { message = 'Oops, Looks like Susi is taking a break'; console.log(err); } console.log(message); // If the message length is more than tweet limit if(message.length > 140){ tweetIt('@'…

Continue ReadingIntegration of SUSI AI in Twitter

Integration of Susi AI to Gitter

This blog post discusses the development of Susi Messenger bot on Gitter. It replies instantly to the messages sent to it, using the Susi API. The Streaming API notifies us when a user messages to the SUSI chat room. The REST API helps to message back with a reply from SUSI API, to the SUSI chat room. Feel free to message to the already made SUSI AI account on Gitter and have a chat with it. Prerequisites Basic knowledge about calling API’s and fetching data or posting data to the API. Node.js language. Github Heroku Figure - Architecture for running SUSI AI on different messaging services. This blog post will walk you through each of the steps required to integrate SUSI AI to Gitter: Setup SUSI AI Bot on Gitter Create a Github or twitter account with a username having 'Susi' as its substring because this is the name that will be shown with the reply string we will get from Susi AI. Now you need to sign in to Gitter with a twitter or Github account from here. Create your community by visiting this page. After writing your community name press next, invite the people you want to be in this room and press next. You will be redirected to your communities lobby. This lobby is the chat room to which we will deploy our SUSI AI. Now visit the Gitter developer page, press sign in on the top right. You will be redirected to your apps page. Copy the personal access token written there as shown in this image: (The area colored black will have your access token). 5. On a new tab, in your browser visit   https://api.gitter.im/v1/rooms?access_token=YOUR_ACCESS_TOKEN, with YOUR_ACCESS_TOKEN replaced by the token we just copied. A JSON object will be shown on our browser screen. You will see the value of 'name' key as YOUR_COMMUNITY_NAME/Lobby. Copy the id of this chat room, as we will need it later. You can refer to the image below, you will have your chat room id in the area colored black. Create a new heroku app here. This app will accept the requests from Gitter and Susi api. Set the config variables for this heroku app in the setting tab of your account. Set ROOM_ID to the id of the chat room and TOKEN to the personal access token, we copied in steps 4 and 5. These were the formalities to be done to have our chat bot account on Gitter. Let’s jump to the code part of how this integration will be done: To use the two config variables set in Heroku, we need these two lines in our Node js code: var roomId = process.env.ROOM_ID; var token = process.env.TOKEN; We need to set up an options variable with our access token and room id in it: // Setting the options variable to use it in the https request block var options = { hostname: 'stream.gitter.im', port: 443, path: '/v1/rooms/' + roomId + '/chatMessages', method: 'GET', headers: {'Authorization': 'Bearer '…

Continue ReadingIntegration of Susi AI to Gitter

Using Variables in a SUSI skill

One of the best feature provided in making a skill is the ease of using variables. From storing the favourite book of the user to the most recent movie he searched for to the mood he is in, variables play an indispensable part. If any problem is faced with the code part, the skill referred in this blog is coded in this file in susi_skill_data repository.  This link refers to the official docs of SUSI, which walk you through some basic examples of how to use variables in a SUSI skill. Great skills can be achieved using them like the skill below: It’s easy to make such skills by using variables. Let’s check it out how this skill can be achieved. To store value in a variable we use this syntax during the skill development ^value^>_variableName First, let’s save the favourite dish of the user and then we will try to surprise him/her with a witty answer. I love * dish ^$1$^>_userFavouriteDish So, if the user types “I love biryani dish”, $1$ will be equal to biryani. Let’s save it to _userFavouriteDish variable. Now if user asks “What should i eat” to SUSI, I bet SUSI will answer a well calculated answer! What should i eat? I am sure you will love $_userFavouriteDish$! Another example that can answer back the user efficiently: How to cook biryani? #Gives recipies and links to cook a dish * cook * !console:To cook $title$ , check out $href$ and make sure you have $ingredients$! ^$2$^>_recentSearch { "url":"http://www.recipepuppy.com/api/?q=$2$", "path":"$.results" } eol In the above code, we saved the dish searched for at the end of the output. If somehow user ends up asking “what is the most recent dish i searched for”. It’s skill will be: what is the most recent dish I searched for? It was $_recentSearch$ Even if before asking this question, user asks “how to cook sushi”. The _recentSearch variable will be overridden with value “sushi” instead of “biryani”. Hence, SUSI won’t mistake answering “most recent dish” as “sushi”! Now I think we are bit comfortable with use of variables in a skill. Let’s get back to our target skill i.e. remembering skill. We store the thing asked to remember in a variable having the same name as of that thing and the statement related to it as the value of that variable. Examples: Remember that my keys are on the table. So the variable will be named “keys” and it’s value will be “on the table”. Remember that my birthday is on 20th of December. So the variable will be named “birthday” and it’s value will be “on 20th of December”. Remember that my meetings are at 8 pm with mentors and at 9:30 pm with Shruti. So the variable will be named “meetings” and it’s value will be “at 8 pm with mentors and at 9:30 pm with Shruti”. Hence the skill: Remember that my * is * | Remember that my * is * Okay, remembered!^$2$^>_$1$ When the user will ask for any of its…

Continue ReadingUsing Variables in a SUSI skill

Integration of SUSI AI to Alexa

An Alexa skill which can be used to ask susi for answers like: "Alexa, ask susi chat who are you" or "Alexa, ask susi chat what is the temperature in berlin". If at any point of time, you are unclear about the code in the blog post, you can check the code of the already made SUSI Alexa skill from the susi_alexa_skill repository. Getting Started : Alexa Susi AI Skill Follow the instructions below: Visit the Amazon developer site and Login. Click Alexa, on the top bar. Click Alexa skills kit. Click on add a new skill button on the top right of the page. We will be at the skill information tab.   Write the name of the skill Write the invocation name of the skill i.e. the name that will be used to trigger your skill. Like in our case, if we need to ask anything (as we have 'susi chat' as the invocation name), we will ask with "Alexa, ask susi chat" as a prefix to our question sentence. By clicking next, we will be redirected to the second tab i.e. Interaction model. We need to fill two fields here i.e. intent schema and sample utterances. For intent schema, we need to write all the available intents and the parameters for each of them. Like in our case: { "intents": [ { "slots": [ { "name": "query", "type": "AMAZON.LITERAL" } ], "intent": "callSusiApi" } ] } We have a single intent that is "callSusiApi" and the parameter it accepts is "query" of type "AMAZON.LITERAL" (in simple words, a string type). Parameters are termed as slots here. The different types of slots available, can be seen from here. For sample utterances, we need to tell what utterances by the client will lead to what intent. In our case: We have just one intent and the whole string uttered by the client should be fed to this intent as a "query" slot (parameter). Let's click next now. We will be shifted to the configuration tab. We will be making a lambda function, which will hold the code for our Susi skill, further we need to link that code to this skill. To do the linking we need to get the Amazon resource name i.e. ARN and fill it in the field named endpoint: To get the amazon resource name, in a new tab, visit here. Visit "Lambda" followed by get started button. Click on "Create a lambda function": We need to select a blueprint for our lambda function. Select the "blank function" option for that. Click next. For configure triggers option, click this box and select "Alexa skills kit" option. Click next. In configure function tab, just write the name of the function and its description. Let’s code our lambda function: // basic syntax that should be available in the lambda function var https = require('http'); exports.handler = (event, context) => { try { if (event.session.new) { // New Session console.log("NEW SESSION") } switch (event.request.type) { case "LaunchRequest": //…

Continue ReadingIntegration of SUSI AI to Alexa

Deploy SUSI.AI to a Messenger

Integration of SUSI AI to messenger platform has become a vital step as to enhance the popularity of this chatbot and to target a large base of users. For example - Viber claims that it has a user base of 800 million. So just integrating SUSI AI to Viber can increase its user base exponentially. This integration also proves to be a big boon, if the chat bot learns with the number and variations in the questions being asked. Like in the case of the web chat client (Susi AI). This blog post will walk you through on how to deploy SUSI.AI to a messenger platform (Viber and Line messengers are used as an example in this post). We will be using Node.js and REST API technology in our example integrations. The repository of deployment of Susi AI to Viber can be found at susi_viberbot, and to Line messenger at susi_linebot. The SUSI AI Viberbot can be followed from here and Linebot by scanning this QR code. The diagram below will give you an overview on what flow is followed to deploy SUSI AI chatbot to various messenger platforms. Fig: Integration of Susi AI to chat messengers. Let’s walk through each of the steps mentioned in the above diagram. To get familiar with SUSI.AI chatbot. We have an API from where we fetch answers. To get a reply for the query ‘hi’, we can visit the API link with the query ‘hi’ appended to it (http://api.susi.ai/susi/chat.json?q=hi). You can chat with SUSI AI here. To set up a private SUSI AI chatbot account. A account must be set up in the messenger platform, so that the user can message in that account to get a reply by the chatbot. Steps to set up the chatbot account is dependent on the messenger platform. To set up a webhook url. The message sent to the chatbot account, must somehow connect to the chatbot. This message can be fed as a query to the chatbot, so that accordingly chatbot can think of a reply. To achieve this we need a url referred to as the webhook url. The messages sent by the user, to the SUSI AI chatbot account on the messenger, can then be redirected to this url. (Heroku platform allows 5 apps to be hosted on its platform for free, so you can check this documentation on how to host a node js app there.) Now we need to think on how to handle these messages. To host code on our webhook url As said earlier, we will be using Node js technology. Generally, the messages from our SUSI AI chatbot account on the messenger will travel as requests to our webhook url. These come as POST requests to our url. To handle that we can use this piece of code: app.post('/', function(request, response) { response.writeHead(200); // first step here, getting the message string from the request body // second step, calling the chatbot to get the reply to this message //…

Continue ReadingDeploy SUSI.AI to a Messenger

Deploy SUSI AI to Viber messenger

Prerequisites Basic knowledge about calling API’s and fetching data or posting data to the API. Node.js language. Github Heroku Fig - Architecture for running all different messaging services. To integrate Susi AI chat to Viber, a public account is needed, messaging to which users can chat with Susi. We need to have a webhook url. Webhook url is a url which serves our Node.js code i.e. the code we will write to serve requests from Viber and to respond back to it. Whenever a user messages to the SUSI AI public account, these messages come as post requests to our webhook url. The url then requests Susi API to give an answer for the (question based) message received from Viber. The answer fetched from Susi API is sent to the messenger platform’s API by the webhook url, to show it to the user on Viber. As said we need a public account for our chat bot. The steps to be followed can be seen from here (Steps 2 and 3). The REST API helps to make applications follow a RESTful way. In this way, the requests and response are in the form of JSON objects. Any language can be used to make an application follow a RESTful way. In this blog, I will be using Node.js language. The Rest API Viber, is the document to be followed for integration of a chatbot to Viber. Let’s go through each of the steps: To call Susi API and fetch an answer from it for a query (‘hi’ in this case). Let's first visit http://api.asksusi.com/susi/chat.json?q=hi from the browser. We will get a JSON object as follows: The answer can be found as the value of the key named expression. In this case it is “Hallo!”. To fetch the answer through coding, we can use this code snippet in Node js: // including request module var request = require(‘request’); // setting options to make a successful call to Susi API. var options = { method: 'GET', url: 'http://api.asksusi.com/susi/chat.json', qs: { timezoneOffset: '-330', q:'hi' } }; // A request to the Susi bot request(options, function (error, response, body) { if(error) throw new Error(error); //answer fetched from susi ans = (JSON.parse(body)).answers[0].actions[0].expression; } The properties required for the call are set up through a json object (i.e. options). Pass the options object to our request function as its 1st parameter. The response by the API will be stored in ‘body’ variable. We need to parse this body, to be able to access the properties of that body object. Hence, fetching the answer from Susi API. Let’s set the webhook url for our Susi public account. The folder containing our Node.js code must be pushed to a repo in github. We need to do some changes to the default package.json file in our project. This file has a code portion: The “test” key and its value must be replaced with "start": "node index.js" i.e. node followed by the name of the main file which has to accept the…

Continue ReadingDeploy SUSI AI to Viber messenger

How to teach SUSI.AI skills using external API’s

A powerful feature of SUSI is, that it can use external API’s to learn new skills. The generic syntax used here is: Question string !console:constant answer string + answer variable { "url" : "API to be called", "path" : "path from where answer will be fetched" } eol I will try to explain this syntax with the help of some useful examples. Let’s start with a very basic example: I want SUSI to be able to answer questions like “What is the date today?”. Let’s try to tackle this step by step. As we can infer from the above-written syntax, to teach SUSI a skill involving external API call, we need to be clear about five things namely: Question string i.e. “What is the date today?” (in this case). Constant answer string i.e. “The date today is ” The API to be called i.e. “http://calapi.inadiutorium.cz/api/v0/en/calendars/default/today” The path which contains our answer. When we visit this API url, we get the result as follows: {   "date":"2017-05-16",   "season":"easter",   "season_week":5,   "celebrations":[     {       "title":"",       "colour":"white",       "rank":"ferial",       "rank_num":3.13     }   ],   "weekday":"tuesday" } The whole JSON object is represented with the ‘$’ sign. As date is a property of this object, so date can be accessed with “$.date” - this string is referred to as the path.  The last one is the answer variable. We can see that the result of API url contains many “key:value” pairs. Answer variable is the value of the last key variable(i.e. date) referred in path string. This value is stored in a variable named $object$. So our answer variable turns out to be $object$.    Now, as we have all the five things ready with us, we can make our SUSI skill: What is the date today? !console:$object$ { “url”:“http://calapi.inadiutorium.cz/api/v0/en/calendars/default/today”, “path” : “$.date” } eol Kudos! But where to feed this skill and check if SUSI chat bot is able to answer “What is the date today?” appropriately. To test the working of a skill: Open dream.asksusi.com, write whatever name you like for the pad and then click OK. Replace the data written on your pad with the skill code you created. You don’t need to save it, it is saved automatically. Now your page should look something like this: To check if this skill is working properly: Visit SUSI chat bot. In the textbox below, write dream followed by the name of your pad and then press Enter key. SUSI will reply with “dreaming enabled for YOUR-PAD-NAME”. Now write the question string i.e. What is the date today? and you should be shown today’s date! For more clarity, refer to this image: Great, that you made it! You can now contribute skills by making a PR to this repository and see those skills live on SUSI without enabling any dream! Just ask your question and get your own skilled answers. Let’s learn more about skills by introducing some changes to this question. Let’s go through some variations of this question:…

Continue ReadingHow to teach SUSI.AI skills using external API’s