-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
272 lines (252 loc) · 11.4 KB
/
routes.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"use strict";
const {FACEBOOK_PAGE_TOKEN, FACEBOOK_PAGE_ID, FACEBOOK_VERIFY_TOKEN} = require("system").env;
const log = require("ringo/logging").getLogger(module.id);
const response = require("ringo/jsgi/response");
const strings = require("ringo/utils/strings");
const dates = require("ringo/utils/dates");
const numbers = require("ringo/utils/numbers");
const {Path} = require("fs");
const {FBMessenger, MultipartStream} = require("../../lib/fbmessenger");
const fbmUtils = require("../../lib/utils");
const {Application} = require("stick");
const app = exports.app = new Application();
app.configure("params", "route");
const bot = new FBMessenger(FACEBOOK_PAGE_TOKEN, false);
if (FACEBOOK_PAGE_TOKEN == null || FACEBOOK_PAGE_ID == null || FACEBOOK_VERIFY_TOKEN == null) {
log.error("ENV: " + require("system").env);
throw new Error("Facebook bot config missing! Please set FACEBOOK_PAGE_TOKEN, FACEBOOK_PAGE_ID, and " +
"FACEBOOK_VERIFY_TOKEN to run this test bot.");
}
app.get("/", function(req) {
return response.html("<h1>Hello World!</h1>");
});
app.get("/favicon.ico", function(req) {
return response.redirect("http://orf.at/favicon.ico");
});
// Facebook Messenger callback verification endpoint
log.info("Binding GET /birdie-webhook-callback for messenger ...");
app.get("/birdie-webhook-callback", function(req) {
if (req.params["hub.verify_token"] === FACEBOOK_VERIFY_TOKEN) {
log.info("Successful challenge!");
return response.text(req.params["hub.challenge"]);
}
log.error("Failed validation. Make sure the validation tokens match.");
return response.setStatus(403).text("Failed validation. Make sure the validation tokens match.");
});
// Processing the requests
log.info("Binding POST /birdie-webhook-callback for messenger ...");
app.post("/birdie-webhook-callback", function(req) {
if (req.postParams !== null && fbmUtils.isMessagingCallback(req.postParams)) {
try {
processMessages(fbmUtils.getMessagingForPage(req.postParams, FACEBOOK_PAGE_ID).filter(fbmUtils.isMessage));
processPostbacks(fbmUtils.getMessagingForPage(req.postParams, FACEBOOK_PAGE_ID).filter(fbmUtils.isPostback));
} catch (e) {
log.error("Processing error!", e);
}
} else {
log.error("Invalid request", req.toSource());
}
return response.json({
"timestamp": Date.now()
}).ok();
});
const processMessages = function(messages) {
messages.forEach(function(message) {
processText((message.message.text || "").toLocaleLowerCase(), message);
});
};
const processPostbacks = function(postbacks) {
postbacks.forEach(function(postbackMessage) {
processText((postbackMessage.postback.payload || "").toLocaleLowerCase(), postbackMessage);
});
};
const processText = function(text, message) {
if (strings.contains(text, "photo") || strings.contains(text, "image")) {
bot.sendImageAttachment(message.sender.id, new MultipartStream("photo.jpg", new Path(module.resolve("./fixtures/photo.jpg"))));
} else if (strings.contains(text, "video")) {
bot.sendVideoAttachment(message.sender.id, new MultipartStream("video.mp4", new Path(module.resolve("./fixtures/video.mp4"))));
} else if (strings.contains(text, "audio")) {
bot.sendAudioAttachment(message.sender.id, new MultipartStream("audio.mp3", new Path(module.resolve("./fixtures/audio.mp3"))));
} else if (strings.contains(text, "file") || strings.contains(text, "document")) {
bot.sendFileAttachment(message.sender.id, new MultipartStream("document.pdf", new Path(module.resolve("./fixtures/document.pdf"))));
} else if (strings.contains(text, "generic")) {
bot.sendGenericTemplate(message.sender.id, [
{
"title":"„Pokemon Go“: Hype sprengt alle Erwartungen",
"image_url":"http://orf.at/static/images/site/news/20160728/pokemon_go_datenschutz_pure_r.4699402.jpg",
"subtitle":"Pikachu und Co. erobern erneut die Welt.",
"buttons":[
{
"type":"web_url",
"url":"http://orf.at/stories/2349101/",
"title":"Story lesen \uD83D\uDC53"
},
{
"type":"postback",
"title":"Mehr zu Pokemon Go",
"payload": "news:stories:pokemongo"
}
]
},
{
"title":"Alles steht Kopf: Psychologischer Beistand für Kinobesuch",
"image_url":"http://orf.at/static/images/site/news/20150939/alles_steht_kopf_pixar_film_pure_n.4644298.jpg",
"subtitle":"„Alles steht Kopf“ spielt im Hirn einer Elfjährigen, bei der nicht nur die Außenwelt aus den Fugen gerät.",
"buttons":[
{
"type":"web_url",
"url":"http://orf.at/stories/2300732/",
"title":"Story lesen \uD83D\uDC53"
},
{
"type":"postback",
"title":"Mehr über Pixar",
"payload": "news:stories:pixar"
}
]
}
]);
} else if (strings.contains(text, "button")) {
bot.sendButtonTemplate(message.sender.id, "\uD83D\uDC8C Who you gonna vote for?", [
{
"type": "postback",
"title": "Donald Trump",
"payload": "vote:trump"
},
{
"type": "postback",
"title": "Hillary Clinton",
"payload": "vote:clinton"
}
]);
bot.sendButtonTemplate(message.sender.id, "Need a link to share?", [
{
"type": "web_url",
"title": "dev.ORF.at",
"url": "http://dev.orf.at"
}
], true);
bot.sendButtonTemplate(message.sender.id, "Need a link not to share?", [
{
"type": "web_url",
"title": "dev.ORF.at",
"url": "http://dev.orf.at"
}
], false);
} else if (strings.contains(text, "advanced")) {
bot.sendAdvancedTemplate(message.sender.id, {
"template_type": "airline_checkin",
"intro_message": "Check-in is available now!",
"locale": "en_US",
"pnr_number": "XYZDD",
"flight_info": [
{
"flight_number": "RG1234",
"departure_airport": {
"airport_code": "VIE",
"city": "Vienna International Airport",
"terminal": "T3",
"gate": "F16"
},
"arrival_airport": {
"airport_code": "TSR",
"city": "Timișoara",
"terminal": "T1",
"gate": "G1"
},
"flight_schedule": {
"boarding_time": "2016-12-24T10:00",
"departure_time": "2016-12-24T11:00",
"arrival_time": "2016-12-24T12:10"
}
}
],
"checkin_url": "https:\/\/www.orf.at"
});
} else if (strings.contains(text, "rickrolling")) {
bot.sendAdvancedTemplate(message.sender.id, {
"template_type": "open_graph",
"elements":[
{
"url": "https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb",
"buttons": [
{
"type": "web_url",
"url": "https://en.wikipedia.org/wiki/Rickrolling",
"title": "View More"
}
]
}
]
});
} else if (strings.contains(text, "quick")) {
if (strings.contains(text, "gif")) {
bot.sendQuickReplies(message.sender.id, new MultipartStream("animated.gif", new Path(module.resolve("./fixtures/animated.gif"))), [
{
"content_type": "text",
"title": "WTF?!",
"payload": "quick:wtf:" + Date.now()
},
{
"content_type": "text",
"title": "Cool!",
"payload": "quick:cool:" + Date.now()
}
]);
} else {
bot.sendQuickReplies(message.sender.id, "1, 2, oder 3?", [
{
"content_type": "text",
"title": "1!",
"payload": "quick:1:" + Date.now()
},
{
"content_type": "text",
"title": "2!",
"payload": "quick:2:" + Date.now()
},
{
"content_type": "text",
"title": "3!",
"payload": "quick:3:" + Date.now()
}
]);
}
} else if (strings.contains(text, "insights")) {
const duatc = bot.getDailyUniqueActiveThreads();
const duc = bot.getDailyUniqueConversations();
if (duatc.data.length > 0) {
bot.sendTextMessage(message.sender.id, duatc.data[0].description);
bot.sendTextMessage(message.sender.id, duatc.data[0].values.map(function(day) {
return dates.format(dates.parse(day.end_time), "yyyy-MM-dd") + " => " +
numbers.format(day.value, "#,###,##0", java.util.Locale.ENGLISH);
}));
} else {
bot.sendTextMessage(message.sender.id, "No daily unique active thread count available, sorry \uD83D\uDE2A");
}
if (duc.data.length > 0) {
bot.sendTextMessage(message.sender.id, duc.data[0].description);
bot.sendTextMessage(message.sender.id, duc.data[0].values.map(function(day) {
return dates.format(dates.parse(day.end_time), "yyyy-MM-dd") + " => " +
"TURN_ON: " + numbers.format(day.value.TURN_ON, "#,###,##0", java.util.Locale.ENGLISH) + "\n" +
"TURN_OFF: " + numbers.format(day.value.TURN_OFF, "#,###,##0", java.util.Locale.ENGLISH) + "\n" +
"DELETE: " + numbers.format(day.value.DELETE, "#,###,##0", java.util.Locale.ENGLISH) + "\n" +
"REPORT_SPAM: " + numbers.format(day.value.REPORT_SPAM, "#,###,##0", java.util.Locale.ENGLISH) + "\n" +
"OTHER: " + numbers.format(day.value.OTHER, "#,###,##0", java.util.Locale.ENGLISH);
}));
} else {
bot.sendTextMessage(message.sender.id, "No daily unique conversation count available, sorry \uD83D\uDE2A");
}
} else if (strings.contains(text, "code")) {
const url = bot.getMessengerCode();
if (url !== null) {
bot.sendImageAttachment(message.sender.id, url);
} else {
bot.sendTextMessage(message.sender.id, "Could not generate the code, sorry \uD83D\uDE2A");
}
} else if (strings.contains(text, "get_started")) {
bot.sendTextMessage(message.sender.id, "Welcome to the birdie test bot! \uD83D\uDC26");
} else {
bot.sendTextMessage(message.sender.id, "Heyo, current time is: " + new Date());
}
};