Skip to content

Commit 0d9e000

Browse files
committed
pošleme tomovi toma
1 parent a74c05a commit 0d9e000

File tree

2 files changed

+80
-23
lines changed

2 files changed

+80
-23
lines changed

src/tg/bot.deno.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import {
88
BOT_TOKEN,
99
DOMAIN,
1010
genRandomToken,
11+
řekniTomovi,
12+
getFileBase64,
1113
MAIN_CHAT_ID,
12-
PRINTER_TOKEN,
1314
STICEKR_SET_NAME,
1415
STICEKR_SET_OWNER,
1516
tgCall,
@@ -169,7 +170,7 @@ export async function* handleTgUpdate(data: any) {
169170
});
170171
}
171172

172-
const chungus_balls_extended = "No tak kde je to tvoje";
173+
const chungus_balls_extended = "No tak kde je to tvoje ";
173174
if (
174175
data.message.chat.id === MAIN_CHAT_ID &&
175176
!(data.message.message_id % 100000)
@@ -338,27 +339,8 @@ Be grateful for your abilities and your incredible success and your considerable
338339

339340
const trig = "/řekni_tomovi";
340341
if (text.startsWith(trig) && data.message.chat.id === MAIN_CHAT_ID) {
341-
const response = await fetch("https://printomat.slama.dev/submit", {
342-
headers: {
343-
"Content-Type": "application/x-www-form-urlencoded",
344-
},
345-
body: new URLSearchParams({
346-
message: `${data.message.from.first_name} říká: ${
347-
text.slice(trig.length).trim()
348-
}`,
349-
image: "",
350-
token: PRINTER_TOKEN,
351-
}).toString(),
352-
method: "POST",
353-
});
354-
const txt = await response.text();
355-
await tgCall({
356-
chat_id: data.message.chat.id,
357-
reply_to_message_id: data.message.message_id,
358-
text: `Tom říká (${response.status}): ${
359-
/<p>(.*?)<\/p>/.exec(txt)?.[1] ?? txt
360-
}`,
361-
});
342+
const image = await getFileBase64(data.message.reply_to_message ?? data.message);
343+
řekniTomovi(data.message.from.first_name, text.slice(trig.length).trim(), image, data.message.chat.id);
362344
}
363345

364346
if (data.message.from.id == (13*(2*29811374 + 1)) &&

src/tg/utils.deno.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// deno-lint-ignore-file no-explicit-any
22
// The authors disclaim copyright to this source code (they are ashamed to
33
// admit they wrote it)
4+
import { encodeBase64 } from "jsr:@std/encoding";
45

56
export const BOT_TOKEN = Deno.env.get("TG_BOT_TOKEN");
67
export const MAIN_CHAT_ID = parseInt(Deno.env.get("TG_MAIN_CHAT_ID")!);
@@ -104,3 +105,77 @@ export async function tgCall(
104105
}
105106
return req;
106107
}
108+
109+
export async function řekniTomovi(
110+
sender: string,
111+
message: string,
112+
image: string = "",
113+
chat_id: number = MAIN_CHAT_ID,
114+
) {
115+
if (sender) {
116+
message = `${sender} říká: ${message}`;
117+
}
118+
119+
const response = await fetch("https://printomat.slama.dev/submit", {
120+
headers: {
121+
"Content-Type": "application/x-www-form-urlencoded",
122+
},
123+
body: new URLSearchParams({
124+
message: message,
125+
image: image,
126+
token: PRINTER_TOKEN,
127+
}).toString(),
128+
method: "POST",
129+
});
130+
const txt = await response.text();
131+
if (chat_id) {
132+
await tgCall({
133+
chat_id: chat_id,
134+
reply_to_message_id: chat_id,
135+
text: `Tom říká (${response.status}): ${
136+
/<p>(.*?)<\/p>/.exec(txt)?.[1] ?? txt
137+
}`,
138+
});
139+
}
140+
}
141+
142+
export async function getFileBase64(message: any, maxSize = Infinity) {
143+
function* files() {
144+
yield message.sticker?.thumb;
145+
yield message.sticker?.thumbnail;
146+
yield message.sticker;
147+
yield message.video_note?.thumb;
148+
yield message.video_note?.thumbnail;
149+
yield message.video_note;
150+
yield message.video?.thumb;
151+
yield message.video?.thumbnail;
152+
yield message.video;
153+
yield message.document;
154+
if (message.photo) {
155+
yield* message.photo;
156+
}
157+
}
158+
159+
let bestCandidate = undefined;
160+
for (const file of files()) {
161+
if (
162+
!file || file.file_size > maxSize ||
163+
file.file_size < bestCandidate?.file_size
164+
) continue;
165+
bestCandidate = file;
166+
}
167+
168+
if (!bestCandidate) {
169+
return;
170+
}
171+
172+
const fileData = await tgCall(
173+
{ file_id: bestCandidate.file_id },
174+
"getFile",
175+
);
176+
const response = await fetch(
177+
`https://api.telegram.org/file/bot${BOT_TOKEN}/${fileData.result.file_path}`,
178+
);
179+
const fileContent = new Uint8Array(await response.arrayBuffer());
180+
return encodeBase64(fileContent);
181+
}

0 commit comments

Comments
 (0)