Public
Script
October 23, 2023
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
import { tgEchoCommand } from "https://esm.town/v/smalldogenergy/tgEchoCommand";
import { tgWeatherCommand } from "https://esm.town/v/smalldogenergy/tgWeatherCommand";
import { tgUpdates } from "https://esm.town/v/smalldogenergy/tgUpdates";
import process from "node:process";
export async function tgWebhook(req, res) {
const botSecretToken = req.get("X-Telegram-Bot-Api-Secret-Token");
if (botSecretToken !== process.env.tgBotSecretToken) {
res.status(401).json({ ok: false, description: "Unauthorized" });
return;
}
const payload: TgUpdatePayload = req.body;
if (payload.message) {
tgUpdates.push(payload);
const args = {
chatId: payload.message.chat.id,
text: payload.message.text,
languageCode: payload.message.from.language_code,
};
if (args.text.startsWith("/weather")) {
tgWeatherCommand(args);
} else {
tgEchoCommand(args);
}
}
res.status(200).json({ ok: true });
}
interface TgUpdatePayload {
update_id: number;
message?: TgMessage;
}
interface TgMessage {
from: { language_code: "string" };
chat: { id: number };
text: string;
}
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!
October 23, 2023