-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (62 loc) · 2.59 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
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const facebookModule = require('fb-bot');
const Sequelize = require('sequelize');
const models = require('./models');
let fs = require('fs');
let env = JSON.parse(fs.readFileSync('env.json', 'utf8'));
const request = require('request');
const MessagesController = require('./controllers/messages');
const UsersConteoller = require('./controllers/users');
const ButtonsController = require('./controllers/buttons');
const MessagesHelper = require('./helpers/messagesHelper');
const MyJobsHelper = require('./helpers/MyjobsHelper');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
facebookModule.init({
APP_SECRET: env.APP_SECRET,
VALIDATION_TOKEN: env.VALIDATION_TOKEN,
PAGE_ACCESS_TOKEN: env.PAGE_ACCESS_TOKEN,
SERVER_URL: env.SERVER_URL
});
app.use((req, res, next) => {
//tests for all web hooks
next();
});
app.use(bodyParser.json({verify: facebookModule.verifyRequestSignature}));
app.get('/', facebookModule.authGET, function (req, res, next) {
res.status(200).send(req.query['hub.challenge']);
});
app.post('/', facebookModule.parsePOST, function (req, res) {
// console.log("req body :", req.body);
let dataList = req.afterParse;
if (dataList[0].messageType === 'Postback') {
UsersConteoller.saveUser(dataList[0].senderId);
MessagesHelper.welcome(dataList);
} else if (dataList[0].messageType === '') {
if (dataList[0].message.text === 'hi'
|| dataList[0].message.text === 'HI'
|| dataList[0].message.text === 'Hi'
|| dataList[0].message.text === 'Hello'
|| dataList[0].message.text === 'hello') {
UsersConteoller.saveUser(dataList[0].senderId);
MessagesHelper.welcome(dataList);
} else {
MessagesHelper.inputForID(dataList[0]);
}
} else if (dataList[0].messageType === 'QuickReply') {
console.log(" PAYLOAD : ", dataList[0].message.quick_reply);
let next = dataList[0].message.quick_reply.payload.split('_')[1];
let type = dataList[0].message.quick_reply.payload.split('_')[2];
let answerId = dataList[0].message.quick_reply.payload.split('_')[3];
MessagesHelper.nextStep(next, parseInt(type), parseInt(answerId), dataList)
} else {
console.log(" NOT MESSAGE WEB HOOK ");
}
res.sendStatus(200);
});
app.post('/mass', (req, res) => {
UsersConteoller.sendMassMessages(req, res);
});
app.listen(3000, () => console.log('Webhook server is listening, port 3000'));