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 ' + token} };
We will send this options variable when making a request so that Gitter lets our request through and notifies us when a client messages to our SUSI chat room.
The res.on(‘data’) accepts a function which is called when a client messages to our SUSI chat room:
// making a request to gitter stream API var req = https.request(options, function(res) { res.on('data', function(chunk) { // do stuff } } req.on('error', function(e) { console.log('Hey something went wrong: ' + e.message); }); req.end();
According to the docs of REST API in Gitter, the JSON data that we receive when a client sends a message to a chat room is like this:
To get this response set in a variable, we can use this code snippet:
res.on('data', function(chunk) { var msg = chunk.toString(); if(msg != " \n"){ // If message is not an empty message var jsonMsg = JSON.parse(msg);
Now we have this json response as shown in the above figure in the jsonMsg variable. To extract the client’s message from this json object:
var clientMsg = jsonMsg.text;
As we now have the user query in clientMsg variable. Let’s call Susi API and fetch an answer from it for a query.
As an example, let’s first take the query as ‘hi’ and visit http://api.asksusi.com/susi/chat.json?q=hi from the browser. We will get a JSON object as follows:
{ "query": "hi", "count": 1, "client_id": "aG9zdF8xMDMuMjkuMjIyLjE4MA==", "query_date": "2017-07-17T02:29:44.171Z", "answers": [{ "data": [{ "0": "hi", "token_original": "hi", "token_canonical": "hi", "token_categorized": "hi", "timezoneOffset": "-330", "language": "en" }], "metadata": {"count": 1}, "actions": [{ "type": "answer", "language": "de", "expression": "Hallo!" }], "skills": ["/susi_skill_data/models/general/smalltalk/de/German-Standalone-aiml2susi.txt"] }], "answer_date": "2017-07-17T02:29:44.179Z", "answer_time": 8, "language": "en", "session": {"identity": { "type": "host", "name": "103.29.222.180", "anonymous": true }} }
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 for our client message, 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 susiOptions = { method: 'GET', url: 'http://api.asksusi.com/susi/chat.json', qs: { timezoneOffset: '-330', q: clientMsg //the client message sent to SUSI chat room. } }; // A request to the Susi bot request(susiOptions, 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. susiOptions). Pass the susiOptions object to our request function as its 1st parameter. The response from 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.
As we now have the answer, let’s call the API of Gitter to show our answer back to the user. Let’s code the request for that:
// To send a reply by Susi AI to client's message back to Gitter var gitterOptions = { method: 'POST', url: 'https://api.gitter.im/v1/rooms/'+roomId+'/chatMessages', headers: { 'authorization': 'Bearer '+ token , 'content-type': 'application/json', 'accept': 'application/json' }, body: { text: ans }, json: true }; // making the request to Gitter API request(gitterOptions, function (error, response, body) { if(error) throw new Error(error); console.log(body); });
Hence, we have made the basic chat work!
The streaming API of Gitter notifies us for every message sent to our chat room. We will also be notified about the reply message sent by ourselves. To not fall into an infinite loop of answers and questions by SUSI itself, we must include this line in our code:
res.on('data', function(chunk) { var msg = chunk.toString(); if(msg != ” \n”){ // If message is not an empty message var jsonMsg = JSON.parse(msg); if(jsonMsg.fromUser.displayName != 'SusiAI'){ // If it’s not our own answer // do stuff } } } req.on('error', function(e) { console.log('Hey something went wrong: ' + e.message); }); req.end();
The display name in my case is ‘SusiAI’, but it may be different in your case according to the Github or Twitter id made by you.
- Upload this code to Github.
- Connect the Heroku app to the Github repository, which has your code.
- Deploy on the development branch. If you intend to contribute, it is recommended to Enable Automatic Deploys.
Branch Deployment.
Successful Deployment.
- Go to your Gitter room created and enjoy chatting with Susi.Resources
You must be logged in to post a comment.