Dom Steil

Bot Development

CryptoSlackBot: n. A bot that executes buy and sell orders on cryptocurrency exchanges.

Aptbot: n. A bot that executes Quote-to-Cash functions from within messaging platforms.

Do you want to build your own slack bot, universal bot, facebook messenger bot? Eventually list it as an App and publish the bot to a directory?

Over the past 5 months I have developed bots for: Slack, Skype, and mobile web.

There is a common pattern when building these bots: dialog triggers, parameter gathering, message brokering, and response formatting. In this article I will explain:

How to Build a Bot How to connect the Bot with any REST API How to host and deploy the Bot How to create a Bot API How to Build a Bot

These are the tools and languages you are going to use to build the bot:

Node.js NPM Javascript or C# Heroku Toolbelt Azure Command Line Git Bash Github or Bitbucket’s Botkit Microsoft’s Botbuilder When building a bot it is important to understand that you are abstracting away the UI with intelligent cognitive services. There is an abundance of web based services and applications that are locked into their own interfaces. We have arrived at a time where often we interact with apps without a UI. Why do I need a phone to call an Uber, can’t I just tell the uber bot in a Slack channel to pick me up from work and drop me off at the train. The bot needs two parameters: from and to.

This new paradigm of having intelligent assistants is deemed conversation-as-a-service.

The front-end is the conversations that you will have with the Bot to gather the paramaters that are passed to your server in the form of a serialized JSON object. The server deserializes it, and responds back to your bot with its own serialized JSON object.

The user interacts with the bot –> formats the request with the parameters –> the API retrieves the call data –> responds with error/data –> formats the response to the user.

When gathering the parameters with a bot you need to initiate the conversation or dialog. To start a conversation or dialog you include an array of words associated with a particular conversation. When the bot hear to execute a program, this is exactly triggers the specific dialogue, and ultimately what drives the JSON request to the API service.

You can also combine Natural Language Processing with your bot to make it more intelligent using a service like LUIS AI. Take an utterance and match it to a particular intent which is tied to a set of questions (the dialog).

Once this is achieved, the bot will store and pass this as a request in a serialized JSON object, the server deserializes it and responds back with callback data.

Everything in Javascript is an object.

Everything, in Javascript, is an object.

URL Hacking | Passing of Parameters

Once you pass the various parameters needed to the API, it responds back to your Bot at which point you can drive another action.

Essentially you are pulling in the functionality of any web service into a command line where you have interactive programs branching and executing various functions.

Let’s Start Building

There are a number of company’s that enable you to build a bot:

Howdy.Ai’s Botkit Microsoft Universal Botbuilder API.AI In order to get one of these up an running you are going to need to execute the following code.

Botkit for a Slack Bot

In this turorial I am using Botkit.

cd c:
mkdir newbot
cd newbot

npm install botkit
npm install superagent
npm install body-parser
npm install express

npm init

git init
git commit "first commit"
git push -u origin master

git add .
git heroku:set -a <appname>
git push heroku master

Let’s break it down:

The first thing we are going to do is create a new folder for the bot application:

cd c: mkdir newbot cd newbot Once you have your new folder you are going to create an app on Heroku.

Then you need to install the various NPM packages that you need to get the bot up and running.’s Botkit is a great way to customize and get a bot up and running for a variety of different messenger platforms.

npm install botkit npm install body-parser npm install express npm install superagent Express will be used for the server and body-parser will be used to format the hypercards in the platform channels.

npm init will create your app’s JSON package.

npm init Once you have your application setup the next step is to deploy it to Heroku.

Make sure you setup a Procfile so that your bot can “live” on the server.

Create a Procfile in the directory with the text worker: node server.js Now that you have the base bot app setup you can begin deploying the code to Github.

Create the repo on Github and then set the remote in your directory.

git init git commit "first commit" git push -u origin master

Connecting Bots to a REST API

Download the npm module of whatever rest api or wrapper you want to bot to connect with. Check out my github page to see examples.

Deploying to Heroku

It is important to configure the vars in your Heroku Application so that they are not explicitly declared in your code.

The variables needed are your Slack Apps Client ID and Client Secret.

git add . git heroku:set -a git push heroku master Deploying to Azure

It is important to configure the vars in your Azure Application so that they are not explicitly declared in your code. You also want to register your app on the bot framework you can connect to various platforms/services.

The variables needed are your Microsoft App ID and Microsoft Password.

Create an Azure account and create a new Web App. On the Bot Framework portal, set your website and app id/password.

If your web app is up and running and your bot is deployed you ahould be able to interact with it on your site.

OAUTH2 and Slack

Instead of just having a custom integration bot, what about creating an actual Slack app?

This is arguably the trickiest part of getting your app up and running.

You want to enable other Slack teams to use your bot in a secure way.

Slack Authenticate’s users by exchanging an authentication token for a bot token.

In doing so it gives your app access to the org with the different access scope.

This can be for bots, /commands, and webhooks user access.

The key is creating the 1) response http request 2) then having the response 3) redirect to the /x http request 4) which contains the oAuth 5) and grant’s access.

One way to test is using ngrok and pointing the request URL to your local server which has the public reachable IP through grok.

Enter the Grok ID on your App ID in Slack, in the interactive buttons, and the URLs.

start up ngrok

ngrok http 8080 then start up your app locally

node server.js then inspect the ngrok requests

ngrok http 4040 lastly, go to localserver in the browser.

In Conclusion

The bot logic can be used across multiple platforms in that there can be multiple inputs from any messenging platform/sensors/Holograms; the bot is the broker of these inputs to other platforms. When building keep this in mind. The bot is and will continue to be a catalyst for web services to be brought into single platforms. The new OS is immersive in that we have untapped, disconnected mediums that can now leverage the same bot logic and state.

Next part will be on how to create your Bot’s own API.