-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
99 lines (82 loc) · 2.14 KB
/
test.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
const process = require("process");
const Telegraf = require("telegraf");
const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const util = require("util");
const readFile = util.promisify(fs.readFile);
const stat = util.promisify(fs.stat);
const writeFile = util.promisify(fs.writeFile);
class File {
constructor(filename) {
this.filename = filename;
}
async getContent () {
let fileExists = true;
try {
await stat(this.filename);
}
catch (e) {
fileExists = false;
}
let content;
if (fileExists)
content = JSON.parse(await readFile(this.filename, "utf8"));
else
content = [];
return content;
}
async setContent (content) {
await writeFile(this.filename, JSON.stringify(content), "utf8");
}
}
class Registry {
constructor(kw) {
this.file = new File(kw.filename);
}
async add(chatID) {
const content = await this.file.getContent();
content.push(chatID);
this.file.setContent(filterUnique(content));
function filterUnique(list) {
const unique = [];
for (const el of list)
if (! unique.includes(el)) unique.push(el);
return unique;
}
}
async getAll() {
return await this.file.getContent();
}
}
const registry = new Registry({filename: "hello.world"});
const bot = new Telegraf(process.env.TELEGRAM_TOKEN)
bot.start((ctx) => {
registry.add(ctx.chat.id);
});
bot.hears(/ping/i, (ctx) => {
ctx.reply("pong!");
});
bot.launch()
const app = express();
app.use(bodyParser.json());
app.post("/notify", async (req, res) => {
const clients = await registry.getAll();
await Promise.all(clients.map(client => { bot.telegram.sendMessage(client, formatMessage(req.body)); }));
res.send();
});
function formatMessage (data) {
let msg = "";
if (data.added.length > 0) {
msg += "Добавлены: \n";
for (const title of data.added)
msg += `- ${title}\n`;
}
if (data.removed.length > 0) {
msg += "Пропали: \n";
for (const title of data.removed)
msg += `- ${title}\n`;
}
return msg;
}
app.listen(8080, () => { console.log("listening"); });