-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelegramBot.js
146 lines (146 loc) · 4.4 KB
/
TelegramBot.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
134
135
136
137
138
139
140
141
142
143
144
145
146
var events = require("events");
var crypto = require("crypto");
var Telegraf = null;
var TelegramMessage = require("./TelegramMessage");
var TelegramInteraction = require("./TelegramInteraction");
var TelegramPaymentInProgress = require("./TelegramPaymentInProgress");
var TelegramPayment = require("./TelegramPayment");
var TelegramUser = require("./TelegramUser");
if (typeof EventEmitter === "undefined") {
var { EventEmitter } = events;
}
module.exports = class extends EventEmitter {
constructor(options, client) {
super();
this.options = Object.assign({
"token": "",
"debug": !1,
"test": !1
}, options || {});
if (client) {
this.client = client;
} else {
Telegraf = require("telegraf").Telegraf;
this.client = new Telegraf(this.options.token, {
"telegram": {
"testEnv": this.options.test
}
});
}
this.commands = new Map();
this.slashCommands = new Map();
this.users = new Map();
this.menubtn = "commands";
if (this.options.debug) {
this.client.on("polling_error", console.log);
this.client.on("webhook_error", console.log);
this.client.on("error", console.log);
} else {
this.client.on("polling_error", () => {});
this.client.on("webhook_error", () => {});
}
this.client.on("message", message => {
if (message.message.successful_payment) {
return this.emit("paymentSuccess", new TelegramPayment(message, this));
}
message = new TelegramMessage(message, this);
if (!message.content) {
return;
}
var args = message.content.split(" ");
var cmd = args.shift();
var command = this.slashCommands.get(cmd) || this.commands.get(cmd);
if (command) {
try {
command.execute({
message,
command,
args,
"bot": this
});
} catch(e) {
console.log(e);
}
}
this.emit("message", message);
});
this.client.on("callback_query", interaction => {
this.emit("interaction", new TelegramInteraction(interaction, this));
});
this.client.on("pre_checkout_query", payment => {
this.emit("paymentProgress", new TelegramPaymentInProgress(payment, this));
});
}
command(basic, executor) {
if (typeof basic === "string") {
basic = {
"name": basic
};
}
this.commands.set(basic.name, Object.assign(basic, {
"execute": executor
}));
return this;
}
slashCommand(basic, executor) {
if (!basic.name.startsWith("/")) {
throw new Error("Slash command starts with /.");
}
this.slashCommands.set(basic.name, Object.assign(basic, {
"execute": executor
}));
return this;
}
menuButton(target, label, link) {
this.menubtn = { target, label, link };
return this;
}
run() {
this.client.launch();
var commands = [];
for (var cmd of this.slashCommands.values()) {
commands.push({
"command": cmd.name.replace("/", ""),
"description": cmd.description
});
}
this.client.telegram.setMyCommands(commands);
if (this.menubtn.target == "web") {
this.client.telegram.setChatMenuButton({
"menuButton": JSON.stringify({
"type": "web_app",
"text": this.menubtn.label,
"web_app": {
"url": this.menubtn.link
}
})
});
} else {
this.client.telegram.setChatMenuButton({
"menuButton": JSON.stringify({
"type": "commands"
})
});
}
return this;
}
parseInitData(qs) {
if (typeof qs !== "string") {
return !1;
}
var dcs = decodeURIComponent(qs).split("&").sort();
var hash = dcs.find(a => a.startsWith("hash="));
if (!hash) {
return !1;
}
hash = hash.split("=")[1];
dcs = dcs.filter(a => !a.startsWith("hash=")).join("\n");
var secretKey = crypto.createHmac("sha256", "WebAppData").update(this.options.token).digest();
var checkHash = crypto.createHmac("sha256", secretKey).update(dcs).digest("hex");
return (checkHash === hash ? new TelegramUser(JSON.parse(dcs.split("\n").find(a => a.startsWith("user=")).split("=")[1]), this) : null);
}
stop() {
this.client.bot("SIGINT");
return this;
}
};