-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathecho-express.js
47 lines (36 loc) · 946 Bytes
/
echo-express.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
'use strict'
const http = require('http')
const express = require('express')
const bodyParser = require('body-parser')
const Bot = require('../')
let bot = new Bot({
token: 'PAGE_TOKEN',
verify: 'VERIFY_TOKEN',
app_secret: 'APP_SECRET'
})
bot.on('error', (err) => {
console.log(err.message)
})
bot.on('message', (payload, reply) => {
let text = payload.message.text
bot.getProfile(payload.sender.id, (err, profile) => {
if (err) throw err
reply({ text }, (err) => {
if (err) throw JSON.stringify(err)
console.log(`Echoed back to ${profile.first_name} ${profile.last_name}: ${text}`)
})
})
})
let app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
app.get('/', (req, res) => {
return bot._verify(req, res)
})
app.post('/', (req, res) => {
bot._handleMessage(req.body)
res.end(JSON.stringify({status: 'ok'}))
})
http.createServer(app).listen(3000)