grammyjs / website

The grammY documentation website.
https://grammy.dev
MIT License
46 stars 109 forks source link

Vercel deployment of bot with webhooks is not working #1110

Closed shahbaz4783 closed 3 months ago

shahbaz4783 commented 3 months ago

I deployed my telgram on vercel by following the guide on grammy. when i send /start to bot is isnt sending any response.

Here is the code of the bot

import { Bot, webhookCallback } from 'grammy';
import express from 'express';

const bot = new Bot(process.env.BOT_TOKEN!);

bot.command('start', (ctx) => ctx.reply(`Welcome ${ctx.from?.first_name}`));
bot.command('help', (ctx) => ctx.reply('Send /start to start the bot.'));

const app = express();
app.use(express.json());

// Set up the webhook
const secretPath = `/webhook/${bot.token}`;
app.use(secretPath, webhookCallback(bot, 'express'));

const PORT = process.env.PORT || 3000;
app.listen(PORT, async () => {
    console.log(`Server is running on port ${PORT}`);
    // Set the webhook
    await bot.api.setWebhook(process.env.URL + secretPath);
});

i also got success message from webhook

{
"ok": true,
"result": true,
"description": "Webhook was set"
}
KnorpelSenf commented 3 months ago

You need to provide more info.

shahbaz4783 commented 3 months ago

You need to provide more info.

  • What is the output of getWebhookInfo?
  • Does your server receive the requests from Telegram?
  • Does express receive the requests from Telegram?
  • Does grammY receive the updates (but fails to call the right handlers)?
  • Are there any errors?
  • If you enable DEBUG mode, what is the output?

Output of webhookinfo

{
"ok": true,
"result": {
"url": "https://telegram-bot-seven-delta.vercel.app/api/bot",
"has_custom_certificate": false,
"pending_update_count": 6,
"last_error_date": 1723100098,
"last_error_message": "Wrong response from the webhook: 404 Not Found",
"max_connections": 40,
"ip_address": "76.76.21.61"
}
}

It works fine locally with ngrok

Screenshot 2024-08-08 at 12 30 00 PM

this is what i get when i start the server locally.

KnorpelSenf commented 3 months ago

This tells you that there's a mismatch between the path that you used when calling setWebhook and the path on which your server actually expects updates

shahbaz4783 commented 3 months ago

This tells you that there's a mismatch between the path that you used when calling setWebhook and the path on which your server actually expects updates

how can i fix the mismatch.

for URL, im using vercel deployed url

https://telegram-bot-seven-delta.vercel.app

and for setting webhook, im adding /api/bot above url

i also tried this for URL

https://telegram-bot-seven-delta.vercel.app/api/bot

i get this when i open the link:

Screenshot 2024-08-08 at 1 55 10 PM
KnorpelSenf commented 3 months ago
// Set up the webhook
const secretPath = `/webhook/${bot.token}`;
app.use(secretPath, webhookCallback(bot, 'express'));

You listen on /webhook/<token> so that's what you have to append, not /api/bot.

shahbaz4783 commented 3 months ago

Yeah i figured out the issue, thanks for the help!