-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
96 lines (72 loc) · 2.35 KB
/
index.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
const pkg = require('./package.json');
const assert = require('assert');
const TelegramBot = require('node-telegram-bot-api');
const Unifi = require('ubnt-unifi');
const debug = require('debug');
const LOG = debug('app');
LOG(`Starting ${pkg.name} v${pkg.version}`);
assert(process.env.UNIFI_USERNAME, 'UNIFI_USERNAME is not defined')
assert(process.env.UNIFI_PASSWORD, 'UNIFI_PASSWORD is not defined')
assert(process.env.UNIFI_AP_MAC, 'UNIFI_AP_MAC is not defined')
assert(process.env.TELEGRAM_TOKEN, 'TELEGRAM_TOKEN is not defined')
function parseNote(note) {
if (!note) {
return false;
}
const match = note.match(/user:([\wа-яА-Я]+)/u);
if (!match) {
return false;
}
const name = match[1];
return name;
}
const unifi = new Unifi({
host: 'ctrl.mech.sh',
port: 443,
username: process.env.UNIFI_USERNAME,
password: process.env.UNIFI_PASSWORD,
site: 'default',
insecure: true,
});
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, {
polling: true
});
const visitors = {};
unifi.on('**', function (...args) {
debug('unifi')('incoming event:', this.event, args);
});
unifi.on('ctrl.connect', () => LOG('connected'));
unifi.on('ctrl.disconnect', () => LOG('disconnected'));
unifi.on('ctrl.reconnect', () => LOG('reconnect'));
unifi.on('ctrl.error', (err) => LOG('error', err));
unifi.on('wu.connected', async (event) => {
const { data } = await unifi.get(`stat/sta/${event.user}`);
const { ap_mac, note } = data[0];
const name = parseNote(note);
if (name && ap_mac === process.env.UNIFI_AP_MAC) {
LOG(`add visitor ${event.user} with name ${name}`);
visitors[event.user] = { name };
}
});
unifi.on('wu.disconnected', (event) => {
if (!visitors[event.user]) {
return;
}
const { name } = visitors[event.user];
LOG(`remove visitor ${event.user} with name ${name}`);
delete visitors[event.user];
});
unifi.get('stat/sta')
.then(({ data }) => {
for (const { ap_mac, mac, note } of data) {
const name = parseNote(note);
if (name && ap_mac === process.env.UNIFI_AP_MAC) {
LOG(`add visitor ${mac} with name ${name}`);
visitors[mac] = { name };
}
}
});
bot.onText(/\/(whoshome|whosthere)/, (msg) => {
const names = Object.values(visitors).map(e => e.name)
bot.sendMessage(msg.chat.id, `*Сейчас в офисе:*\n${names.join('\n')}`, { parse_mode: 'Markdown' });
});