-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (61 loc) · 1.84 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
const version = require('./package.json').version
const express = require('express')
const bodyParser = require('body-parser')
const server = express().use(bodyParser.json())
const config = require("./package.json").config
const chat = require('./services/chatService')
const port = process.env.PORT || config.port
const keyword = require('./data/keyword').keyword
server.get('/webhook', (req, res) => {
let VERIFY_TOKEN = config.vertifyToken
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 === VERIFY_TOKEN) {
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challenge);
} else {
res.sendStatus(403);
}
} else {
res.sendStatus(403)
}
})
server.post('/webhook', (req, res) => {
let body = req.body
let recipient = body.entry[0].messaging[0].sender.id
console.log('body', body)
let message = getMessage(body.entry[0].messaging[0].message.text)
console.log(body)
chat.sendMessage(recipient, message)
res.end()
})
server.get('/', (req, res) => {
let message = getMessage('test')
chat.sendMessage('5271474912870505', message)
res.send({
status: 200, message: '.....'
})
})
// send server version.
server.get('/version', (req, res) => {
res.send(version)
})
server.listen(port, () => {
console.log(config.serverRunningMessage)
})
function getMessage(key) {
let result = undefined
for (i in keyword) {
if (i.includes(key)) {
result = keyword[i]
break
}
}
if (result === undefined) {
return 'ไม่เข้าใจสิ่งที่คุณพูด'
} else {
return result
}
}