Writing a Slack
ChatBot
by Jesus Manuel Olivas / weKnow
ā— WHO AM I?
Jesus Manuel Olivas
jmolivas@weknowinc.com
jmolivas
jmolivas
drupal.org/u/jmolivas
jmolivas.weknowinc.com
WeGive
2,572,697
WeAre
WeKnow
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.Ā 
Do You Want to Build a
ChatBot?
Installation
# Installing Ā Botkit
npm install -g botkit
# Creating new project
botkit new —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
# Exposer port using ngrok
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
bot.reply(message, {
attachments:[
{
"title": ā€œLorem ipsum.",
"image_url": "https://imgs.xkcd.com/comics/twitter_bot_2x.png",
"color": "#ff0000"
}
]
});
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.
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;
bot.reply(message, 'intent: ' + intent + ', type: ' + type);
});
Thank you … Questions?

Writing a slack chatbot DrupalCampLA

  • 1.
    Writing a Slack ChatBot byJesus Manuel Olivas / weKnow
  • 2.
    ā— WHO AMI? Jesus Manuel Olivas jmolivas@weknowinc.com jmolivas jmolivas drupal.org/u/jmolivas jmolivas.weknowinc.com
  • 3.
  • 4.
  • 5.
  • 6.
    What is thissession 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
  • 7.
    What is aChatBot?
  • 8.
    What is aChatBot A computer program designed to mimic conversations with people via audio or text.
  • 9.
    But why aChatBot?
  • 10.
    Some of thethings we do in our every day lives, especially at our jobs, can be automated.
  • 11.
    Whenever a humanis involved in a repetitive task … there is a chance for an error.
  • 12.
  • 13.
    Do You Wantto Build a ChatBot?
  • 14.
    Installation # Installing Ā Botkit npminstall -g botkit # Creating new project botkit new —platform slack
  • 15.
    Start the application #Start Server node bot.js # Start server clientId=CLIENT_ID clientSecret=CLIENT_SECRET PORT=3000 node bot.js
  • 16.
    Start the application #Create .env file clientId=CLIENT_ID
 clientSecret=CLIENT_SECRET 
 PORT=3000 # Exposer port using ngrok ngrok http 3000
  • 17.
    Minimum configuration var Botkit= require('botkit'); var request = require('request'); var bot_options = { clientId: process.env.clientId, clientSecret: process.env.clientSecret, scopes: ['bot'] };
  • 18.
    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.’) });
  • 19.
    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.') });
  • 20.
    Return an attachment bot.reply(message,{ attachments:[ { "title": ā€œLorem ipsum.", "image_url": "https://imgs.xkcd.com/comics/twitter_bot_2x.png", "color": "#ff0000" } ] });
  • 21.
  • 22.
    API calls controller.hears(['show cryptoprices'], 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)] }] }); } }); });
  • 23.
  • 24.
    A key featurefor a bot is its ability to understand the intentions of humans and the extraction of relevant information from that intention
  • 25.
    Natural language processing(NLP) can be defined as the ability of a machine to analyze, understand, and generate human speech.
  • 26.
    The goal ofNLP is to make interactions between computers and humans feel exactly like interactions between humans and humans.
  • 30.
    Easily create textor voice based bots that humans can chat with on their preferred messaging platform. https://wit.ai/ Wit.AI
  • 31.
    botkit-witai # Require dependency npminstall --save botkit-witai # Start server clientId=CLIENT_ID clientSecret=CLIENT_SECRET PORT=3000 wit_ai_token=WIT_AI_TOKEN node bot.js
  • 32.
    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);
  • 33.
    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; bot.reply(message, 'intent: ' + intent + ', type: ' + type); });
  • 34.
    Thank you …Questions?