This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb.js
194 lines (168 loc) · 5.65 KB
/
fb.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var config = require('./app/config.js');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const express = require('express');
const fetch = require('node-fetch');
const request = require('request');
const winston = require('winston');
const Wit = require('node-wit').Wit;
const log = require('node-wit').log;
// Wit.ai parameters
const WIT_TOKEN = config.wit.serverAccessToken;
// Messenger API parameters
const FB_PAGE_TOKEN = config.messenger.pageAccessToken;
if (!FB_PAGE_TOKEN) { throw new Error('missing FB_PAGE_TOKEN') }
const FB_APP_SECRET = config.messenger.appSecret;
if (!FB_APP_SECRET) { throw new Error('missing FB_APP_SECRET') }
var FB_VERIFY_TOKEN = config.messenger.validationToken;
const fbMessage = (id, text) => {
const body = JSON.stringify({
recipient: { id },
message: { text },
});
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
return fetch('https://graph.facebook.com/me/messages?' + qs, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body,
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
};
const sessions = {};
const findOrCreateSession = (fbid) => {
var sessionId;
Object.keys(sessions).forEach(k => {
if (sessions[k].fbid === fbid) {
sessionId = k;
}
});
if (!sessionId) {
sessionId = new Date().toISOString();
sessions[sessionId] = {fbid: fbid, context: {}};
}
return sessionId;
};
const actions = {
send({sessionId}, {text}) {
const recipientId = sessions[sessionId].fbid;
if (recipientId) {
return fbMessage(recipientId, text)
.then(() => null)
.catch((err) => {
console.error(
'Oops! An error occurred while forwarding the response to',
recipientId,
':',
err.stack || err
);
});
} else {
console.error('Oops! Couldn\'t find user for session:', sessionId);
return Promise.resolve()
}
},
};
// Setting up our bot
const wit = new Wit({
accessToken: WIT_TOKEN,
actions: actions,
logger: new log.Logger(log.INFO)
});
// Starting our webserver and putting it all together
const router = express.Router();
router.use(({method, url}, rsp, next) => {
rsp.on('finish', () => {
console.log(`${rsp.statusCode} ${method} ${url}`);
});
next();
});
router.use(bodyParser.json({ verify: verifyRequestSignature }));
// Webhook setup
router.get('/webhook', (req, res) => {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === FB_VERIFY_TOKEN) {
res.send(req.query['hub.challenge']);
} else {
res.sendStatus(400);
}
});
router.post('/webhook', function (req, res) {
const data = req.body;
console.log(data);
if (data.object === 'page') {
data.entry.forEach(entry => {
entry.messaging.forEach(event => {
if (event.message) {
// Yay! We got a new message!
// We retrieve the Facebook user ID of the sender
const sender = event.sender.id;
// We retrieve the user's current session, or create one if it doesn't exist
// This is needed for our bot to figure out the conversation history
const sessionId = findOrCreateSession(sender);
// We retrieve the message content
const {text, attachments} = event.message;
// check if message is sent from the page
if (text.is_echo){
fbMessage(sender, "Please interact with me with your own accounts. :)");
}
if (attachments) {
// We received an attachment
// Let's reply with an automatic message
fbMessage(sender, 'Sorry, I can only understand text.')
.catch(console.error);
} else if (text) {
// We received a text message
// Let's forward the message to the Wit.ai Bot Engine
// This will run all actions until our bot has nothing left to do
wit.runActions(
sessionId, // the user's current session
text, // the user's message
sessions[sessionId].context // the user's current session state
).then((context) => {
// Our bot did everything it has to do.
// Now it's waiting for further messages to proceed.
console.log('Waiting for next user messages');
// log context to the logfile
// Based on the session state, you might want to reset the session.
// This depends heavily on the business logic of your bot.
// Example:
if (context['done']) {
delete sessions[sessionId];
}
// Updating the user's current session state
// sessions[sessionId].context = context;
})
.catch((err) => {
console.error('Oops! Got an error from Wit: ', err.stack || err);
})
}
} else {
console.log('received event', JSON.stringify(event));
}
});
});
}
});
function verifyRequestSignature(req, res, buf) {
var signature = req.headers["x-hub-signature"];
if (!signature) {
console.error("Couldn't validate the signature.");
} else {
var elements = signature.split('=');
var method = elements[0];
var signatureHash = elements[1];
var expectedHash = crypto.createHmac('sha1', FB_APP_SECRET)
.update(buf)
.digest('hex');
if (signatureHash != expectedHash) {
throw new Error("Couldn't validate the request signature.");
}
}
}
module.exports = router;