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('@'…
