Building chat bots with node.js

I'm Slobodan Stojanovic

CTO of Cloud Horizon &

JS Belgrade meetup organizer

Chatterbot / chat bot

a type of conversational agent, a computer program designed to simulate an intelligent conversation with one or more human users via auditory or textual methods.

A bit of a history

1950: Turing Test

1966: Eliza

1972: Parry

1983: Racter

1995: Alice

2005: Jabberwacky

2011: Hubot

2014: Slack bot

2015: Telegram bots

2015: FB Messenger bots

2016: Skype, Apple...

How to build

a chat bot

What do you need to know to build

a chat bot?

Natural language processing?

Artificial intelligence?

Programming?

How the most popular platforms work

Let's see

Slack

Webhooks

Slash commands

Bot users

- Text answers

- Attachments

- Buttons

- Delayed and multiple responses

API/Webhook:

- Webhooks

- Slash commands

Web Sockets (Real-time):

- Bot users

Telegram

Bots

Inline bots

- Text answers

- Attachments

- Custom keyboards

API/Webhook:

- Bots

- Inline bots

 

No real-time API

FB Messenger

Bots

- Text answers

- Attachments
- Structured answers (buttons, bubbles, receipts...)

API/Webhook:

- Bots

No real-time API

Let's build a
simple bot for
FB Messenger!

We'll use node.js

- easy to build an API for webhooks

- works nice for real-time too

FB has a sample bot code in node.js.

Let's see it!
 

https://github.com/fbsamples/messenger-platform-samples

- Simple express.js app

- 3 API endpoints for
webhooks

- Answers sent as HTML requests with 'request'

And 839 lines of boilerplate code to cover everything you need

I hate boilerplate code...

What if I tell you that you need just 5 lines of JS code to build a chat bot?

var botBuilder = require('claudia-bot-builder');

module.exports = botBuilder(function (request) {
  return 'Thanks for sending ' + request.text;
});
const botBuilder = require('claudia-bot-builder');

module.exports = botBuilder(request =>
    `Thanks for sending ${request.text}`);

Or even less with ES6 :)

Meet the
Claudia Bot Builder

 


https://github.com/claudiajs/claudia-bot-builder

Build chat bots for

FB, Slack, Telegram and Skype

and deploy them on

AWS Lambda

in just a few minutes

Why AWS Lambda?

- Perfect for web hooks and chat bots

- Cheap - free for 1 million requests per month, less then $0.2 for next million

- Works with node.js v4.3.2

Let's build
Space Explorer Bot


source:
https://github.com/stojanovic/space-explorer-bot

Simple chat bot that connects to the NASA API, pulls some data and shows it to the user in the chat 

First we need bot.js file with just a simple text:

const botBuilder = require('claudia-bot-builder');

module.exports = botBuilder(request =>
    'Hello from space explorer bot! ' +
    'Your request was: ' +
    ${request.text});

Then let's add some dependencies we need. Run following 3 commands from terminal:

npm init

npm install claudia-bot-builder -S

npm install claudia -g

Before we continue you need to create a new FB page for the bot and an app with Messenger integration, as explained in the Facebook Messenger Getting Started Guide

And finally run a following command from the terminal:

claudia create --region us-east-1 \
    --api-module bot --configure-fb-bot

Then just copy webhook url and verification token that command prints for you to FB App and subscribe the app to the page you created.

That's it!
You created your first chat bot for FB Messenger.

Before we dive deep in the code make sure you have NASA API key from:

https://api.nasa.gov/

And add that key as a stage variable in API Gateway.
You can do it from UI or with this command:

aws apigateway create-deployment \
  --rest-api-id API_ID --stage-name latest \
  --variables nasaApiKey=YOUR_NASA_API_KEY

Where API_ID is your API id from auto-generated claudia.json file.

And this is the final result:

https://vimeo.com/172001135

Or check it out live on FB:
m.me/space-explorer-bot

Is there anything else you can do with Claudia Bot Builder?

Yes, of course!

Receive and send messages for:

- FB Messenger

- Telegram

- Slack (slash commands only at the moment)

- Skype

Easily create template messages for FB including buttons, attachments and receipts

Easily create message attachments for Slack including buttons, images, etc.

Send delayed messages in Slack

And let us know if you build something cool using Claudia Bot Builder!

That's it!

Q & A time!

Claudia Bot Builder is here:
github.com/claudiajs/claudia-bot-builder

And you can follow me on:

github.com/stojanovic

twitter.com/slobodan_

Building chat bots with node.js

By Slobodan Stojanovic

Building chat bots with node.js

An introduction to chat bots development using node.js and Claudia Bot Builder, simple library that helps you to build chat bots for various platforms and deploy them to AWS Lambda.

  • 4,096