-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbot_en.js
333 lines (322 loc) · 11.8 KB
/
bot_en.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* https://github.com/amirwolf5122/telegram-bot-cloudflare
*/
const TOKEN = '***'// Get it from @BotFather https://core.telegram.org/bots#6-botfather
const ADMIN = 5831914878 //// Get it from @chatIDrobot
addEventListener('fetch', event => {
const url = new URL(event.request.url)
if (url.pathname === '/endpoint') {
event.respondWith(handleWebhook(event,database))
} else if (url.pathname === '/registerWebhook') {
event.respondWith(registerWebhook(event, url, '/endpoint', 'QUEVEDO_BZRP_Music_Sessions_522'))
} else if (url.pathname === '/unRegisterWebhook') {
event.respondWith(unRegisterWebhook(event))
} else {
event.respondWith(new Response('No handler for this request'))
}
})
async function handleWebhook (event,db) {
if (event.request.headers.get('X-Telegram-Bot-Api-Secret-Token') !== 'QUEVEDO_BZRP_Music_Sessions_522') {
return new Response('Unauthorized', { status: 403 })
}
const update = await event.request.json()
event.waitUntil(onUpdate(update,db))
return new Response('Ok')
}
async function onUpdate (update,db) {
if ('callback_query' in update) {
await onCallbackQuery(update.callback_query,db)
}
if ('message' in update) {
await onMessage(update.message,db)
}
return new Response('Ok')
//const myConfigs = (await db.get("ban"))?.split("\n") || [];
//return fetch(`https://api.telegram.org/bot${TOKEN}/sendMessage?chat_id=${ADMIN}&text=hi4${JSON.stringify(myConfigs, null, 2)}`);
}
async function onCallbackQuery (callback,db) {
if (callback.data == "ban"){
const ban = (await db.get("ban"))?.trim().split("\n") || [];
const user_id = callback.message.reply_markup.inline_keyboard[0][0].callback_data.split(":")[0].toString()
if (!(ban.includes(user_id))) {
ban.push(user_id);
await db.put("ban",ban.join("\n"));
(await fetch(apiUrl('sendMessage', {
chat_id: parseInt(user_id),
text: "BAN!"
}))).json()
}
(await fetch(apiUrl('sendMessage', {
chat_id: callback.message.chat.id,
text: "BAN!",
reply_to_message_id: callback.message.message_id
}))).json()
(await fetch(apiUrl('deleteMessage', {
chat_id: callback.message.chat.id,
message_id: callback.message.message_id
}))).json()
}else{
(await fetch(apiUrl('answerCallbackQuery', {
callback_query_id: callback.id,
text:"@amir_wolf512",
}))).json()
}
return new Response('Ok')
}
async function onMessage (message,db) {
const text = message.text || ""
if (message.chat.type == "private") {
const ban = ((await db.get("ban"))?.trim().split("\n")) || [];
if ( message.from.id == ADMIN || !(ban.includes(message.from.id.toString()))) {
if (message.from.id == ADMIN) {
if (text.split(" ").length ==2 && text.split(" ")[0] == "ban" && !isNaN(text.split(" ")[1]/0)) {
const user_id = text.split(" ")[1]
if (!(ban.includes(user_id))) {
ban.push(user_id);
await db.put("ban",ban.join("\n"));
(await fetch(apiUrl('sendMessage', {
chat_id: parseInt(user_id),
text: "BAN!"
}))).json()
}
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "BAN!",
reply_to_message_id: message.message_id
}))).json()
}
else if (text.split(" ").length ==2 && text.split(" ")[0] == "unban" && !isNaN(text.split(" ")[1]/0)) {
const user_id = text.split(" ")[1]
if ((ban.includes(user_id))) {
const ban2 = ban.filter(elem => elem !== user_id);
if (JSON.stringify(ban2, null, 2) != "[]"){
await db.put("ban",ban2.join("\n"));
}else{
await db.delete("ban");
}
(await fetch(apiUrl('sendMessage', {
chat_id: parseInt(user_id),
text: "UNban!"
}))).json()
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "UNban!",
reply_to_message_id: message.message_id
}))).json()
}else{
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "ID not found",
reply_to_message_id: message.message_id
}))).json()
}
}
else if (text == "banlist") {
if (JSON.stringify(ban, null, 2) != "[]"){
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "Banned list:\n〰️\n<code>"+ban.join("</code>\n〰️\n<code>")+'</code>',
parse_mode: "HTML",
reply_to_message_id: message.message_id
}))).json()
}else{
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "The list is empty",
reply_to_message_id: message.message_id
}))).json()
}
}
else if (text == "cleanbanall") {
if (JSON.stringify(ban, null, 2) != "[]"){
await db.delete("ban");
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "The list is empty",
reply_to_message_id: message.message_id
}))).json()
}else{
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "The list is empty",
reply_to_message_id: message.message_id
}))).json()
}
}
}
if (text == "/start") {
if (message.from.id == ADMIN) {
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: `
hello welcome
List of commands
➿〰️〰️〰️〰️〰️〰️〰️➿
▶️ <code>ban</code> [chatid]
➿〰️〰️〰️〰️〰️〰️〰️➿
▶️ <code>unban</code> [chatid]
➿〰️〰️〰️〰️〰️〰️〰️➿
▶️ <code>banlist</code>
➿〰️〰️〰️〰️〰️〰️〰️➿
▶️ <code>cleanbanall</code>
➿〰️〰️〰️〰️〰️〰️〰️➿
〰️〰️@amir_wolf512〰️〰️
➿〰️〰️〰️〰️〰️〰️〰️➿`,
reply_to_message_id: message.message_id,
parse_mode: "HTML",
}))).json()
}else{
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "Send your message so I can deliver it to my owner :)",
reply_to_message_id: message.message_id
}))).json()
}
}else{
if (message.from.id != ADMIN) {
const replymarkup23 = JSON.stringify({
inline_keyboard:
[
[
{
text: "Name:",
callback_data: message.chat.id+':'+message.message_id
},
{
text: message.chat.first_name,
callback_data: message.chat.id+':'+message.message_id
}
],
[
{
text: "Last name",
callback_data: message.chat.id+':'+message.message_id
},
{
text: ((message.chat.last_name) || "None"),
callback_data: message.chat.id+':'+message.message_id
}
],
[
{
text: "id",
callback_data: message.chat.id+':'+message.message_id
},
{
text: message.chat.id,
callback_data: message.chat.id+':'+message.message_id
}
],
[
{
text: "Ban the user",
callback_data: "ban"
}
],
[
{
text: "Go to private message",
url: ((message.chat.username && 't.me/'+message.chat.username) || 'tg://openmessage?user_id='+message.chat.id)
}
]
]
})
if ('reply_to_message' in message) {
if (message.reply_to_message.from.id != message.from.id && !('reply_markup' in message.reply_to_message)){
(await fetch(apiUrl('copyMessage', {
from_chat_id: message.chat.id,
message_id: message.message_id,
chat_id: ADMIN,
reply_markup: replymarkup23
}))).json()
}else{
if (message.reply_to_message.from.id == message.from.id){
var reply = message.reply_to_message.message_id+1
}else{
var reply = message.reply_to_message.reply_markup.inline_keyboard[0][0].callback_data
}
(await fetch(apiUrl('copyMessage', {
from_chat_id: message.chat.id,
message_id: message.message_id,
chat_id: ADMIN,
reply_to_message_id: reply,
reply_markup: replymarkup23
}))).json()
}
}else{
(await fetch(apiUrl('copyMessage', {
from_chat_id: message.chat.id,
message_id: message.message_id,
chat_id: ADMIN,
reply_markup: replymarkup23
}))).json()
}
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "Sent!",
reply_to_message_id: message.message_id
}))).json()
}else{
if(message.reply_to_message.from.id !=message.from.id){
const id23 = message.reply_to_message.reply_markup.inline_keyboard[0][0].callback_data.split(":")
const id223 = (await fetch(apiUrl('copyMessage', {
from_chat_id: message.chat.id,
message_id: message.message_id,
chat_id: id23[0],
reply_to_message_id:id23[1],
reply_markup: JSON.stringify({
inline_keyboard:
[
[
{
text: message.reply_to_message.from.first_name,
callback_data: message.message_id
}
]
]
})
})))
const response23 = await id223.json();
if(response23.description == "Forbidden: bot was blocked by the user"){
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "Failed to send The bot has been blocked by the user",
reply_to_message_id: message.message_id
}))).json()
}else{
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "Sent!",
reply_to_message_id: message.message_id
}))).json()
}
}
}
}
}else{
(await fetch(apiUrl('sendMessage', {
chat_id: message.chat.id,
text: "BAN!:(",
reply_to_message_id: message.message_id
}))).json()
}
}
return new Response('Ok')
}
async function registerWebhook (event, requestUrl, suffix, secret) {
// https://core.telegram.org/bots/api#setwebhook
const webhookUrl = `${requestUrl.protocol}//${requestUrl.hostname}${suffix}`
const r = await (await fetch(apiUrl('setWebhook', { url: webhookUrl, secret_token: secret }))).json()
return new Response('ok' in r && r.ok ? 'Ok' : JSON.stringify(r, null, 2))
}
async function unRegisterWebhook (event) {
const r = await (await fetch(apiUrl('setWebhook', { url: '' }))).json()
return new Response('ok' in r && r.ok ? 'Ok' : JSON.stringify(r, null, 2))
}
function apiUrl (methodName, params = null) {
let query = ''
if (params) {
query = '?' + new URLSearchParams(params).toString()
}
return `https://api.telegram.org/bot${TOKEN}/${methodName}${query}`
}