-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (81 loc) · 2.34 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
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const moment = require('moment')
const app = express()
require('moment/locale/fr')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
const weekEnd = ['weekend', 'we', 'week end', 'week-end']
const hello = ['hello', 'salut', 'bonjour', 'coucou']
const server = app.listen(process.env.PORT || 5000, () => {
console.log(
'Express server listening on port %d in %s mode',
server.address().port,
app.settings.env
)
})
function getNextWeekend() {
const now = moment().locale('fr')
const deadlineFrom = moment()
.isoWeekday(5)
.set({ h: 18, m: 0, s: 0 })
const deadlineTo = moment()
.isoWeekday(5)
.add(3, 'days')
.set({ h: 8, m: 0, s: 0 })
if (now >= deadlineFrom && now <= deadlineTo)
return `C'est le week-end !\nhttps://www.youtube.com/watch?v=Meb7uaNlcS0`
return `C'est ${deadlineFrom.from(now)}`
}
/* For Facebook Validation */
app.get('/webhook', (req, res) => {
if (
req.query['hub.mode'] &&
req.query['hub.verify_token'] === process.env.VERIFY_TOKEN
) {
res.status(200).send(req.query['hub.challenge'])
} else {
res.status(403).end()
}
})
/* Handling all messenges */
app.post('/webhook', (req, res) => {
if (req.body.object === 'page') {
req.body.entry.forEach(entry => {
entry.messaging.forEach(event => {
if (event.message && event.message.text) {
if (
new RegExp(weekEnd.join('|')).test(event.message.text.toLowerCase())
)
sendMessage(event.sender.id, getNextWeekend())
else if (
new RegExp(hello.join('|')).test(event.message.text.toLowerCase())
)
sendMessage(event.sender.id, `Salut`)
}
})
})
res.status(200).end()
}
})
function sendMessage(senderId, message) {
request(
{
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: {
recipient: { id: senderId },
message: { text: message }
}
},
function(error, response) {
if (error) {
console.log('Error sending message: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
}
)
}