How to make SUSI AI Twitch Bot
In this blog post, we’ll learn how to make SUSI AI Twitch bot. SUSI.AI is an intelligent Open Source personal assistant. SUSI AI Bots are built to enable users to chat with SUSI on different clients. Twitch is a live streaming video platform. We can integrate SUSI AI to the live chat on any of its channels. Pre-requisites: A Main Twitch account (This is the account on whose channel users would be able to talk to SUSI) A separate Twitch account for SUSI. GitHub Account Create a GitHub from here - Sign up on GitHub Heroku Account To create Heroku account, go to Heroku and click on Sign Up. Node.js Install Node.js from https://nodejs.org/en/ if it isn’t installed already on your computer. To check if node is already installed or not, open terminal and type the command: node -v If you see something like this - (version can be different) v9.4.0 Then Node.js is installed on your computer and you can follow along. Procedure: You can either fork susi_twitchbot repository and then simply deploy it on Heroku to create SUSI AI Twitch bot or you can create a new repository on your account then deploy it. The following section will describe how to create a new repository for SUSI AI Twitch bot. If you want to deploy susi_twitchbot directly then skip this section and directly move on to the deployment section. Creating the SUSI AI Twitch codebase: 1. Create a folder on your computer with any name. Open terminal and change your current directory to the new folder that you just created. 2. Type npm init command and enter details like name, version etc. (preferably just keep pressing enter key and let the values stay default) 3. Create a file with the same name that you wrote in entry point (index.js by default). NOTE - It should be in the same folder that you created earlier. 4. In the same folder, type the following commands in command line - npm i -s tmi.js We need tmi.js module to easily interact with Twitch messaging interface. For more details, visit https://www.tmijs.org/. npm i -s request We need request module to make a GET request to the SUSI API and retrieve JSON data for a particular query. npm i -s express We need express module to create successful connection with Heroku through the port provided by it. 5. Open package.json file. It should look like this: (Adding “start” script is necessary for deploying the app on Heroku) { "name": "susi_twitchbot", "version": "1.0.0", "description": "SUSI.AI Twitchbot", "main": "index.js", "scripts": { "start": "node index.js" }, "author": "", "license": "ISC", "dependencies": { "express": "^4.16.3", "request": "^2.86.0", "tmi.js": "^1.2.1" } } 6. Copy the following code into the file that you created i.e. index.js var tmi = require('tmi.js'); var request = require('request'); const express = require('express'); const app = express(); const userChannel = process.env.CHANNEL; var ans; var options = { options: { debug: true }, connection: { reconnect: true }, identity: { username: process.env.USERNAME, password: process.env.OAUTH_TOKEN }, channels: [userChannel] }; var client = new tmi.client(options); // Connect the client to the server client.connect(); client.on('chat', function(channel, userstate, message, self){ if(message.includes("@"+process.env.USERNAME)){ // checking if SUSI is tagged var u = message.split("@" + process.env.USERNAME + " "); // Setting options to make a successful call to SUSI API var options1 = { method: 'GET', url: 'http://api.susi.ai/susi/chat.json', qs: { timezoneOffset: '-300', q: u[1] } }; request(options1, function(error, response, body) { if (error) throw new Error(error); if((JSON.parse(body)).answers[0]) ans = userstate['display-name'] + " " + (JSON.parse(body)).answers[0].actions[0].expression; else ans = userstate['display-name'] + " Sorry, I could not understand what you just said." …
