-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (44 loc) · 1.46 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
/* eslint-env node */
'use strict'
// Set environment variables at the start of the program
const dotenv = require('dotenv');
dotenv.load();
const express = require('express');
const bodyParser = require('body-parser');
const config = require('./config');
const Responser = require('./response/responser.js');
const app = express();
const responser = new Responser();
app.set('port', (process.env.PORT || 5000))
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))
// Process application/json
app.use(bodyParser.json())
// Index route
app.get('/', function (req, res) {
res.send('Hello world, I am a chat bot')
})
// for Facebook verification
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === config.FACEBOOK_VERIFY_TOKEN) {
res.send(req.query['hub.challenge'])
}
res.send('Error, wrong token')
})
app.post('/webhook/', function (req, res) {
let messaging_events = req.body.entry[0].messaging
for (let i = 0; i < messaging_events.length; i++) {
let event = req.body.entry[0].messaging[i]
let sender = event.sender.id
if (event.message && event.message.text) {
let text = event.message.text
console.log('Message received:', text);
responser.respond(sender, text);
}
}
res.sendStatus(200)
})
// Spin up the server
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'))
})