-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.mjs
76 lines (67 loc) · 2.31 KB
/
api.mjs
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
import fetch from "node-fetch"
import telegram from "telegram-bot-api"
import fs from "fs"
var config = JSON.parse(fs.readFileSync("./config.json", "utf-8"));
var tele = new telegram({token: config.token});
const url = `https://api.telegram.org/bot${config.token}/`;
export async function sendMessage(text, chat_id, reply_markup) {
if (reply_markup === undefined)
reply_markup = {remove_keyboard: true};
else
reply_markup = {keyboard: reply_markup};
await fetch(`${url}sendMessage`, {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({chat_id, text, reply_markup})
});
}
export async function sendPhoto(link, chat_id, reply_markup) {
if (reply_markup === undefined)
reply_markup = {remove_keyboard: true};
else
reply_markup = {keyboard: reply_markup};
await fetch(`${url}sendPhoto`, {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({chat_id, photo: link, reply_markup})
});
}
export async function sendDocument(link, chat_id, reply_markup) {
if (reply_markup === undefined)
reply_markup = {remove_keyboard: true};
else
reply_markup = {keyboard: reply_markup};
await fetch(`${url}sendDocument`, {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({chat_id, document: link, reply_markup})
});
}
let lastOffset = 0;
export function getUpdates() {
return fetch(`${url}getUpdates`, {
headers: {"Content-Type": "application/json"},
method: "POST",
body: JSON.stringify({offset: lastOffset + 1})
})
.then(res => res.json())
.then(res => {
res = res.result;
if (res.length > 0)
lastOffset = res[res.length - 1].update_id;
return res;
}).catch(err => {
console.error(err);
});
/* return tele.getUpdates( { offset : lastOffset+1 } ).then(res=>{
if(res.length > 0)
lastOffset= res[ res.length -1 ].update_id;
return res;
});*/
}
export async function sendBackup(chat_id) {
let now = parseInt(fs.readFileSync(`./backup/users/now`, "utf-8"));
await tele.sendDocument({chat_id, document: `./backup/users/${now}.json`});
now = parseInt(fs.readFileSync(`./backup/problems/now`, "utf-8"));
await tele.sendDocument({chat_id, document: `./backup/problems/${now}.json`});
}