forked from chyuaner/moztw_doorlock_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (91 loc) · 3.01 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require('dotenv').config();
const { Telegraf } = require('telegraf');
const { getDoorStatus, openDoor } = require('./doorlock');
let telegram_api_key = process.env["TELEGRAM_BOT"]
let allow_chat_ids = process.env["ALLOW_CHAT_ID"].split(',');
let allow_usernames = process.env["ALLOW_USERNAME"].split(',');
/**
* 檢查是否授權開門
* @return bool 1:授權開門 0:未授權
*/
function checkFrom(ctx) {
let callChatId = ctx.chat.id;
let callUsername = ctx.from.username;
// 不允許的聊天室
if (!allow_chat_ids.includes(callChatId.toString())) {
return false;
}
// 不允許的使用者
else if (!allow_usernames.includes(callUsername)){
return false;
}
else {
return true;
}
}
// Create an instance of the `Bot` class and pass your bot token to it.
// const bot = new Bot(telegram_api_key); // <-- put your bot token between the ""
const bot = new Telegraf(telegram_api_key); // <-- put your bot token between the ""
// bot.command("help" /* , ... */);
bot.command(["opendoor", "dooropen"], (ctx) => {
let callUsername = ctx.from.username;
let isAllow = checkFrom(ctx);
if (isAllow) {
// 開門
// TODO: 待測試
openDoor().then((res) => {
// console.log(res.data);
// ctx.reply(JSON.stringify(res.data, null, ' '));
ctx.reply("@"+callUsername+" 已打開MozTW門(11樓)")
})
.catch((error) => {
let status = error.response.status;
let data = error.response.data;
ctx.reply(status+"錯誤: "+data);
});
} else {
ctx.reply("非請勿入!")
}
});
bot.command(["doorstatus"], (ctx) => {
let isAllow = checkFrom(ctx);
if (isAllow) {
// 取得門鎖狀態
getDoorStatus().then((res) => {
// console.log(res.data);
ctx.reply(JSON.stringify(res.data, null, ' '));
})
.catch((error) => {
let status = error.response.status;
let data = error.response.data;
ctx.reply(status+"錯誤: "+data);
});
} else {
ctx.reply("非請勿入!")
}
});
bot.command("debug", (ctx) => {
// console.log(ctx.from);
// console.log(ctx.chat);
let callchatId = ctx.chat.id;
let callchatTitle = ctx.chat.title;
let callUserId = ctx.from.id;
let callUsername = ctx.from.username;
let isallow = checkFrom(ctx);
let sendText = "<b>Debug資訊:</b> \n"
+"chat.id: "+callchatId.toString()+"\n"
+"chat.title: "+callchatTitle+"\n"
+"from.id: "+callUserId.toString()+"\n"
+"from.username: "+callUsername+"\n"
+"isAllow:"+isallow;
// Send the reply.
ctx.replyWithHTML(sendText)
});
// Handle other messages.
bot.on("message", (ctx) => ctx.reply("這不是門神能處理的事~"));
bot.launch().catch(err => {
colsole.log(err);
});
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))