This repository was archived by the owner on Dec 30, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
50 lines (42 loc) · 1.19 KB
/
bot.js
File metadata and controls
50 lines (42 loc) · 1.19 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
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/node
'use strict'
const path = require('path')
const _ = require('lodash')
const BotApi = require('telegram-bot-api')
const conf = require(path.join(__dirname, 'config.json'))
const parser = require(path.join(__dirname, 'lib', 'parser'))
var apiOptions = {
token: conf.general.token,
updates: {
enabled: true,
get_interval: conf.general.updateInterval
}
}
if (typeof conf.general.http_proxy === 'object') {
apiOptions.http_proxy = conf.general.http_proxy
}
var api = new BotApi(apiOptions)
var BotData = {}
api.getMe((err, data) => {
if (err) {
console.error(err.error)
process.exit(1)
}
BotData = data
api.on('message', (message) => {
onMessage(message)
})
})
var commands = require(path.join(__dirname, 'lib', 'commands'))(api, conf.commands)
function onMessage (message) {
var commandData = parser(message.text)
if (commandData.user === BotData.username || !commandData.user) {
console.log(commandData)
if (_.has(commands, commandData.command)) {
commands[commandData.command](message, commandData)
} else {
commands['UNKNOWN'](message, commandData)
console.error('unknown command >', commandData.command, '<')
}
}
}