Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
import axios from "axios";
import { createResponse, useHTTP } from "valtown";
const BOT_TOKEN = Deno.env.get("TELEGRAM_TOKEN");
const TELEGRAM_API_URL = `https://api.telegram.org/bot${BOT_TOKEN}`;
useHTTP(async (req, res) => {
try {
const body = req.body;
if (body && body.message && body.message.text) {
const chatId = body.message.chat.id;
const text = body.message.text;
// Send the same message back to the user
await sendMessage(chatId, text);
}
res.send(createResponse(200, "OK"));
} catch (error) {
console.error("Error handling request:", error);
res.send(createResponse(500, "Internal Server Error"));
}
});
async function sendMessage(chatId, text) {
try {
const url = `${TELEGRAM_API_URL}/sendMessage`;
const payload = {
chat_id: chatId,
text: text,
};
await axios.post(url, payload);
} catch (error) {
console.error("Error sending message:", error);
}
}
yawnxyz-telegramwebhookechomessage.web.val.run
August 23, 2024