This repository has been archived by the owner on Feb 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
277 lines (232 loc) · 7.81 KB
/
server.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
273
274
275
276
277
/**
* Messenger Translator
* Copyright (C) 2020 - 2022, Adriane Justine Tan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const express = require('express')
const basicAuth = require('express-basic-auth')
const localeStrings = require('./src/locale/')
const hash = require('./src/utils/hash.js')
const logger = require('./src/utils/log.js')
const send = require('./src/utils/send.js')
const database = require('./src/database.js')
const users = require('./src/users.js')
const feedbacks = require('./src/feedbacks.js')
const profile = require('./src/profile.js')
const translate = require('./src/translate.js')
const ACCESS_TOKEN = process.env.ACCESS_TOKEN
const APP_SECRET = process.env.APP_SECRET
const VALIDATION_TOKEN = process.env.VALIDATION_TOKEN
const USERNAME = process.env.USERNAME
const PASSWORD = process.env.PASSWORD
const PORT = process.env.PORT || 8080
const DEBUG = process.env.DEBUG
if (!ACCESS_TOKEN || !VALIDATION_TOKEN || !APP_SECRET) {
throw new Error('Access, App Secret and/or validation token is not defined')
}
const authUsers = {}
authUsers[USERNAME] = PASSWORD
const app = express()
const auth = basicAuth({ users: authUsers, challenge: true })
app.use('/logs', express.static(logger.directory))
app.use((req, res, next) => {
res.set('Content-Type', 'text/plain')
res.set('Content-Language', 'en')
next()
})
app.use('/webhook', express.json({
verify: (req, res, buf) => {
const signature = req.get('x-hub-signature')
if (!signature) throw new Error('No signature')
const elements = signature.split('=')
const algo = elements[0]
const defined = elements[1]
const expected = hash(algo, buf, APP_SECRET)
if (defined !== expected) {
logger.write('Invalid signature', 1)
logger.write(`Signature: ${signature}`, 1)
logger.write('Body:', 1)
logger.write(req.body, 1)
res.status(403).send('Invalid signature')
throw new Error('Invalid signature')
}
}
}))
app.get('/webhook', (req, res) => {
const mode = req.query['hub.mode']
const verifyToken = req.query['hub.verify_token']
const challenge = req.query['hub.challenge']
if (mode === 'subscribe' && verifyToken === VALIDATION_TOKEN) {
res.status(200).send(challenge)
} else {
logger.write('Mode/verification token doesn\'t match', 1)
logger.write('Parameters:', 1)
logger.write({ mode, verifyToken, challenge }, 1)
res.status(403).send('Mode/verification token doesn\'t match')
}
})
app.post('/webhook', (req, res) => {
const data = req.body
if (data.object !== 'page') {
logger.write('Object is not a page', 1)
logger.write('Data:', 1)
logger.write(data, 1)
res.status(403).send('An error has occurred')
return
}
data.entry.forEach(entry => {
entry.messaging.forEach(event => {
if (DEBUG) {
console.log('A new event was received: ')
console.log(event)
}
if (event.message) {
receivedMessage(event)
res.status(200).send('Success')
} else if (event.postback) {
receivedPostback(event)
res.status(200).send('Success')
} else {
logger.write('Unknown/unsupported event', 1)
logger.write('Event:', 1)
logger.write(event, 1)
res.status(400).send('Unknown/unsupported event')
}
})
})
})
app.get('/feedbacks', auth, (req, res) => {
feedbacks.getFeedbacks().then(feedbacks => {
res.set('Content-Type', 'application/json')
res.status(200).json(feedbacks)
})
})
/**
* Handles received postback events.
*
* @param {object} event Event object sent by Facebook
* @return void
*/
async function receivedPostback (event) {
const senderID = event.sender.id
const postback = event.postback
const payload = postback.payload
if (DEBUG) console.log(`Postback was called with payload: ${payload}`)
let user = await users.getUser(senderID)
if (user === null) {
const fbProfile = await profile.getProfile(senderID)
user = await users.addUser(senderID, fbProfile)
}
await send(user.psid, null, 'mark_seen')
await send(user.psid, null, 'typing_on')
if (DEBUG) {
console.log('User Data: ')
console.log(user)
}
switch (payload) {
case 'get_started':
case 'get_help': {
const helpMessage = localeStrings(user.locale, 'help')
await send(user.psid, helpMessage)
break
}
case 'change_language': {
const language = postback.title.split('--language ')[1]
const response = await profile.changeLanguage(user, language)
await send(user.psid, response)
break
}
default:
logger.write('Unknown/unsupported payload', 1)
logger.write('Event:', 1)
logger.write(event, 1)
}
await send(user.psid, null, 'typing_off')
}
/**
* Handles all messages received.
*
* @param {object} event Event object sent by Facebook
* @return void
*/
async function receivedMessage (event) {
const senderID = event.sender.id
const message = event.message
const text = message.text
if (text && DEBUG) console.log(`Message was received with text: ${text}`)
let user = await users.getUser(senderID)
if (user === null) {
const fbProfile = await profile.getProfile(senderID)
user = await users.addUser(senderID, fbProfile)
}
await send(user.psid, null, 'mark_seen')
await send(user.psid, null, 'typing_on')
const langRegex = /^(-?-?lang(uage)? (.+))$/i
const feedback = /^(-?-?(feedback|fb) (.+))$/i
const messageOnly = /^(-?-?message-only)$/i
let response = ''
if (DEBUG) {
console.log('User Data: ')
console.log(user)
}
if (message.attachments) {
response = localeStrings(user.locale, 'unsupported_attachment')
} else if (text.match(/^(-?-?help)$/i) !== null) {
response = localeStrings(user.locale, 'help')
} else if (text.match(langRegex) !== null) {
const language = langRegex.exec(text)[3]
response = await profile.changeLanguage(user, language)
} else if (text.match(feedback) !== null) {
const message = feedback.exec(text)[3]
await feedbacks.logFeedback(user.psid, user.name, message)
response = localeStrings(user.locale, 'feedback_confirmation')
} else if (text.match(messageOnly)) {
user.message = user.message === 0 ? 1 : 0
await users.setUser(user)
const id = 'message_only_' + (user.message === 1 ? 'enabled' : 'disabled')
response = localeStrings(user.locale, id)
} else response = await translate(text, user)
const result = await send(user.psid, response)
if (!result) {
const longMessage = localeStrings(user.locale, 'long_message')
await send(user.psid, longMessage)
}
await send(user.psid, null, 'typing_off')
}
const server = app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`)
})
process.on('uncaughtException', error => {
logger.write('Uncaught Exception', 1)
logger.write(`Error: ${error.message}`, 1)
logger.write(error, 1)
})
process.on('unhandledRejection', error => {
logger.write('Unhandled Promise rejection', 1)
logger.write('Error:', 1)
logger.write(error, 1)
})
process.on('SIGINT', () => {
server.close(() => {
console.log('Exiting process...')
process.exit(0)
})
})
server.on('close', async () => {
console.log('Server is closing...')
logger.close()
database.close()
})
module.exports = { app, server }