未加星标

How To Develop A Chat Bot With Node.js

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
How To Develop A Chat Bot With Node.js BySlobodan Stojanovi October 17th, 2016 Node.js UI

In the past few months, chat bots have become very popular, thanks to Slack, Telegram and Facebook Messenger. But the chat bot idea is not new at all.

A chat bot interface is mentioned in the famous Turing test in 1950. Then there was Eliza in 1966, a simulation of a Rogerian psychotherapist and an early example of primitive natural language processing. After that came Parry in 1972, a simulation of a person with paranoid schizophrenia (and, yes, of course, Parry met Eliza ).

In 1983, there was a book named The Policeman’s Beard Is Half Constructed , which was generated by Racter, an artificial intelligence computer program that generated random English-language prose, later released as a chat bot.

One of the most famous was Alice ( a rtificial l inguistic I nternet c omputer e ntity), released in 1995. It wasn’t able to pass the Turing test, but it won the Loebner Prize three times. In 2005 and 2006, the same prize was won by two Jabberwacky bot characters.

And in 2014, Slackbot made chat bots popular again. In 2015, Telegram and then Facebook Messenger released chat bot support; then, in 2016 Skype did the same, and Apple and some other companies announced even more chat bot platforms.


How To Develop A Chat Bot With Node.js
(Image: The Stocks ) (Large preview) What Do You Need To Know To Build A Chat Bot?

The answer to that mostly depends on what you want to build, of course.

In most cases, you can build a chat bot without knowing much about artificial intelligence (AI), either by avoiding it completely or by using some existing libraries for basic AI.

The same goes for natural language processing (NLP); it’s more important than AI, but you can build a chat bot using an NLP library or, for some platforms, simply by using buttons and UI elements instead of word processing.

And finally, do you even need to know programming? There are a lot of visual bot builders, so probably not. But it can be useful.

How To Build A Facebook Messenger Bot

This is an article about building chat bots, so let’s finally dive deep into it. Let’s build a simple Facebook Messenger bot.

We’ll use Node.js, but you can build a chat bot with any programming language that allows you to create a web API.

Why Node.js? Because it’s perfect for chat bots: You can build a simple API quickly with hapi.js, Express, etc.; it supports real-time messages (RTM) for Slack RTM bots; and it’s easy to learn (at least easy enough to build a simple chat bot).

Facebook already has a sample chat bot written in Node.js, available on GitHub . If you check the code, you’ll see that it uses the Express framework and that it has three webhooks (for verification, authentication and receiving messages). You’ll also see that it sends responses with Node.js’ Request module.

Sounds simple?

It is. But this complete sample bot has 839 lines of code. It’s not much and you probably need just half of that, but it’s still too much boilerplate code to start with.

What if I told you that we could have the same result with just five lines of javascript?

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

Or even fewer if you use ECMAScript 6:

const botBuilder = require('claudia-bot-builder');
module.exports = botBuilder(request => `Thanks for sending ${request.text}`); Meet The Claudia Bot Builder

The Claudia Bot Builder helps developers create chat bots for Facebook Messenger, Telegram, Skype and Slack, and deploy them to Amazon Web Services’ (AWS) Lambda and API Gateway in minutes.

The key idea behind the project is to remove all of the boilerplate code and common infrastructure tasks, so that you can focus on writing the really important part of the bot ― your business workflow. Everything else is handled by the Claudia Bot Builder.

Why AWS Lambda? It’s a perfect match for chat bots: Creating a simple API is easy; it responds much faster to the first request than a free Heroku instance; and it’s really cheap. The first million requests each month are free, and the next million requests are just $0.20!

Here’s how easy it is to build a Facebook Messenger bot with Claudia Bot Builder:

Let’s Build A Space Explorer Bot

Space Explorer is a simple Messenger chat bot that uses NASA’s API to get data and images about space.

Before we begin, create a Facebook page and app, and add Messenger integration, as described in Facebook’s “ Getting Started ” guide.

Then, create a file named bot.js with the following content:

const botBuilder = require('claudia-bot-builder');
module.exports = botBuilder(request => `Hello from space explorer bot! Your request was: ${request.text}`);

Install these dependencies:

npm init;
npm install claudia-bot-builder -S;
npm install claudia -g;

Create a Lambda function and follow the instructions in the video above to connect it with your Facebook app:

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

That’s it! You’ve created your first chat bot for Facebook Messenger.

If you send a message to your page, your bot will reply. But the answer is too simple. Let’s add something more interesting!

Integrate NASA’s API

Before we continue, visit NASA’s API portal and get an API key.

Then, add your API key as a nasaApiKey stage variable in API Gateway. You can do that from the UI or by running the following command:

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

Here, API_ID is your API ID from the claudia.json file that was auto-generated in the previous step.

Let’s add a better answer to the text messages. Claudia Bot Builder has a simple builder for Facebook Messenger template messages (the documentation is on GitHub ).

const botBuilder = require('claudia-bot-builder');
const fbTemplate = botBuilder.fbTemplate;
const rp = require('minimal-request-promise');
module.exports = botBuilder((request, originalApiRequest) => {
// If r

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:How To Develop A Chat Bot With Node.js
本站链接:http://www.codesec.net/view/483833.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(28)