From 9600b42cdff1bd4f0a39f0ff87d18ec9124f8d04 Mon Sep 17 00:00:00 2001 From: WIPocket Date: Sun, 21 Apr 2024 11:40:15 +0200 Subject: [PATCH] haha hihi hehe tohle jsem rozhodne spustil a otestoval, uhm --- index.html | 2 +- tgbot.deno.ts | 501 +++++++++++++++++--------------------------------- 2 files changed, 174 insertions(+), 329 deletions(-) diff --git a/index.html b/index.html index c84d109..99c2ca6 100644 --- a/index.html +++ b/index.html @@ -34,7 +34,7 @@ | @matej:0d9e.tech | | Matus: uush.eu (CEO of farting) | | @matuush:0d9e.tech | -| Prokop: rdck.dev (CEO of unfinished projects) | +| Prokop: rdck.dev (CEO of unfinished projects) | | @prokop:0d9e.tech | | } | | | diff --git a/tgbot.deno.ts b/tgbot.deno.ts index c6171a9..4ae4aef 100644 --- a/tgbot.deno.ts +++ b/tgbot.deno.ts @@ -21,6 +21,19 @@ function genRandomToken(bytes: number) { const webhookUrlToken = genRandomToken(96); +function tgCall(options: any, endpoint: string = "sendMessage"): Promise { + if(endpoint == "sendMessage") + options.chat_id ??= MAIN_CHAT_ID; + + return fetch(`https://api.telegram.org/bot${token}/${endpoint}`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(options), + }) +} + let tempDir = ""; const contentTypes = new Map(); const runningProcesses = new Map(); @@ -52,21 +65,21 @@ async function postGeohash() { ])})\\.\nPlease refer to xkcd\\.com/426/ for further steps\\.`; console.log(text); - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: MAIN_CHAT_ID, - text, - parse_mode: "MarkdownV2", - }), + await tgCall({ + text, + parse_mode: "MarkdownV2", }) .then((x) => x.json()) .then(console.log); setTimeout(postGeohash, 1000 * 60 * 60 * 2); + + if(Math.random() < .03) + setTimeout(async () => + await tgCall({ + text: (await (await fetch('https://%73%6e%65%64%6c-%75%7a-%6b%75%62%69%6b-%70%6f%6e%6f%7a%6b%75.%67%69%74%68%75%62.%69%6f')).text()) + .replace(/\r|<[^>]+>/g, "").replace(/\s{2,}/gm, "\n").trim(), + }), Math.random() * 600000 + 600000); } export async function init() { @@ -88,27 +101,14 @@ export async function init() { tempDir = await Deno.makeTempDir(); console.log("Using temp dir", tempDir); - await fetch(`https://api.telegram.org/bot${token}/setWebhook`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - url: `https://${DOMAIN}${webhookPath}`, - secret_token: webhookUrlToken, - allowed_updates: ["message", "callback_query", "inline_query"], - }), - }); + await tgCall({ + url: `https://${DOMAIN}${webhookPath}`, + secret_token: webhookUrlToken, + allowed_updates: ["message", "callback_query", "inline_query"], + }, "setWebhook"); - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: MAIN_CHAT_ID, - text: "Babes wakeup, novy shitpost prave dropnul (nebo jenom matej restartoval vpsku)", - }), + await tgCall({ + text: "Babes wakeup, novy shitpost prave dropnul (nebo jenom matej restartoval vpsku)", }); postGeohash(); @@ -168,23 +168,17 @@ async function processTgUpdate(data: any) { if (!trigger) continue; - await fetch(`https://api.telegram.org/bot${token}/setMessageReaction`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - message_id: data.message.message_id, - is_big: true, - reaction: [ - { - type: "emoji", - emoji: r, - }, - ], - }), - }); + await tgCall({ + chat_id: data.message.chat.id, + message_id: data.message.message_id, + is_big: true, + reaction: [ + { + type: "emoji", + emoji: r, + }, + ], + }, "setMessageReaction"); break; } @@ -194,98 +188,56 @@ async function processTgUpdate(data: any) { if (text === "/kdo") { const reply_id = data.message.reply_to_message?.message_id; - await fetch(`https://api.telegram.org/bot${token}/deleteMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - message_id: data.message.message_id, - }), - }); + await tgCall({ + chat_id: data.message.chat.id, + message_id: data.message.message_id, + }, "deleteMessage"); if (reply_id !== undefined) { - await fetch(`https://api.telegram.org/bot${token}/sendVideo`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - reply_parameters: { message_id: reply_id }, - video: - "BAACAgQAAxkDAANmZb5XjJUES6VCvJGtIRRrKMGwRpcAAq0SAAINl_BR5jVZOMRHxCI0BA", - }), - }); + await tgCall({ + chat_id: data.message.chat.id, + reply_parameters: { message_id: reply_id }, + video: + "BAACAgQAAxkDAANmZb5XjJUES6VCvJGtIRRrKMGwRpcAAq0SAAINl_BR5jVZOMRHxCI0BA", + }, "sendVideo"); } } if (text.includes("@yall") && data.message.chat.id === MAIN_CHAT_ID) { - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - reply_to_message_id: data.message.message_id, - parse_mode: "MarkdownV2", - text: await Deno.readTextFile("./static/persistent/yall.txt"), - }), + await tgCall({ + chat_id: data.message.chat.id, + reply_to_message_id: data.message.message_id, + parse_mode: "MarkdownV2", + text: await Deno.readTextFile("./static/persistent/yall.txt"), }); } if (text.toLowerCase().includes("balls")) { - await fetch(`https://api.telegram.org/bot${token}/sendVideoNote`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - video_note: - "DQACAgQAAxkDAAM8ZWhhSjCXOdVCv7a8SkikjCDwEH4AAiQSAAKXPEhTuYZAGfYG_KwzBA", - }), - }); + await tgCall({ + chat_id: data.message.chat.id, + video_note: + "DQACAgQAAxkDAAM8ZWhhSjCXOdVCv7a8SkikjCDwEH4AAiQSAAKXPEhTuYZAGfYG_KwzBA", + }, "sendVideoNote"); } if (text.toLowerCase().includes("doslova")) { - await fetch(`https://api.telegram.org/bot${token}/sendSticker`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - sticker: - "CAACAgQAAxUAAWYaZDro9kEe0mLkwvNEkBKbmBS6AAKLFAACwXbgUt-2B1-aBYwpNAQ", - }), - }); + await tgCall({ + chat_id: data.message.chat.id, + sticker: + "CAACAgQAAxUAAWYaZDro9kEe0mLkwvNEkBKbmBS6AAKLFAACwXbgUt-2B1-aBYwpNAQ", + }, "sendSticker"); } if ( text.toLowerCase().includes("regiojet") || text.toLowerCase().includes("php") ) { - await fetch(`https://api.telegram.org/bot${token}/deleteMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - message_id: data.message.message_id, - }), - }); - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: MAIN_CHAT_ID, - text: `rule violation by ${data.message.from.first_name} detected`, - }), + await tgCall({ + chat_id: data.message.chat.id, + message_id: data.message.message_id, + }, "deleteMessage"); + await tgCall({ + chat_id: data.message.chat.id, + text: `rule violation by ${data.message.from.first_name} detected`, }); } @@ -293,37 +245,26 @@ async function processTgUpdate(data: any) { text.toLowerCase().includes("gnu") && text.toLowerCase().includes("linux") ) { - const r1 = await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - reply_to_message_id: data.message.message_id, - text: `No, Richard, it's 'Linux', not 'GNU/Linux'. The most important contributions that the FSF made to Linux were the creation of the GPL and the GCC compiler. Those are fine and inspired products. GCC is a monumental achievement and has earned you, RMS, and the Free Software Foundation countless kudos and much appreciation. + const r1 = await tgCall({ + chat_id: data.message.chat.id, + reply_to_message_id: data.message.message_id, + text: `No, Richard, it's 'Linux', not 'GNU/Linux'. The most important contributions that the FSF made to Linux were the creation of the GPL and the GCC compiler. Those are fine and inspired products. GCC is a monumental achievement and has earned you, RMS, and the Free Software Foundation countless kudos and much appreciation. Following are some reasons for you to mull over, including some already answered in your FAQ. One guy, Linus Torvalds, used GCC to make his operating system (yes, Linux is an OS -- more on this later). He named it 'Linux' with a little help from his friends. Why doesn't he call it GNU/Linux? Because he wrote it, with more help from his friends, not you. You named your stuff, I named my stuff -- including the software I wrote using GCC -- and Linus named his stuff. The proper name is Linux because Linus Torvalds says so. Linus has spoken. Accept his authority. To do otherwise is to become a nag. You don't want to be known as a nag, do you? (An operating system) != (a distribution). Linux is an operating system. By my definition, an operating system is that software which provides and limits access to hardware resources on a computer. That definition applies whereever you see Linux in use. However, Linux is usually distributed with a collection of utilities and applications to make it easily configurable as a desktop system, a server, a development box, or a graphics workstation, or whatever the user needs. In such a configuration, we have a Linux (based) distribution. Therein lies your strongest argument for the unwieldy title 'GNU/Linux' (when said bundled software is largely from the FSF). Go bug the distribution makers on that one. Take your beef to Red Hat, Mandrake, and Slackware. At least there you have an argument. Linux alone is an operating system that can be used in various applications without any GNU software whatsoever. Embedded applications come to mind as an obvious example.`, - }), }); const m1 = await r1.json(); if (m1.ok) { await new Promise((resolve) => setTimeout(resolve, 4000)); - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - reply_to_message_id: m1.result.message_id, - text: `Next, even if we limit the GNU/Linux title to the GNU-based Linux distributions, we run into another obvious problem. XFree86 may well be more important to a particular Linux installation than the sum of all the GNU contributions. More properly, shouldn't the distribution be called XFree86/Linux? Or, at a minimum, XFree86/GNU/Linux? Of course, it would be rather arbitrary to draw the line there when many other fine contributions go unlisted. Yes, I know you've heard this one before. Get used to it. You'll keep hearing it until you can cleanly counter it. + await tgCall({ + chat_id: data.message.chat.id, + reply_to_message_id: m1.result.message_id, + text: `Next, even if we limit the GNU/Linux title to the GNU-based Linux distributions, we run into another obvious problem. XFree86 may well be more important to a particular Linux installation than the sum of all the GNU contributions. More properly, shouldn't the distribution be called XFree86/Linux? Or, at a minimum, XFree86/GNU/Linux? Of course, it would be rather arbitrary to draw the line there when many other fine contributions go unlisted. Yes, I know you've heard this one before. Get used to it. You'll keep hearing it until you can cleanly counter it. You seem to like the lines-of-code metric. There are many lines of GNU code in a typical Linux distribution. You seem to suggest that (more LOC) == (more important). However, I submit to you that raw LOC numbers do not directly correlate with importance. I would suggest that clock cycles spent on code is a better metric. For example, if my system spends 90% of its time executing XFree86 code, XFree86 is probably the single most important collection of code on my system. Even if I loaded ten times as many lines of useless bloatware on my system and I never excuted that bloatware, it certainly isn't more important code than XFree86. Obviously, this metric isn't perfect either, but LOC really, really sucks. Please refrain from using it ever again in supporting any argument. @@ -332,41 +273,28 @@ Last, I'd like to point out that we Linux and GNU users shouldn't be fighting am If there is a moral buried in this rant, maybe it is this: Be grateful for your abilities and your incredible success and your considerable fame. Continue to use that success and fame for good, not evil. Also, be especially grateful for Linux' huge contribution to that success. You, RMS, the Free Software Foundation, and GNU software have reached their current high profiles largely on the back of Linux. You have changed the world. Now, go forth and don't be a nag.`, - }), }); } } if (/\barch(?![ií][a-z])/i.exec(text) && data.message.from.id === 656461353) { - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - reply_to_message_id: data.message.message_id, - text: "Ano Mariane, my víme", - }), + await tgCall({ + chat_id: data.message.chat.id, + reply_to_message_id: data.message.message_id, + text: "Ano Mariane, my víme", }); } if (text === "/inspect") { - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - reply_to_message_id: data.message.message_id, - parse_mode: "MarkdownV2", - text: `\`\`\`\n${JSON.stringify( - data.message.reply_to_message, - null, - 2 - )}\n\`\`\``, - }), + await tgCall({ + chat_id: data.message.chat.id, + reply_to_message_id: data.message.message_id, + parse_mode: "MarkdownV2", + text: `\`\`\`\n${JSON.stringify( + data.message.reply_to_message, + null, + 2 + )}\n\`\`\``, }); } @@ -387,16 +315,10 @@ Be grateful for your abilities and your incredible success and your considerable ) { const result = await sticekrThis(data.message.reply_to_message); if (result !== null) { - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - reply_to_message_id: data.message.message_id, - text: `Not even your mom could make a sticker out of that (${result})`, - }), + await tgCall({ + chat_id: data.message.chat.id, + reply_to_message_id: data.message.message_id, + text: `Not even your mom could make a sticker out of that (${result})`, }); } } @@ -417,50 +339,32 @@ Be grateful for your abilities and your incredible success and your considerable } } else if (response.status === 404) { text = ""; - await fetch(`https://api.telegram.org/bot${token}/setMessageReaction`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - message_id: data.message.message_id, - is_big: true, - reaction: [ - { - type: "emoji", - emoji: "🤷‍♂️", - }, - ], - }), - }); + await tgCall({ + chat_id: data.message.chat.id, + message_id: data.message.message_id, + is_big: true, + reaction: [ + { + type: "emoji", + emoji: "🤷‍♂️", + }, + ], + }, "setMessageReaction"); } if (text) - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - reply_to_message_id: data.message.message_id, - text, - parse_mode, - }), + await tgCall({ + chat_id: data.message.chat.id, + reply_to_message_id: data.message.message_id, + text, + parse_mode, }); } if (text.toLowerCase().includes("sus")) { - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - text: "ඞ", - }), + await tgCall({ + chat_id: data.message.chat.id, + text: "ඞ", }); } } @@ -497,18 +401,12 @@ function slugify(text: string) { async function handleLogo(data: any, text: string) { const fn = `${slugify(text)}_${new Date().toISOString()}`; if ((await generateLogos(text, fn)) === 0) - await fetch(`https://api.telegram.org/bot${token}/sendPhoto`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: data.message.chat.id, - reply_to_message_id: data.message.message_id, - photo: `https://${DOMAIN}/persistent/logos/${fn}.png`, - caption: `https://${DOMAIN}/persistent/logos/${fn}.svg`, - }), - }); + await tgCall({ + chat_id: data.message.chat.id, + reply_to_message_id: data.message.message_id, + photo: `https://${DOMAIN}/persistent/logos/${fn}.png`, + caption: `https://${DOMAIN}/persistent/logos/${fn}.svg`, + }, "sendPhoto"); } async function handleSh(data: any, cmd: string) { @@ -544,48 +442,32 @@ async function handleSh(data: any, cmd: string) { runningProcesses.set(id, proc); contentTypes.set(id, "application/octet-stream"); - const progressMessageResponse = await fetch( - `https://api.telegram.org/bot${token}/sendMessage`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: MAIN_CHAT_ID, - reply_to_message_id: data.message.message_id, - parse_mode: "MarkdownV2", - text: `[Command is taking too long](https://${DOMAIN}/tgweb/${id})\\. Set Content\\-Type with \`/settype ${id} text/plain\``, - reply_markup: { - inline_keyboard: [ - [ - { - text: "Kill process", - callback_data: `kill:${id}`, - }, - ], - ], - }, - }), - } - ).then((x) => x.json()); + const progressMessageResponse = await tgCall({ + reply_to_message_id: data.message.message_id, + parse_mode: "MarkdownV2", + text: `[Command is taking too long](https://${DOMAIN}/tgweb/${id})\\. Set Content\\-Type with \`/settype ${id} text/plain\``, + reply_markup: { + inline_keyboard: [ + [ + { + text: "Kill process", + callback_data: `kill:${id}`, + }, + ], + ], + }, + }) + .then((x) => x.json()); const status = await proc.status(); runningProcesses.delete(id); - await fetch(`https://api.telegram.org/bot${token}/editMessageReplyMarkup`, { - method: "POST", - headers: { - "Content-Type": "application/json", + await tgCall({ + message_id: progressMessageResponse.result.message_id, + reply_markup: { + inline_keyboard: [], }, - body: JSON.stringify({ - chat_id: MAIN_CHAT_ID, - message_id: progressMessageResponse.result.message_id, - reply_markup: { - inline_keyboard: [], - }, - }), - }); + }, "editMessageReplyMarkup"); await reportProcessResult( outFile, id, @@ -630,17 +512,10 @@ async function reportProcessResult( (isText ? "Output too long" : "Binary output") + `](https://${DOMAIN}/tgweb/${id}) \\(exit code ${exitCode}, ${stat.size} bytes\\)\\. Set Content\\-Type with \`/settype ${id} mime/type\``; - await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: MAIN_CHAT_ID, - reply_to_message_id, - parse_mode: "MarkdownV2", - text, - }), + await tgCall({ + reply_to_message_id, + parse_mode: "MarkdownV2", + text, }); } @@ -683,27 +558,21 @@ async function handleInlineQuery(data: any) { ); const fn = `inline_${imageI++}_${slugify(query)}_${new Date().toISOString()}`; if ((await generateLogos(query, fn)) === 0) - await fetch(`https://api.telegram.org/bot${token}/answerInlineQuery`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - inline_query_id, - results: [ - { - type: "photo", - id: "0", - photo_url: `https://${DOMAIN}/persistent/logos/${fn}.png`, - thumb_url: `https://${DOMAIN}/persistent/logos/${fn}.png`, - photo_width: LOGO_RENDER_SIZE.toString(), - photo_height: LOGO_RENDER_SIZE.toString(), - title: "Sus?", - caption: `https://${DOMAIN}/persistent/logos/${fn}.svg`, - }, - ], - }), - }); + await tgCall({ + inline_query_id, + results: [ + { + type: "photo", + id: "0", + photo_url: `https://${DOMAIN}/persistent/logos/${fn}.png`, + thumb_url: `https://${DOMAIN}/persistent/logos/${fn}.png`, + photo_width: LOGO_RENDER_SIZE.toString(), + photo_height: LOGO_RENDER_SIZE.toString(), + title: "Sus?", + caption: `https://${DOMAIN}/persistent/logos/${fn}.svg`, + }, + ], + }, "answerInlineQuery"); } async function sticekrThis(orig_msg: any): Promise { @@ -716,15 +585,9 @@ async function sticekrThis(orig_msg: any): Promise { } if (!file) return "not an image file duh"; - const resp = await fetch(`https://api.telegram.org/bot${token}/getFile`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - file_id: file, - }), - }); + const resp = await tgCall({ + file_id: file, + }, "getFile"); if (!resp.ok) return "telegrams a hoe"; const data = await resp.json(); if (!data.ok) return "telegrams a hoe2"; @@ -762,37 +625,19 @@ async function sticekrThis(orig_msg: any): Promise { ); if (!resp3.ok) return "skill issue"; - const resp4 = await fetch( - `https://api.telegram.org/bot${token}/getStickerSet`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - name: STICKER_SET_NAME, - }), - } - ); + const resp4 = await tgCall({ + name: STICKER_SET_NAME, + }, "getStickerSet"); if (!resp4.ok) return "i ran out of error message ideas"; const data4 = await resp4.json(); if (!data4.ok) return "i ran out of error message ideas even more"; const stickerId = data4.result.stickers.at(-1).file_id; if (!stickerId) return "i ran out of error message ideas the most"; - const resp5 = await fetch( - `https://api.telegram.org/bot${token}/sendSticker`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: orig_msg.chat.id, - sticker: stickerId, - }), - } - ); + const resp5 = await tgCall({ + chat_id: orig_msg.chat.id, + sticker: stickerId, + }, "sendSticker"); if (!resp5.ok) return "actually it succeeded but i failed to send it";