-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
108 lines (93 loc) · 3.18 KB
/
bot.js
File metadata and controls
108 lines (93 loc) · 3.18 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
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
106
107
108
import TelegramBot from 'node-telegram-bot-api'
import * as mqtt from 'mqtt'
import { MachineReader } from './machine-reader.js'
const token = process.env.TELEGRAM_BOT_KEY ?? ''
const allowChatId = process.env.ALLOW_CHAT_ID ?? ''
const bot = new TelegramBot(token, { polling: true })
const client = mqtt.connect("mqtt://mqtt");
let status = {}
client.on("connect", async function () {
const machineReader = new MachineReader();
const machines = await machineReader.getAllMachines()
machines.forEach(machine => {
client.subscribe(`${machine['address']}/+`)
})
});
client.on("message", async function (topic, message) {
// message is Buffer
const [ address, _topic ] = topic.split('/')
if (["manual_feed", "feed_state", "battery_percentage", "power_mode", "indicator"].indexOf(_topic) !== -1) {
if (!address in status) {
status[address] = {}
}
status = { ...status, address: { ...status['address'], [_topic]: message.toString() } }
return
}
if (_topic.includes('error') || _topic.includes('feed')) {
const notificationUsers = process.env.NOTIFICATION_USERS;
await Promise.all(notificationUsers.split('/').map(async user => {
await bot.sendMessage(user, `${_topic}: ${message.toString()}`)
}))
}
});
bot.setMyCommands([
{
command: 'manual_feed',
description: 'manual_feed'
}, {
command: 'get_status',
description: 'get_status'
}, {
command: 'reconnect',
description: 'reconnect'
},
], { scope: { type: 'all_private_chats' }, language_code: 'en' }).then(() => {
console.log('command set ok')
})
bot.onText(/^\/manual_feed/, async (msg) => {
const chatId = msg.chat.id
const allow = allowChatId.split(',').find(chatIdInString => parseInt(chatIdInString) === chatId) !== undefined
console.log(chatId)
if (allow) {
let feedCount
try {
const { groups: { address, limitCount } } = /\/manual_feed (?<address>[^ $]*) (?<limitCount>[^ $]*)/.exec(msg.text)
feedCount = parseInt(limitCount)
client.publish(`${address}/feed`, feedCount.toString(), { qos: 1 }, (err) => {
console.log(err)
})
} catch (e) {
console.log(e)
feedCount = 1
//feedCount = parseInt(limitCount)
//client.publish(`${address}/feed`, feedCount.toString(), { qos: 1 }, (err) => {
//console.log(err)
//})
}
//await bot.sendMessage(msg.chat.id, "ok")
}
})
bot.onText(/^\/get_status/, async (msg) => {
const chatId = msg.chat.id
const allow = allowChatId.split(',').find(chatIdInString => parseInt(chatIdInString) === chatId) !== undefined
try {
const { groups: { address } } = /\/get_status (?<address>[^ $]*)/.exec(msg.text)
if (allow)
await bot.sendMessage(msg.chat.id, JSON.stringify(status[address]))
} catch {
return
}
})
bot.onText(/^\/reconnect/, async (msg) => {
const chatId = msg.chat.id
const allow = allowChatId.split(',').find(chatIdInString => parseInt(chatIdInString) === chatId) !== undefined
try {
const { groups: { address } } = /\/reconnect (?<address>[^ $]*)/.exec(msg.text)
if (allow)
client.publish(`${address}/reconnect`, '', { qos: 1 }, (err) => {
console.log(err)
})
} catch {
return
}
})