-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtelegram_bot.js
More file actions
45 lines (35 loc) · 982 Bytes
/
telegram_bot.js
File metadata and controls
45 lines (35 loc) · 982 Bytes
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
var vars = require(__dirname + "/vars.js")
const TelegramBot = require("node-telegram-bot-api")
var chatBot = null
const EventEmitter = require("events")
class Bot extends EventEmitter {
constructor() {
super()
}
init(listenChatIdOnly = false) {
var token = vars.options.telegram.token
if(!token) return
chatBot = new TelegramBot(token, {polling: true})
if(listenChatIdOnly) {
chatBot.on("message", (message) => {
const chatId = message.chat.id
console.log("TELEGRAM CHAT ID:", chatId)
chatBot.sendMessage(chatId, "CHAT ID: " + chatId)
})
return
}
chatBot.on("polling_error", function(error) {
console.log("Telegram error:", error)
})
chatBot.on("message", (message) => {
message = message.text.toLowerCase()
this.emit("messageReceived", message)
})
}
sendMessage(message) {
var chatId = vars.options.telegram.chatId
if(!chatBot || !chatId) return
chatBot.sendMessage(chatId, message)
}
}
module.exports = Bot