tarasyarema-tgwebhookhandler.web.val.run
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
import { blob } from "https://esm.town/v/std/blob";
import { telegramSendMessage } from "https://esm.town/v/vtdocs/telegramSendMessage?v=5";
import process from "node:process";
const DB_KEY = "telegram_Chotillo";
const LS = "/ls";
const X = "/x";
const X_ = X + " ";
const initBlob = async () => {
let exist = await blob.getJSON(DB_KEY);
if (!exist) {
await blob.setJSON(DB_KEY, []);
exist = [];
}
return exist;
};
const send = async (chatId: number, msg: string) => {
await telegramSendMessage(
process.env.telegramBotToken,
{ chat_id: chatId, text: msg },
);
};
export const telegramWebhookEchoMessage = async (req: Request) => {
const db: string[] = await initBlob();
// Verify this webhook came from our bot
if (
req.headers.get("x-telegram-bot-api-secret-token")
!== process.env.telegramWebhookSecret
) {
return new Response(undefined, { status: 401 });
}
// Echo back the user's message
const body = await req.json();
const text: string = body.message.text;
const chatId: number = body.message.chat.id;
if (text.startsWith(LS)) {
await send(chatId, db.join("\n"));
return Response.json("ok");
}
if (!text.startsWith(X_)) {
return Response.json("ok");
}
const cmd = text.split(X_)[1];
await blob.setJSON(DB_KEY, [...db, cmd]);
await send(chatId, `Processed ${cmd.length} bytes!`);
return Response.json("ok");
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
November 29, 2023