Writing a Slack
ChatBot
Jesus Manuel Olivas / weKnowDrupalCon Seattle 2019
$ whoami
Jesus Manuel Olivas
jmolivas@weknowinc.com
jmolivas
jmolivas
drupal.org/u/jmolivas
jmolivas.weknowinc.com
Mexicali

Mexico + California
Calexico

California + Mexico
$ ifconfig
weAre
weKnow
weGive
4,067,294
What is this session about.
> What is a ChatBot?
> How to write a chatbot
> Enable support for Slack
> Integrate with an external API
> Integrate with Natural Language Processing service platform
What is a ChatBot?
What is a ChatBot
A computer program designed to
mimic conversations with people
via audio or text.
But why a ChatBot?
Some of the things we do in our
every day lives, especially at our
jobs, can be automated.
Whenever a human is involved in a
repetitive task … there is a chance
for an error.
Let the Robots do the work. 
Hi, my name is T-800
How Can I help you ?
User start day and ask bot about today meetings/calls
User ask bot about project/task status
User use bot to add time to time-tracking system
User ask bot about a topic on a knowledge base
Use case
Do You Want to Build a
ChatBot?
https://botkit.ai/
Botkit
Building Blocks for Building Bots
https://slack.com/
https://api.slack.com/
Installation
# Installing  Botkit
npm install -g botkit
# Creating new project
botkit new --name botkit --platform slack
Start the application
# Start Server
node bot.js
# Start server
clientId=CLIENT_ID clientSecret=CLIENT_SECRET PORT=3000
node bot.js
Start the application
# Create .env file
clientId=CLIENT_ID

clientSecret=CLIENT_SECRET 

PORT=3000
# Expose port using ngrok (for testing locally)
ngrok http 3000
Minimum configuration
var Botkit = require('botkit');
var request = require('request');
var bot_options = {
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
scopes: ['bot']
};
Hear for messages
// Create the Botkit controller, which controls instances of the bot.
var controller = Botkit.slackbot(bot_options);


controller.hears(['Hi'], 'ambient', function(bot, message) {
bot.reply(message, 'Hi ... I heard a message.’)
});
Hear for messages
// Create the Botkit controller, which controls instances of the bot.
var controller = Botkit.slackbot(bot_options);


controller.hears(['hola', 'que tal'], 'ambient', function(bot, message) {
bot.reply(message, 'Hola que tal.')
});
Return an attachment
controller.hears(['Open the pod bay doors'], incomingEvent, function(bot, message) {
bot.reply(message, {
attachments:[
{
"title": "I’m sorry, Dave, I’m afraid I can’t do that.",
"image_url": "https://i.ytimg.com/vi/NJ-CcFcM9Hw/maxresdefault.jpg",
"color": "#ff0000"
}
]
});
});
Slack block-kit-builder
https://api.slack.com/tools/block-kit-builder
Slack block-kit-builder (templates)
Integrate API … REST/GraphQL
API calls
controller.hears(['show crypto prices'], incomingEvent, function(bot, message) {
bot.reply(message, 'fetching information ...');
request({ method: ‘get', uri: 'https://api.bitso.com/v3/ticker', json: true }, function (err, response, json) {
let colors = [ 'good', 'warning', 'danger'];
for(var i = 0; i < json.payload.length; i++) {
bot.reply(message, {
attachments: [{
"title": json.payload[i].book,
"text": JSON.stringify(json.payload[i], undefined, 2),
"color": colors[Math.floor(Math.random()*colors.length)]
}]
});
}
});
});
Natural Language Processing
A key feature for a bot is its ability to
understand the intentions of humans and
the extraction of relevant information from
that intention
Natural language processing (NLP) can be
defined as the ability of a machine to
analyze, understand, and generate human
speech.
The goal of NLP is to make interactions
between computers and humans feel
exactly like interactions between humans
and humans.
NLP
Knowledge base
Easily create text or voice based bots that humans
can chat with on their preferred messaging platform.
https://wit.ai/
Wit.AI
botkit-witai
# Require dependency
npm install --save botkit-witai
# Start server
clientId=CLIENT_ID clientSecret=CLIENT_SECRET PORT=3000
wit_ai_token=WIT_AI_TOKEN node bot.js
botkit-witai
# Usage
var wit = require('botkit-witai')({
accessToken: process.env.wit_ai_token,
minConfidence: 0.6
});
# Register middleware
controller.middleware.receive.use(wit.receive);
botkit-witai
controller.hears(['.*'], incomingEvent, function (bot, message) {

var intent = message.entities.intent[0].value;
var intent_type = intent + '_type';
var type = message.entities[intent_type][0].value;
// Call your API to do something with the return payload
bot.reply(message, 'intent: ' + intent + ', type: ' + type);
});
Thank you … Questions?

Writing a slack chatbot seattle