-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.js
More file actions
40 lines (34 loc) · 1.03 KB
/
bot.js
File metadata and controls
40 lines (34 loc) · 1.03 KB
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
const Bot = require('messenger-bot')
const http = require('http')
const cli = require('./cli')
const handlers = require('./handlers')
let bot = null
if (cli.interactive) {
bot = require('./interactive').instance
module.exports.sendMessage = bot.sendMessage.bind(bot)
} else {
bot = new Bot({
token: process.env.PAGE_ACCESS_TOKEN,
verify: 'testbot_verify_token',
app_secret: process.env.APP_SECRET,
})
bot.on('error', (err) => {
console.log(err.message)
})
bot.on('message', (payload, reply) => {
bot.getProfile(payload.sender.id, (err, profile) => {
if (err) throw err
payload.sender.profile = profile
handlers.disptachMessage(payload, reply)
})
})
bot.on('postback', handlers.dispatchPostback)
module.exports.sendMessage = bot.sendMessage.bind(bot)
bot.startListening = function() {
const port = process.env.PORT || 3000
http.createServer(bot.middleware()).listen(port, () => {
console.log(`Echo bot server running at port ${port}.`)
})
}
}
bot.startListening()