/
facebook-example-botkit.js
50 lines (37 loc) · 1.53 KB
/
facebook-example-botkit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* Copyright (c) 2016-2019 Dashbot Inc All rights reserved */
'use strict';
if (!process.env.DASHBOT_API_KEY_FACEBOOK) {
throw new Error('"DASHBOT_API_KEY_FACEBOOK" environment variable must be defined');
}
if (!process.env.FACEBOOK_VERIFY_TOKEN) {
throw new Error('"FACEBOOK_VERIFY_TOKEN" environment variable must be defined');
}
if (!process.env.FACEBOOK_PAGE_TOKEN) {
throw new Error('"FACEBOOK_PAGE_TOKEN" environment variable must be defined');
}
var dashbot = require('../src/dashbot')(process.env.DASHBOT_API_KEY_FACEBOOK,
{urlRoot: process.env.DASHBOT_URL_ROOT, debug:true}).facebook;
var port = 4000;
var webHookPath = '/facebook/receive/';
var Botkit = require('botkit');
var controller = Botkit.facebookbot({
access_token: process.env.FACEBOOK_PAGE_TOKEN,
verify_token: process.env.FACEBOOK_VERIFY_TOKEN
});
var bot = controller.spawn({
});
controller.middleware.receive.use(dashbot.receive);
controller.middleware.send.use(dashbot.send);
// if you are already using Express, you can use your own server instance...
// see "Use BotKit with an Express web server"
controller.setupWebserver(port,function(err,webserver) {
controller.createWebhookEndpoints(controller.webserver, bot, function() {
console.log('Facebook webhook available at http://localhost:' + port + webHookPath);
});
});
controller.on('facebook_optin', function(bot, message) {
bot.reply(message, 'Welcome to my app!');
});
controller.on('message_received', function(bot, message) {
bot.reply(message, 'You are right when you say '+message.text);
});