-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tel_bot.js
330 lines (323 loc) · 13.9 KB
/
tel_bot.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
/**
* @fileoverview This script handle a Telegram support bot to be contacted anonymously.
*
* @author Mqtth3w https://github.com/Mqtth3w/
* @license GPL-3.0+ https://github.com/Mqtth3w/Forwarder-Telegram-bot/blob/main/LICENSE
*
*/
/**
* Event listener that listens for the 'fetch' event and delegates to the `handleRequest` function.
*
* @param {FetchEvent} event - The FetchEvent object representing the incoming fetch event.
* The event contains the `request` object, which holds details about the incoming HTTP request.
*/
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
// "Const"
const nick = "Mqtth3w"; // Change it with your nickname (NOT something linkable to you or your identity)
const susp_info = "Sorry, the service is temporarily suspended.";
let url = `https://api.telegram.org/bot${API_KEY}/`;
const user_guide = "https://github.com/Mqtth3w/Forwarder-Telegram-bot/tree/main#user-guide";
const faq = "https://github.com/Mqtth3w/Forwarder-Telegram-bot/tree/main#faq";
// State "Const"
const blocked = ["-1"]
const suspended = false;
const custom_susp = "";
const pinned_usr = "";
const pc_user = true; // protect_content: If true protects the contents
const pc_dest = false; // of the sent message from forwarding and saving
const silent_user = false; // If true the user will receive the notifications without sound
const silent_dest = false;
/**
* Sends a text message to a specified user via a Telegram bot.
*
* @param {number|string} cId - The chat ID of the user to send the message to.
* @param {string} txt - The text message to be sent.
* @param {boolean} [pc=true] - Whether to protect the content of the message. Defaults to true.
* @param {boolean} [s=false] - If true, disables notification for the message. Defaults to false.
* @param {number|string} [prf] - The chat ID for the profile, used to generate an inline keyboard with a link to the user profile.
* @returns {Promise<void>} - This function does not return a value.
*/
async function SendMessage(cId, txt, pc = true, s = false, prf) {
let payload = {
chat_id: cId,
text: txt,
protect_content: pc,
disable_notification: s,
};
if (prf) {
payload.reply_markup = {
inline_keyboard: [[
{
text: "User Profile",
url: `tg://user?id=${prf}`,
}
]]
};
}
await fetch(url + 'sendMessage', {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
};
/**
* Forwards any type of message to a specified user via a Telegram bot.
*
* @param {number|string} cId - The chat ID of the user to send the message to.
* @param {number|string} fcId - The chat ID of the user who sent the message to be forwarded.
* @param {number|string} mId - The message ID of the message to be forwarded.
* @param {boolean} [pc=true] - Whether to protect the content of the message. Defaults to true.
* @param {boolean} [s=false] - If true, disables notification for the message. Defaults to false.
* @returns {Promise<void>} - This function does not return a value.
*/
async function ForwardMessage(cId, fcId, mId, pc = true, s = false) {
await fetch(url + 'forwardMessage', {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
chat_id: cId,
from_chat_id: fcId,
message_id: mId,
protect_content: pc,
disable_notification: s,
}),
});
};
/**
* Sends a media message (photo, sticker, document, video, animation, audio, voice, location, or contact) to a specified chat.
* The function determines the media type based on the message structure and sends the appropriate media type.
*
* @param {Object} msg - The message object containing the media or data to be sent.
* @param {number|string} dest - The chat ID of the destination where the status message will be sent.
* @param {number|string} cId - The chat ID of the user who will receive the media.
* @param {boolean} [pc=true] - Whether to protect the content of the message (user side). Defaults to true.
* @param {boolean} [pc_d=false] - Whether to protect the content of the status message (dest side). Defaults to false.
* @param {boolean} [s=false] - If true, disables notification for the media message (user side). Defaults to false.
* @param {boolean} [s_d=false] - If true, disables notification for the status message (dest side). Defaults to false.
* @returns {Promise<void>} - This function does not return a value.
*/
async function SendMedia(msg, dest, chatId, pc = true, pc_d = false, s = false, s_d = false) {
let method = "";
let method2 = "";
let methodr = "";
let fileId = "";
let payload = { chat_id: chatId, protect_content: pc, disable_notification: s };
if (msg.photo) {
method = "sendPhoto";
method2 = "photo";
methodr = "Photo";
fileId = msg.photo[msg.photo.length - 1].file_id;
}
else if (msg.sticker) {
method = "sendSticker";
method2 = "sticker";
methodr = "Sticker";
fileId = msg.sticker.file_id;
}
else if (msg.document) {
method = "sendDocument";
method2 = "document";
methodr = "Document";
fileId = msg.document.file_id;
}
else if (msg.video) {
method = "sendVideo";
method2 = "video";
methodr = "Video";
fileId = msg.video.file_id;
}
else if (msg.animation) {
method = "sendAnimation";
method2 = "animation";
methodr = "Animation";
fileId = msg.animation.file_id;
}
else if (msg.audio) {
method = "sendAudio";
method2 = "audio";
methodr = "Audio";
fileId = msg.audio.file_id;
}
else if (msg.voice) {
method = "sendVoice";
method2 = "voice";
methodr = "Voice";
fileId = msg.voice.file_id;
}
else if (msg.location) {
method = "sendLocation";
methodr = "Location";
payload.latitude = msg.location.latitude;
payload.longitude = msg.location.longitude;
}
else if (msg.contact) {
method = "sendContact";
methodr = "Contact";
payload.phone_number = msg.contact.phone_number;
payload.first_name = msg.contact.first_name;
if (msg.contact.last_name) payload.last_name = msg.contact.last_name;
}
else {
await SendMessage(dest, `Unexpected data, reply not sent.`, pc_d, s_d);
return;
}
if (!msg.location && !msg.contact) {
payload[method2] = fileId;
}
await fetch(url + `${method}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
await SendMessage(dest, `${methodr} sent to ${chatId}.`, pc_d, s_d, chatId);
};
/**
* Handles the incoming HTTP request.
*
* @param {Request} request - The HTTP request object representing the incoming request.
* @returns {Promise<Response>} A Promise that resolves to a Response object, which will be returned as the response to the incoming request.
*/
async function handleRequest(request) {
const secret_token = request.headers.get("X-Telegram-Bot-Api-Secret-Token");
if (secret_token !== SECRET_TOKEN) {
return new Response("Authentication Failed.", { status: 403 });
}
if (request.method === "POST") {
const payload = await request.json();
if (payload.message) {
const chatId = payload.message.chat.id.toString();
const text = payload.message.text || "";
if (chatId === DESTINATION) {
let command = text.split(" ")[0];
if ('reply_to_message' in payload.message) {
// Send reply, get first the original sender id
let infoSender = payload.message.reply_to_message.text || "";
let infoArr = infoSender.split(" ");
const senderId = infoArr[0];
if (senderId && Number(senderId) > 0) {
if (text) {
await SendMessage(senderId, text, pc_user, silent_user);
await SendMessage(DESTINATION, `Reply sent to ${senderId}.`, pc_dest, silent_dest, senderId);
}
else {
await SendMedia(payload.message, DESTINATION, senderId, pc_user, pc_dest, silent_dest);
}
}
else {
await SendMessage(DESTINATION, "Reply only to messages starting with an user ID.", pc_dest, silent_dest);
}
}
else if (command === "/start") {
await SendMessage(DESTINATION, "Hello, chief!", pc_dest, silent_dest);
}
else if (command === "/block") {
await SendMessage(DESTINATION, `Edit it from the code because the worker is stateless and
after some time it will clear all the state variable edits. Otherwise you can use the DB version.`, pc_dest, silent_dest);
}
else if(command === "/unblock") {
await SendMessage(DESTINATION, `Edit it from the code because the worker is stateless and
after some time it will clear all the state variable edits. Otherwise you can use the DB version.`, pc_dest, silent_dest);
}
else if (command === "/suspend") {
await SendMessage(DESTINATION, `Edit it from the code because the worker is stateless and
after some time it will clear all the state variable edits. Otherwise you can use the DB version.`, pc_dest, silent_dest);
}
else if (command === "/unsuspend") {
await SendMessage(DESTINATION, `Edit it from the code because the worker is stateless and
after some time it will clear all the state variable edits. Otherwise you can use the DB version.`, pc_dest, silent_dest);
}
else if (command === "/help") {
await SendMessage(DESTINATION, `User guide: ${user_guide}. FAQ: ${faq}.`, pc_dest, silent_dest);
}
else if (command === "/blocked") {
let blocked_str = 'blocked = ["' + blocked.join('", "') + '"]';
await SendMessage(DESTINATION, blocked_str, pc_dest, silent_dest);
}
else if (command === "/pin") {
await SendMessage(DESTINATION, `Edit it from the code because the worker is stateless and
after some time it will clear all the state variable edits. Otherwise you can use the DB version.`, pc_dest, silent_dest);
}
else if (command === "/unpin") {
await SendMessage(DESTINATION, `Edit it from the code because the worker is stateless and
after some time it will clear all the state variable edits. Otherwise you can use the DB version.`, pc_dest, silent_dest);
}
else if (command === "/show") {
let infoBlock = text.split(" ");
if (infoBlock[1] && Number(infoBlock[1]) > 0) {
await SendMessage(DESTINATION, `User ${infoBlock[1]}.`, pc_dest, silent_dest, infoBlock[1]);
}
else {
await SendMessage(DESTINATION, "Invalid User ID.", pc_dest, silent_dest);
}
}
else if (command === "/pcuser") {
await SendMessage(DESTINATION, `Edit it from the code because the worker is stateless and
after some time it will clear all the state variable edits. Otherwise you can use the DB version.`, pc_dest, silent_dest);
}
else if (command === "/pcdest") {
await SendMessage(DESTINATION, `Edit it from the code because the worker is stateless and
after some time it will clear all the state variable edits. Otherwise you can use the DB version.`, pc_dest, silent_dest);
}
else if (command === "/silentuser") {
await SendMessage(DESTINATION, `Edit it from the code because the worker is stateless and
after some time it will clear all the state variable edits. Otherwise you can use the DB version.`, pc_dest, silent_dest);
}
else if (command === "/silentdest") {
await SendMessage(DESTINATION, `Edit it from the code because the worker is stateless and
after some time it will clear all the state variable edits. Otherwise you can use the DB version.`, pc_dest, silent_dest);
}
else if (payload.message.entities && payload.message.entities.length > 0 && payload.message.entities[0].type === "bot_command") {
await SendMessage(DESTINATION, `Hey chief! Invalid command, check the User guide at ${user_guide}.`, pc_dest, silent_dest);
}
else if (pinned_usr) {
if (text) {
await SendMessage(pinned_usr, text, pc_user, silent_user);
await SendMessage(DESTINATION, `Reply sent to ${pinned_usr}`, pc_dest, silent_dest, pinned_usr);
}
else {
await SendMedia(payload.message, DESTINATION, pinned_usr, pc_user, pc_dest, silent_user, silent_dest);
}
}
else {
await SendMessage(DESTINATION, `Hey chief! Invalid command, check the User guide at ${user_guide}.`, pc_dest, silent_dest);
}
}
else if (!blocked.includes(chatId) && suspended) {
await SendMessage(chatId, `${susp_info} {custom_susp}`);
}
else if (!blocked.includes(chatId)) {
const first_name = payload.message.from.first_name;
const last_name = payload.message.from.last_name;
const username = payload.message.from.username;
let user = first_name;
if (last_name) {
user = user + " " + last_name;
}
let info = chatId + " " + user;
let extraInfo = `language_code=${payload.message.from.language_code} is_bot=${payload.message.from.is_bot}`;
if (text === "/start") {
await SendMessage(chatId, `Hello, ${user}!`, pc_user, silent_user);
await SendMessage(DESTINATION, username ? `${info} @${username} ${extraInfo} started the bot.` : `${info} ${extraInfo} started the bot.`, pc_dest, silent_dest, chatId);
}
else if (text === "/help") {
await SendMessage(chatId, `This bot forward all messages you send to ${nick}. Through this bot, ${nick} can reply you.`, pc_user, silent_user);
await SendMessage(DESTINATION, username ? `${info} @${username} ${extraInfo} typed /help.` : `${info} ${extraInfo} typed /help.`, pc_dest, silent_dest, chatId);
}
else {
await SendMessage(chatId, `Message sent to ${nick}.`, pc_user, silent_user);
await ForwardMessage(DESTINATION, chatId, payload.message.message_id, pc_dest, silent_dest);
await SendMessage(DESTINATION, username ? `${info} @${username} ${extraInfo}.` : `${info} ${extraInfo}.`, pc_dest, silent_dest, chatId);
}
}
}
}
return new Response("OK", { status: 200 });
};