SlideShare a Scribd company logo
Bots: Basics
WHOAMI
Front End Lead @ Rails Reactor
github.com/viattik
twitter.com/vi_attik
Bots: Basics @ Javascript Tonight, 2017.09.22
What is Chatbot?
Motivation
• Alternative UX
• ~90% of our time on mobile is spent on email
and messaging platforms
• Cheap and fast to develop
https://chatbotsmagazine.com/the-complete-beginner-s-guide-to-chatbots-8280b7b906ca
Examples
Bots: Basics @ Javascript Tonight, 2017.09.22
Bots: Basics @ Javascript Tonight, 2017.09.22
https://t.me/chatwarsbot
Bots: Basics @ Javascript Tonight, 2017.09.22
Bots: Basics @ Javascript Tonight, 2017.09.22
Bots: Basics @ Javascript Tonight, 2017.09.22
Bots: Basics @ Javascript Tonight, 2017.09.22
Platforms
• Telegram
• Slack
• Messenger
• Viber
• …
+ =
https://github.com/viattik/pokedex-bot
Libraries used
• Telegram:
• https://core.telegram.org/bots
• https://github.com/yagop/node-telegram-bot-
api
• PokeAPI:
• https://pokeapi.co
• https://github.com/PokeAPI/pokedex-promise-
v2
Creating bot
• Register bot via BotFather
• Initialise Telegram and PokeAPI
• Handle message
• Define replies
Bots: Basics @ Javascript Tonight, 2017.09.22
Creating bot
• Register bot via BotFather
• Initialise Telegram and PokeAPI
• Handle message
• Define replies
const TelegramBot = require(‘node-telegram-bot-api');

const telegram = new TelegramBot(
“YOUR_BOT_TOKEN",
{ polling: true }
);

const PokedexPromise = require('pokedex-promise-v2');
const pokedex = new PokedexPromise();
Creating bot
• Register bot via BotFather
• Initialise Telegram and PokeAPI
• Handle message
• Define replies
function onMessage(message) {

const text = message.text;

getReply(message.chat.id, text).then((reply) => {

if (!reply) { reply = getDefaultReply(); }

telegram.sendMessage(message.chat.id, reply.text, reply.form);
// or telegram.sendPhoto, telegram.sendAudio etc.

});

}
telegram.on('text', onMessage);
Creating bot
• Register bot via BotFather
• Initialise Telegram and PokeAPI
• Handle message
• Define replies
module.exports.intents = {

'/pokemons': (pageNumber = 0) => {

return pokedex.getPokemonsList(getInterval(pageNumber))

.then(({ results }) => {

return {

reply: TEXTS.POKEMON_LIST(pageNumber, results),

form: {

reply_markup: {

keyboard: [

pageNumber === 0

? [ BUTTONS.MAIN_MENU, BUTTONS.NEXT_PAGE ]

: [ BUTTONS.MAIN_MENU, BUTTONS.PREV_PAGE, BUTTONS.NEXT_PAGE ],

],

resize_keyboard: true,

one_time_keyboard: true,

},

}

};

});

},

'/pokemon': (name) => {

return pokedex.getPokemonByName(name)

.then((response) => {

return [

{ replyMethod: 'sendPhoto', reply: response.sprites.front_default },

{ reply: TEXTS.POKEMON(response) }

];

});

},
...
};
https://t.me/JSTonightPokedexBot
+ +
=
https://github.com/viattik/pokedex-bot
https://your_domain.slack.com/apps/manage/custom-
integrations
Libraries used
• Slack:
• https://api.slack.com/bot-users
• https://scotch.io/tutorials/building-a-slack-bot-with-node-js-and-
chuck-norris-super-powers
• https://www.npmjs.com/package/slackbots
• Wit:
• https://wit.ai
• https://github.com/wit-ai/node-wit
• PokeAPI:
• https://pokeapi.co
• https://github.com/PokeAPI/pokedex-promise-v2
wit.ai
• Natural Language Interface
• Is a part of Facebook
• Easy to start
• Alternative - api.ai
Bots: Basics @ Javascript Tonight, 2017.09.22
Bots: Basics @ Javascript Tonight, 2017.09.22
Bots: Basics @ Javascript Tonight, 2017.09.22
Replies
type + type_name + know_about
=
Information about type
contact (pokemon) + know_about
=
Information about
pokemon
contact + weight + know_about
=
Pokemon weight
greetings
=
hi
(and information about
bot if the first call)
thanks
=
you’re welcome
bye
=
bye
answer (yes or no)
=
…
answer (yes or no)
=
depends on previous
message
const slackSettings = {

token: ‘BOT_TOKEN',

name: 'pokedex'

};

const witAccessToken = ‘WIT_TOKEN';



const slack = new Bot(slackSettings);

const wit = new Wit({ accessToken: witAccessToken });

const pokedex = new Pokedex();
slack.on('message', (message) => {

if (isChatMessage(message) && !isMessageFromPokedex(message)

&& (

!isChannelConversation(message)

|| isChannelConversation(message) && isMentioningPokedex(message)

)

) {

const text = removeMentions(message.text);

wit.message(text).then((data) => {

const processed = processIntent(message, data);

if (!processed) {

postReply(message, TEXTS.DIDNT_UNDERSTAND);

}

pushConversation(message, data);

})

}

});
function processIntent(message, data) {

const intents = data.entities;

logIntents(message, data);

if (intents.greetings) {

const user = getUser(message);

const greetingMessages = getMessagesWithIntents(message,
'greetings');

const text = !greetingMessages.length

? TEXTS.FIRST_GREETING(user)

: TEXTS.RANDOM_GREETING(user);

postReply(message, text);

return true;

}

...

}
function processIntent(message, data) {
...



if (intents.know_about && intents.pokemons) {

postReply(message, TEXTS.POKEMONS_INFO);

return true;

}



if (intents.type && intents.type_name && (intents.know_about
|| intents.question)) {

const typeName = intents.type_name[0].value.toLowerCase();

pokedex.getTypeByName(typeName)

.then((response) => {

postReply(message, TEXTS.TYPE(response));

}).catch((error) => {

if (error.statusCode === 404) {

postReply(message, TEXTS.TYPE_NOT_FOUND);

}

});

return true;

}
...

}
function processIntent(message, data) {
...

if (intents.answer) {

const lastMessage = getLastMessage(message);

if (lastMessage && lastMessage.wit.entities.thanks) {

const answer = intents.answer[0].value;

const text = answer.toLowerCase() === 'yes' ?
TEXTS.OK_HIT_ME : TEXTS.ALRIGHT;

postReply(message, text);

return true;

}

}

}
Bots: Basics @ Javascript Tonight, 2017.09.22
Bots: Basics @ Javascript Tonight, 2017.09.22
Bots: Basics @ Javascript Tonight, 2017.09.22
Bots: Basics @ Javascript Tonight, 2017.09.22
Bots: Basics @ Javascript Tonight, 2017.09.22
Issues
• Wit.ai works, but needs some learning
• Wit.ai works, but pay attention to ‘BETA’ marks
• Save history - needed for yes-no answers
Where to start?
https://chatbotsmagazine.com/
… and more
• Tools
• https://botmock.com
• https://chatfuel.com
• http://botanalytics.co
• Bots:
• https://www.messenger.com/t/mereminder
• https://www.chatbots.org
• https://tiv.ai/me
https://frameworksdays.com/event/frontend-fwdays-17
Contest
+ =
https://t.me/JSTContestBot

More Related Content

PDF
ATX Bot Talk - Hello PyBot
PDF
The lifecycle of a chatbot
PDF
Build your first messenger bot
PDF
Building chat bots using ai platforms (wit.ai or api.ai) in nodejs
PDF
How to Create a Telegram Bot in 5 Easy Steps.pdf
PDF
How to Create a Telegram Bot in 5 Easy Steps.pdf
PDF
How to Create a Telegram Bot in 5 Easy Steps.pdf
PDF
How to Create a Telegram Bot in 5 Easy Steps.pdf
ATX Bot Talk - Hello PyBot
The lifecycle of a chatbot
Build your first messenger bot
Building chat bots using ai platforms (wit.ai or api.ai) in nodejs
How to Create a Telegram Bot in 5 Easy Steps.pdf
How to Create a Telegram Bot in 5 Easy Steps.pdf
How to Create a Telegram Bot in 5 Easy Steps.pdf
How to Create a Telegram Bot in 5 Easy Steps.pdf

Similar to Bots: Basics @ Javascript Tonight, 2017.09.22 (20)

PDF
Identifying and solving enterprise problems
PDF
Create a Bot with Delphi and Telegram - ITDevCon 2016
PPTX
Build your first Chatbot
PDF
Building Enterprise Chat Bots
PDF
Building Messenger Bots
PDF
How to Build a Serverless Chatbot for $0?
PDF
S377 telegrambot
PDF
Chat Bots and how to build a Slack bot
DOCX
PPTX
Abstract On Telegram Bot Using Python.pptx
PPTX
Getting Started With bots
PPTX
CHATBOT using Facebook Messenger
PDF
Introduction to Facebook Messenger, Conversational UI & NLP
PPTX
Building Chatbots
DOCX
A Comprehensive Overview of Chatbot Development_ Tools and Best Practices.docx
PPTX
Chatbots
DOCX
Ct bot tutorial
PDF
Telegram bots with python why not- v2.0
PDF
Build a great conversationalist using Azure Bot Service 2018
PDF
Story of a Cerberus Bot
Identifying and solving enterprise problems
Create a Bot with Delphi and Telegram - ITDevCon 2016
Build your first Chatbot
Building Enterprise Chat Bots
Building Messenger Bots
How to Build a Serverless Chatbot for $0?
S377 telegrambot
Chat Bots and how to build a Slack bot
Abstract On Telegram Bot Using Python.pptx
Getting Started With bots
CHATBOT using Facebook Messenger
Introduction to Facebook Messenger, Conversational UI & NLP
Building Chatbots
A Comprehensive Overview of Chatbot Development_ Tools and Best Practices.docx
Chatbots
Ct bot tutorial
Telegram bots with python why not- v2.0
Build a great conversationalist using Azure Bot Service 2018
Story of a Cerberus Bot
Ad

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Machine learning based COVID-19 study performance prediction
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
cuic standard and advanced reporting.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
A Presentation on Artificial Intelligence
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Assigned Numbers - 2025 - Bluetooth® Document
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Big Data Technologies - Introduction.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Machine learning based COVID-19 study performance prediction
Review of recent advances in non-invasive hemoglobin estimation
MIND Revenue Release Quarter 2 2025 Press Release
cuic standard and advanced reporting.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectroscopy.pptx food analysis technology
Programs and apps: productivity, graphics, security and other tools
Per capita expenditure prediction using model stacking based on satellite ima...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
A comparative analysis of optical character recognition models for extracting...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
A Presentation on Artificial Intelligence
Ad

Bots: Basics @ Javascript Tonight, 2017.09.22

  • 2. WHOAMI Front End Lead @ Rails Reactor github.com/viattik twitter.com/vi_attik
  • 5. Motivation • Alternative UX • ~90% of our time on mobile is spent on email and messaging platforms • Cheap and fast to develop https://chatbotsmagazine.com/the-complete-beginner-s-guide-to-chatbots-8280b7b906ca
  • 14. Platforms • Telegram • Slack • Messenger • Viber • …
  • 16. Libraries used • Telegram: • https://core.telegram.org/bots • https://github.com/yagop/node-telegram-bot- api • PokeAPI: • https://pokeapi.co • https://github.com/PokeAPI/pokedex-promise- v2
  • 17. Creating bot • Register bot via BotFather • Initialise Telegram and PokeAPI • Handle message • Define replies
  • 19. Creating bot • Register bot via BotFather • Initialise Telegram and PokeAPI • Handle message • Define replies
  • 20. const TelegramBot = require(‘node-telegram-bot-api');
 const telegram = new TelegramBot( “YOUR_BOT_TOKEN", { polling: true } );
 const PokedexPromise = require('pokedex-promise-v2'); const pokedex = new PokedexPromise();
  • 21. Creating bot • Register bot via BotFather • Initialise Telegram and PokeAPI • Handle message • Define replies
  • 22. function onMessage(message) {
 const text = message.text;
 getReply(message.chat.id, text).then((reply) => {
 if (!reply) { reply = getDefaultReply(); }
 telegram.sendMessage(message.chat.id, reply.text, reply.form); // or telegram.sendPhoto, telegram.sendAudio etc.
 });
 } telegram.on('text', onMessage);
  • 23. Creating bot • Register bot via BotFather • Initialise Telegram and PokeAPI • Handle message • Define replies
  • 24. module.exports.intents = {
 '/pokemons': (pageNumber = 0) => {
 return pokedex.getPokemonsList(getInterval(pageNumber))
 .then(({ results }) => {
 return {
 reply: TEXTS.POKEMON_LIST(pageNumber, results),
 form: {
 reply_markup: {
 keyboard: [
 pageNumber === 0
 ? [ BUTTONS.MAIN_MENU, BUTTONS.NEXT_PAGE ]
 : [ BUTTONS.MAIN_MENU, BUTTONS.PREV_PAGE, BUTTONS.NEXT_PAGE ],
 ],
 resize_keyboard: true,
 one_time_keyboard: true,
 },
 }
 };
 });
 },
 '/pokemon': (name) => {
 return pokedex.getPokemonByName(name)
 .then((response) => {
 return [
 { replyMethod: 'sendPhoto', reply: response.sprites.front_default },
 { reply: TEXTS.POKEMON(response) }
 ];
 });
 }, ... };
  • 28. Libraries used • Slack: • https://api.slack.com/bot-users • https://scotch.io/tutorials/building-a-slack-bot-with-node-js-and- chuck-norris-super-powers • https://www.npmjs.com/package/slackbots • Wit: • https://wit.ai • https://github.com/wit-ai/node-wit • PokeAPI: • https://pokeapi.co • https://github.com/PokeAPI/pokedex-promise-v2
  • 29. wit.ai • Natural Language Interface • Is a part of Facebook • Easy to start • Alternative - api.ai
  • 34. type + type_name + know_about = Information about type
  • 35. contact (pokemon) + know_about = Information about pokemon
  • 36. contact + weight + know_about = Pokemon weight
  • 40. answer (yes or no) = …
  • 41. answer (yes or no) = depends on previous message
  • 42. const slackSettings = {
 token: ‘BOT_TOKEN',
 name: 'pokedex'
 };
 const witAccessToken = ‘WIT_TOKEN';
 
 const slack = new Bot(slackSettings);
 const wit = new Wit({ accessToken: witAccessToken });
 const pokedex = new Pokedex();
  • 43. slack.on('message', (message) => {
 if (isChatMessage(message) && !isMessageFromPokedex(message)
 && (
 !isChannelConversation(message)
 || isChannelConversation(message) && isMentioningPokedex(message)
 )
 ) {
 const text = removeMentions(message.text);
 wit.message(text).then((data) => {
 const processed = processIntent(message, data);
 if (!processed) {
 postReply(message, TEXTS.DIDNT_UNDERSTAND);
 }
 pushConversation(message, data);
 })
 }
 });
  • 44. function processIntent(message, data) {
 const intents = data.entities;
 logIntents(message, data);
 if (intents.greetings) {
 const user = getUser(message);
 const greetingMessages = getMessagesWithIntents(message, 'greetings');
 const text = !greetingMessages.length
 ? TEXTS.FIRST_GREETING(user)
 : TEXTS.RANDOM_GREETING(user);
 postReply(message, text);
 return true;
 }
 ...
 }
  • 45. function processIntent(message, data) { ...
 
 if (intents.know_about && intents.pokemons) {
 postReply(message, TEXTS.POKEMONS_INFO);
 return true;
 }
 
 if (intents.type && intents.type_name && (intents.know_about || intents.question)) {
 const typeName = intents.type_name[0].value.toLowerCase();
 pokedex.getTypeByName(typeName)
 .then((response) => {
 postReply(message, TEXTS.TYPE(response));
 }).catch((error) => {
 if (error.statusCode === 404) {
 postReply(message, TEXTS.TYPE_NOT_FOUND);
 }
 });
 return true;
 } ...
 }
  • 46. function processIntent(message, data) { ...
 if (intents.answer) {
 const lastMessage = getLastMessage(message);
 if (lastMessage && lastMessage.wit.entities.thanks) {
 const answer = intents.answer[0].value;
 const text = answer.toLowerCase() === 'yes' ? TEXTS.OK_HIT_ME : TEXTS.ALRIGHT;
 postReply(message, text);
 return true;
 }
 }
 }
  • 52. Issues • Wit.ai works, but needs some learning • Wit.ai works, but pay attention to ‘BETA’ marks • Save history - needed for yes-no answers
  • 55. … and more • Tools • https://botmock.com • https://chatfuel.com • http://botanalytics.co • Bots: • https://www.messenger.com/t/mereminder • https://www.chatbots.org • https://tiv.ai/me