Last active
September 4, 2022 14:58
-
-
Save SaDi-BRo/3c054927478159b4b2ba4a89e9af38d3 to your computer and use it in GitHub Desktop.
This is about how to do webhook on deno grammy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Bot, webhookCallback } from 'https://deno.land/x/grammy@v1.11.0/mod.ts'; | |
import { serve } from 'https://deno.land/std@0.154.0/http/server.ts'; | |
import 'https://deno.land/x/dotenv@v3.2.0/load.ts'; | |
const bot = new Bot(Deno.env.get('BOT_TOKEN')!); | |
const handleUpdate = webhookCallback(bot, 'std/http'); | |
bot.on('message', (ctx) => ctx.reply('Hi there!')); | |
serve(async (req) => { | |
if (req.method == 'POST') { | |
const url = new URL(req.url); | |
if (url.pathname.slice(1) == bot.token) { | |
try { | |
return await handleUpdate(req); | |
} catch (err) { | |
console.error(err); | |
} | |
} | |
} | |
return new Response(); | |
}); | |
Deno.env.get('MODE') === 'development' && bot.start(); | |
Deno.env.get('MODE') === 'production' && | |
bot.api.setWebhook( | |
Deno.env.get('WEBHOOK_URL')! + '/' + Deno.env.get('BOT_TOKEN')! | |
); | |
// You need to have BOT_TOKEN, MODE and WEBHOOK_URL environment variables |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment