This repository was archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_test.js
More file actions
257 lines (214 loc) · 8.04 KB
/
bot_test.js
File metadata and controls
257 lines (214 loc) · 8.04 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
// This is main file containing code implementing the Express server and functionality for the Express echo bot.
//
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const path = require('path');
const fs = require('fs');
const token = "EAAWhdVgZA0nwBALALwNXQTk6vKsWsHJmH5PR9JjS7yuN4oWbYSE2auLJisSZBWELP9G7ZAUWk70XPPywyOqZCj2KMOiTOZCDNAjKZBEV01EpUc2joHVPMREmAUqPrQ4TFwyIleQmSZCov7kJsXYTlqJuUJUQDuWut3vIiW2fkMloAZDZD"
var messengerButton = "<html><head><title>Facebook Messenger Bot</title></head><body><h1>Facebook Messenger Bot</h1>This is a bot based on Messenger Platform QuickStart. For more details, see their <a href=\"https://developers.facebook.com/docs/messenger-platform/guides/quick-start\">docs</a>.<script src=\"https://button.glitch.me/button.js\" data-style=\"glitch\"></script><div class=\"glitchButton\" style=\"position:fixed;top:20px;right:20px;\"></div></body></html>";
// The rest of the code implements the routes for our Express server.
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Webhook validation
app.get('/webhook', function (req, res) {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === "SVKGPaHrdAO4u1TLB27V2pF8mEiUkp5HTFNLedmVVvVunIpy61OQekzu0iUhbUnw") {
console.log("Validating webhook");
res.status(200).send(req.query['hub.challenge']);
} else {
console.error("Failed validation. Make sure the validation tokens match.");
res.sendStatus(403);
}
});
// Display the web page
app.get('/', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(messengerButton);
res.end();
});
// Message processing
app.post('/webhook', function (req, res) {
console.log(req.body);
var data = req.body;
// Make sure this is a page subscription
if (data.object === 'page') {
// Iterate over each entry - there may be multiple if batched
data.entry.forEach(function (entry) {
var pageID = entry.id;
var timeOfEvent = entry.time;
// Iterate over each messaging event
entry.messaging.forEach(function (event) {
if (event.message) {
receivedMessage(event);
} else if (event.postback) {
receivedPostback(event);
} else {
console.log("Webhook received unknown event: ", event);
}
});
});
res.sendStatus(200);
}
});
// Incoming events handling
function receivedMessage(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfMessage = event.timestamp;
var message = event.message;
console.log("Received message for user %d and page %d at %d with message:",
senderID, recipientID, timeOfMessage);
console.log(JSON.stringify(message));
var messageId = message.mid;
var messageText = message.text;
var messageAttachments = message.attachments;
if (messageText) {
addMessageToBack({
recipient: {
id: recipientID
},
message: {
text: messageText
}
});
// If we receive a text message, check to see if it matches a keyword
// and send back the template example. Otherwise, just echo the text we received.
switch (messageText) {
case 'chat':
sendImageMessage(senderID, messageText);
sendTextMessage(senderID, "N'est-il pas magnifique ?");
break;
case 'salut':
sendTextMessage(senderID, messageText + " comment tu vas ?");
break;
case 'merci':
sendTextMessage(senderID, " je t'en prie.");
break;
case 'bien et toi ?':
sendTextMessage(senderID, "ça va ! je suis de bonne humeur aujourd'hui car j'ai vu un chat. Écrit ce mot justement (chat)");
break;
default:
sendTextMessage(senderID, recoveryFile());
break;
}
} else if (messageAttachments) {
sendTextMessage(senderID, "Merci pour la pièce jointe");
}
}
/**
Fonction permettant de récupérer une ligne d'un texte défini
Cette fonction retournera une phrase aléatoire du fichier texte2.txt
**/
function recoveryFile() {
var lines = fs.readFileSync(path.resolve(__dirname) + "/text2.txt", "utf8").split("\n");
var index = Math.floor(Math.random() * (lines.length - 1));
return lines[index];
}
function sendImageMessage(sender, text) {
let data =
{
"attachment": {
"type": "image",
"payload": {
"url": "https://www.wikichat.fr/wp-content/uploads/sites/2/comment-soigner-une-plaie-dun-chat.jpg"
}
}
}
// let access_token = "mon token de page";
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: token},
method: 'POST',
json: {
recipient: {id: sender},
message: data,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending image messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
function receivedPostback(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfPostback = event.timestamp;
// The 'payload' param is a developer-defined field which is set in a postback
// button for Structured Messages.
var payload = event.postback.payload;
console.log("Received postback for user %d and page %d with payload '%s' " +
"at %d", senderID, recipientID, payload, timeOfPostback);
// When a postback is called, we'll send a message back to the sender to
// let them know it was successful
sendTextMessage(senderID, "Postback called");
}
//////////////////////////
// Sending helpers
//////////////////////////
function sendTextMessage(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: messageText
}
};
callSendAPI(messageData);
}
function sendGenericMessage(recipientId) {
}
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: token},
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
console.log("Successfully sent generic message with id %s to recipient %s",
messageId, recipientId);
addMessageToBack(messageData);
} else {
console.error("Unable to send message.");
console.error(response);
console.error(error);
}
});
}
function addMessageToBack(messageData) {
messageData = {
mid: messageData.recipient.id,
text: messageData.message.text
};
request({
uri: (process.env.BACK_URL || 'http://127.0.0.1:8080') + '/message/add',
qs: {access_token: token},
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
console.log("Successfully sent message to back.");
} else {
console.error("Unable to send message to back.");
console.error(response);
console.error(error);
}
});
}
// Set Express to listen out for HTTP requests
var server = app.listen(process.env.PORT || 3000, function () {
console.log("Listening on port %s", server.address().port);
});