-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.ts
30 lines (21 loc) · 902 Bytes
/
index.ts
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
// Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.
import { serve } from "std/server";
console.log(`Function "telegram-bot" up and running!`);
import { Bot, webhookCallback } from "grammy";
const bot = new Bot(Deno.env.get("BOT_TOKEN") || "");
bot.command("start", (ctx) => ctx.reply("Welcome! Up and running."));
bot.command("ping", (ctx) => ctx.reply(`Pong! ${new Date()} ${Date.now()}`));
const handleUpdate = webhookCallback(bot, "std/http");
serve(async (req) => {
try {
const url = new URL(req.url);
if (url.searchParams.get("secret") !== Deno.env.get("FUNCTION_SECRET")) {
return new Response("not allowed", { status: 405 });
}
return await handleUpdate(req);
} catch (err) {
console.error(err);
}
});