-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
165 lines (128 loc) · 4.89 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
require('dotenv').config();
const gd = require('./helper-functions/google-dialogflow');
const ad = require('./helper-functions/airtable-database');
const fm = require('./helper-functions/facebook-messenger');
const fs = require('fs');
const uuid = require('uuid');
let sessionId = uuid.v4();
fs.writeFileSync('key.text', sessionId);
const webApp = express();
webApp.use(bodyParser.urlencoded({
extended: true
}))
webApp.use(bodyParser.json());
const PORT = process.env.PORT;
const TOKEN = process.env.TOKEN;
// This get method is to check the app is working
webApp.get('/', (req, res) => {
res.status(200).send(`Hello World.!`);
});
// This method is to verify the Facebook webhook
webApp.get('/webhook', (req, res) => {
let mode = req['query']['hub.mode'];
let token = req['query']['hub.verify_token'];
let challenge = req['query']['hub.challenge'];
if (mode && token) {
if (mode === 'subscribe' && token === TOKEN) {
console.log('Webhook verified by Facebook.')
res.status(200).send(challenge);
} else {
res.status(403).send('Forbidden');
}
}
});
// Format the output of Dialogflow
// and make it ready for Facebook Messenger
const formatMessage = (fulfillmentMessages) => {
if (fulfillmentMessages['message'] === 'text') {
let formatMessageData = {
'text': fulfillmentMessages['text']['text'][0]
}
return formatMessageData;
} else if (fulfillmentMessages['message'] === 'quickReplies') {
let quickReplies = fulfillmentMessages['quickReplies']['quickReplies'];
let text = fulfillmentMessages['quickReplies']['title'];
let quick_replies = [];
quickReplies.forEach(qr => {
let tempDict = {};
tempDict['content_type'] = 'text';
tempDict['title'] = qr;
tempDict['payload'] = '<POSTBACK_PAYLOAD>'
quick_replies.push(tempDict);
});
let formatMessageData = {
'text': text,
'quick_replies': quick_replies
}
return formatMessageData;
}
};
// Get senderID, text, and URL from the incoming message
// from Facebook Messenger
const getSenderIDText = (body) => {
let messageData = {};
let entry = body['entry'][0];
let messaging = entry['messaging'][0];
let senderID = messaging['sender']['id'];
messageData['senderID'] = senderID;
try {
messageData['GET_STARTED'] = messaging['postback']['title'];
} catch (error) {
messageData['GET_STARTED'] = 'NO-DATA';
}
try {
messageData['messageText'] = messaging['message']['text'];
} catch (error) {
messageData['messageText'] = 'NO-DATA';
}
try {
messageData['URL'] = messaging['message']['attachments'][0]['payload']['url'];
} catch (error) {
messageData['URL'] = 'NO-DATA';
}
return messageData;
};
// This method is called when someone
// sends message to the chatbot
webApp.post('/webhook', async (req, res) => {
let body = req['body'];
let messageData = getSenderIDText(body);
if (body['object'] === 'page' && messageData['GET_STARTED'] !== 'NO-DATA') {
let intentData = await gd.checkIntent('hello');
let formatMessageData = formatMessage(intentData['fulfillmentMessages']);
fm.sendMessage(messageData['senderID'], formatMessageData);
res.status(200).send('OK');
} else if ((body['object'] === 'page' && messageData['URL'] !== 'NO-DATA')) {
let intentData = await gd.checkIntent('image');
let formatMessageData = formatMessage(intentData['fulfillmentMessages']);
let bug = intentData['outputContexts'][0]['parameters']['fields']['any.original']['stringValue'];
let fields = {
'Notes': bug,
'Attachments': [{
'url': messageData['URL']
}]
}
let flag = await ad.insertData(fields);
if (flag == 1) {
fm.sendMessage(messageData['senderID'], formatMessageData);
res.status(200).send('OK');
} else {
fm.sendMessage(messageData['senderID'], 'Sorry, something went wrong, please try again.');
res.status(200).send('OK');
}
} else if ((body['object'] === 'page' && messageData['messageText'] !== 'NO-DATA')) {
let intentData = await gd.checkIntent(messageData['messageText']);
let formatMessageData = formatMessage(intentData['fulfillmentMessages']);
fm.sendMessage(messageData['senderID'], formatMessageData);
res.status(200).send('OK');
}
else {
fm.sendMessage(senderIDText['senderID'], 'I encountered a glitch.');
res.status(200).send('OK');
}
});
webApp.listen(PORT, () => {
console.log(`Server is running at ${PORT}`);
});