-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
105 lines (85 loc) · 2.92 KB
/
bot.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict'
const page_token = process.env.page_token
const verify_token = process.env.verify_token
const api_key = process.env.api_key
const botkit = require('botkit')
const os = require('os')
const commandLineArgs = require('command-line-args')
const localtunnel = require('localtunnel')
const ops = commandLineArgs([{
name: 'lt',
alias: 'l',
args: 1,
description: 'Use localtunnel.me to make your bot available on the web.',
type: Boolean,
defaultValue: false
}, {
name: 'ltsubdomain',
alias: 's',
args: 1,
description: 'Custom subdomain for the localtunnel.me URL. This option can only be used together with --lt.',
type: String,
defaultValue: null
}, ])
const controller = botkit.facebookbot({
debug: false,
access_token: process.env.page_token,
verify_token: process.env.verify_token,
json_file_store: 'database'
})
const bot = controller.spawn({})
const Conversation = require('./conversation')
const conversation = new Conversation(api_key, page_token)
if (!page_token) {
console.log('Error: Specify page_token in environment')
process.exit(1)
}
if (!verify_token) {
console.log('Error: Specify verify_token in environment')
process.exit(1)
}
if (!api_key) {
console.log('Error: Specify api_key in environment')
process.exit(1)
}
if (ops.lt === false && ops.ltsubdomain !== null) {
console.log('Error: --ltsubdomain can only be used together with --lt.')
process.exit()
}
controller.setupWebserver(process.env.port || 3000, (err, webserver) => {
controller.createWebhookEndpoints(webserver, bot, () => {
console.log('ONLINE!')
if (ops.lt) {
var tunnel = localtunnel(process.env.port || 3000, { subdomain: ops.ltsubdomain }, (err, tunnel) => {
if (err) {
console.log(err)
process.exit()
}
console.log('Your bot is available on the web at the following URL: ' + tunnel.url + '/facebook/receive')
})
tunnel.on('close', () => {
console.log('Your bot is no longer available on the web at the localtunnnel.me URL.')
process.exit()
})
}
})
})
controller.hears(['help', 'bantuan'], 'message_received', (bot, message) => {
conversation.sendHelp(bot, message)
})
controller.hears(['kurs'], 'message_received', (bot, message) => {
conversation.askBankKurs(bot, message)
})
controller.hears(['pln'], 'message_received', (bot, message) => {
conversation.askPLN(bot, message)
})
controller.hears(['zodiak'], 'message_received', (bot, message) => {
conversation.askZodiak(bot, message)
})
controller.hears(['siswa'], 'message_received', (bot, message) => {
conversation.askSiswa(bot, message)
})
controller.on('message_received', (bot, message) => {
bot.reply(message, 'Maaf perintah tidak ditemukan, silahkan ketik `help atau bantuan`.')
return
})