Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bots: Basics @ Javascript Tonight, 2017.09.22

Bots: Basics @ Javascript Tonight, 2017.09.22

Talk about creating simple chatbots using Javascript, Telegram or Slack API and wit.ai. Javascript Tonight Meetup, 2017.

Avatar for Andrei Sierkov

Andrei Sierkov

September 22, 2017

More Decks by Andrei Sierkov

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. Creating bot • Register bot via BotFather • Initialise Telegram

    and PokeAPI • Handle message • Define replies
  4. Creating bot • Register bot via BotFather • Initialise Telegram

    and PokeAPI • Handle message • Define replies
  5. 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();
  6. Creating bot • Register bot via BotFather • Initialise Telegram

    and PokeAPI • Handle message • Define replies
  7. 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);
  8. Creating bot • Register bot via BotFather • Initialise Telegram

    and PokeAPI • Handle message • Define replies
  9. 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) }
 ];
 });
 }, ... };
  10. 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
  11. wit.ai • Natural Language Interface • Is a part of

    Facebook • Easy to start • Alternative - api.ai
  12. 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();
  13. 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);
 })
 }
 });
  14. 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;
 }
 ...
 }
  15. 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;
 } ...
 }
  16. 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;
 }
 }
 }
  17. Issues • Wit.ai works, but needs some learning • Wit.ai

    works, but pay attention to ‘BETA’ marks • Save history - needed for yes-no answers
  18. … 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