UNPKG

6.92 kBMarkdownView Raw
1# Facebook Messenger Bot
2[![](https://travis-ci.org/bluejamesbond/FacebookMessengerBot.js.svg?branch=master)](https://travis-ci.org/bluejamesbond/FacebookMessengerBot.js)
3The purpose of this library is to offer a simple, light-weight Facebook Messenger Bot API for Node with ES6 support.
4Internally, it uses [Promises to ensure compatibility with `async/await`](https://github.com/bluejamesbond/FacebookMessengerBot.js/blob/master/.babelrc#L13).
5
6**Objective:** Given a set of inputs, the library automatically selects the optimal message format to display this data.
7
8<!-- START doctoc generated TOC please keep comment here to allow auto update -->
9<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
10
11- [Install](#install)
12- [Example](#example)
13- [Demo](#demo)
14- [API By Example](#api-by-example)
15 - [Attach Express Router for Verification and Receiving Messages](#attach-express-router-for-verification-and-receiving-messages)
16 - [Receive Messages](#receive-messages)
17 - [Send Responses](#send-responses)
18 - [Handle Postbacks](#handle-postbacks)
19 - [Delivery and Optin](#delivery-and-optin)
20 - [Pipe Messages into Bot (i.e. I don't use Express!)](#pipe-elements-into-bot-ie-i-dont-use-express)
21 - [Debugging](#debugging)
22 - [Extras: Fetch User](#extras-fetch-user)
23- [Next Release (very soon)](#next-release-very-soon)
24- [Maintainers](#maintainers)
25
26<!-- END doctoc generated TOC please keep comment here to allow auto update -->
27
28## Install
29```
30npm install facebook-messenger-bot --save -E
31```
32
33## Example
34```es6
35import {Bot, Elements} from 'facebook-messenger-bot';
36
37const bot = new Bot(myPageAccessToken, myVerification);
38
39bot.on('message', async message => {
40 const {sender} = message;
41 await sender.fetch('first_name');
42
43 const out = new Elements();
44 out.add({text: `hey ${sender.first_name}, how are you!`});
45
46 await bot.send(sender.id, out);
47});
48
49const app = express();
50app.use('/facebook', bot.router());
51app.listen(3000);
52```
53
54## Demo
55Coming soon
56
57## API By Example
58
59### Attach Express Router for Verification and Receiving Messages
60```es6
61import express from 'express';
62import {Bot} from 'facebook-messenger-bot'; // import Bot class
63
64const app = express();
65const bot = new Bot(myPageAccessToken, myVerification); // create bot instance
66
67app.use('/facebook', bot.router()); // use the router
68app.listen(3000);
69```
70
71### Receive Messages
72```es6
73import {Elements} from 'facebook-messenger-bot';
74
75bot.on('message', async message => {
76 const {sender} = message;
77
78 // get sender id
79 console.log(`Received a message from ${sender.id}`);
80
81 // fetch additional user properties
82 await sender.fetch(`first_name,last_name,profile_pic`, true); // true: use cache
83
84 console.log(`Fetched ${sender.first_name}, ${sender.last_name}, ${sender.profile_pic}`);
85
86 const {text, images, videos, location, audio} = message;
87
88 if (text) {
89 console.log(text); // 'hey'
90 }
91
92 if (images) {
93 console.log(images); // ['http://...', 'http://...']
94 }
95
96 if (videos) {
97 console.log(videos); // ['http://...', 'http://...']
98 }
99
100 if (location) {
101 console.log(location); // {title, long, lat, url}
102 }
103
104 if (audio) {
105 console.log(audio); // url
106 }
107
108 console.log(message); // log the message to learn about all the attributes
109});
110```
111
112### Send Responses
113```es6
114import {Elements} from 'facebook-messenger-bot'; // import Bot class
115
116bot.on('message', async message => {
117 const {sender} = message;
118
119 let out, buttons;
120
121 // ---- send text
122 out = new Elements();
123 out.add({text: 'hey! what up'});
124 await bot.send(sender.id, out);
125
126 // wait for 1s
127 await bot.wait(1000);
128
129 // ---- send image
130 const out = new Elements();
131 out.add({image: 'https://developers.facebook.com/images/devsite/fb4d_logo-2x.png'});
132 await bot.send(sender.id, out);
133
134 await bot.wait(1000);
135
136 // ---- send buttons (single card)
137 buttons = new Buttons();
138 buttons.add({text: 'Google', url: 'http://google.com'});
139 buttons.add({text: 'Yahoo', url: 'http://yahoo.com'});
140 buttons.add({text: 'Bing', url: 'http://bing.com'});
141 out = new Elements();
142 out.add({text: 'search engines', subtext: 'click to get redirected', buttons}); // add a card
143 await bot.send(to, out);
144
145 await bot.wait(2000);
146
147 // ---- send image + buttons (multiple cards)
148 buttons = new Buttons();
149 buttons.add({text: 'Google', url: 'http://google.com'});
150 buttons.add({text: 'Yahoo', url: 'http://yahoo.com'});
151 out = new Elements();
152 out.add({image: 'http://google.com/logo.png', text: 'hey', buttons}); // first card
153 out.add({image: 'http://yahoo.com/logo.png', text: 'hey', buttons}); // second card
154 await bot.send(to, out);
155});
156```
157### Handle Postbacks
158```es6
159bot.on('message', async message => {
160 const {sender} = message;
161
162 let out, buttons;
163
164 // ---- send buttons
165 buttons = new Buttons();
166 buttons.add({text: 'Google', data: 'google', event: 'search-engine'});
167 buttons.add({text: 'Bing', data: 'bing', event: 'search-engine'});
168 buttons.add({text: 'Yahoo', data: 'yahoo', event: 'search-engine'});
169 out = new Elements();
170 out.add({image: 'http://someimage.com', text: 'hey', buttons});
171 await bot.send(to, out);
172 await sleep(2000);
173});
174
175bot.on('search-engine', async (data, message) => {
176 console.log(data); // google, bing, or yahoo
177});
178
179// all postbacks are emitted via 'postback'
180bot.on('postback', async (event, message, data) => {
181 assert(data === message.data);
182 assert(event === message.event);
183
184 console.log(event, message, data);
185});
186
187// if the data cannot be parsed, an 'invalid-postback' is emitted
188bot.on('invalid-postback', async (message) => {
189 console.log(message);
190});
191```
192
193### Delivery and Optin
194```es6
195bot.on('optin', async (message, param) => {
196 assert(param === message.param);
197 assert(param === message.optin);
198
199 console.log(message, param);
200});
201
202bot.on('delivery', async (message, mids) => {
203 assert(mids === message.delivered);
204
205 console.log(message, mids);
206});
207```
208
209### Pipe Messages into Bot (i.e. I don't use Express!)
210```es6
211bot.handleMessage(req.body);
212```
213
214### Debugging
215```es6
216buttons = new Buttons();
217out = new Elements();
218
219...
220
221// you can compare these output with the ones provided on the Facebook website
222console.log(buttons.toJSON());
223console.log(out.toJSON());
224
225// access raw parsed object via 'message' event
226bot.on('message', message => {
227 console.log(message.raw);
228});
229```
230
231### Extras: Fetch User
232```es6
233const user = await bot.fetchUser(id, 'first_name,last_name', true); // true for cache
234```
235
236## Next Release (very soon)
237- Create receipt messages
238
239## Maintainers
240Looking for additional maintainers this repo. Let me know if you are interested.