-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (108 loc) · 3.2 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require('dotenv').config();
const util = require('util');
const fs = require('fs');
const TelegramBot = require('node-telegram-bot-api');
const express = require('express');
const {nanoid} = require('nanoid');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
let chatsMap = {};
const CHATS_MAP_CACHE = './chats_map.json';
const {
TG_TOKEN,
PORT,
API_END_POINT
} = process.env;
const apiKeyRegistered = (chatId) => {
for (const apiKey of Object.keys(chatsMap)) {
if (chatsMap[apiKey] === chatId) {
return apiKey;
}
}
};
const removeChat = async (chatId) => {
for (const apiKey of Object.keys(chatsMap)) {
if (chatsMap[apiKey] === chatId) {
delete chatsMap[apiKey];
}
}
await syncChatsMapCache();
};
const syncChatsMapCache = async () => {
try {
await writeFile(CHATS_MAP_CACHE, JSON.stringify(chatsMap));
}
catch (err) {
console.error(err);
}
};
const handleSendMessageError = async (err, chatId) => {
console.error(`Error on chat ${chatId}, ${err.message}`);
if (/chat not found/.test(err.message)) {
await removeChat(chatId);
}
throw(err);
}
const startBot = async () => {
const bot = new TelegramBot(TG_TOKEN, {polling: true});
bot.onText(/\/start/, async (msg) => {
const curentChatId = msg.chat.id;
let apiKey = nanoid();
const oldApiKey = apiKeyRegistered(curentChatId);
if (oldApiKey) {
delete chatsMap[oldApiKey];
}
chatsMap[apiKey] = curentChatId;
syncChatsMapCache();
try {
await bot.sendMessage(curentChatId, 'Your api key is:');
await bot.sendMessage(curentChatId, `${apiKey}`);
await bot.sendMessage(curentChatId, 'Try it:');
await bot.sendMessage(curentChatId, `\`curl --request GET 'https://${API_END_POINT}/send-notification?message=simple_message_please' --header 'x-api-key: ${apiKey}'\``, {parse_mode : 'markdown'});
}
catch (err) {
console.error(err);
}
});
return bot;
};
const startApp = (bot) => new Promise((resolve) => {
const app = express();
app.get('/send-notification', async (req, res) => {
try {
const chatId = chatsMap[req.headers['x-api-key']];
if (!chatId) {
return res.sendStatus(403);
}
const {message} = req.query;
if (!message) {
return res.sendStatus(400);
}
await bot
.sendMessage(chatId, message)
.catch((err) => handleSendMessageError(err, receiverChat));
res.sendStatus(200);
}
catch (err) {
console.error(err);
res.sendStatus(500);
}
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server listening at ${PORT}`);
resolve();
});
});
const getChatsMap = async () => {
try {
chatsMap = JSON.parse((await readFile(CHATS_MAP_CACHE)).toString());
}
catch (err) {
console.error(err);
}
};
module.exports = {
getChatsMap,
startApp,
startBot
};