-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (40 loc) · 1.52 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
require('dotenv-safe').load();
// Bot part
const axios = require('axios');
const token = process.env.TOKEN;
const chat = process.env.CHANNEL;
const admin = process.env.ADMIN_CHAT_ID;
const sendMessage = (chat_id, text) =>
axios.post(`https://api.telegram.org/bot${token}/sendMessage`, {
chat_id,
text,
}).catch(e => console.error(e.message));
const errorHandler = (e) => sendMessage(admin, e.message);
// Persistence
const sqlite = require('sqlite');
const dbPromise = sqlite.open(`${process.env.DATA_DIR}/db.sqlite`, { Promise });
const prepareQuery = `
CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, identifier TEXT, text TEXT);`;
const selectQuery = identifiers =>
`SELECT identifier FROM messages WHERE messages.identifier IN ('${identifiers.join('\',\'')}')`;
const insertQuery = ({ id, text }) =>
`INSERT INTO messages(identifier, text) VALUES ('${id}', '${text}')`;
const getInfo = require(`./${process.env.INFO_SCRIPT}`);
async function main() {
try {
const db = await dbPromise;
await db.run(prepareQuery);
const data = await getInfo();
const rows = await db.all(selectQuery(data.map(el => el.id)));
const dbIdentifiers = rows.map(row => row.identifier);
const newData = data.filter(el => !dbIdentifiers.includes(el.id));
// Promises in sequence
await newData.reduce((prev, el) => prev.then(async () => {
await sendMessage(chat, el.text);
return db.run(insertQuery(el));
}), Promise.resolve());
} catch(e) {
errorHandler(e);
}
};
main();