bot-base / telegram-bot-template

Telegram bot template based on grammY
294 stars 46 forks source link

Webhook not responding #461

Closed mikececco closed 1 month ago

mikececco commented 4 months ago

I cloned repo, set up the basics, sending a request to telegram bot like /start, request is captured from webhook but not responding. image

I am expecting it to respond as stated here:

feature.command("start", logHandle("command-start"), (ctx) => {
  return ctx.reply(ctx.t("welcome"));
});

All env keys are set: BOT_MODE=webhook BOT_TOKEN=...

I also tried to change NODE_ENV to production and run it with npm start but same behaviour

deptyped commented 4 months ago

Hi. You need to use the path /YOUR_BOT_TOKEN for the webhook (as I can see you are using /webhook), if you haven't changed the path here.

mikececco commented 4 months ago

Hi. You need to use the path /YOUR_BOT_TOKEN for the webhook (as I can see you are using /webhook), if you haven't changed the path here.

I did change it to that path now, as webhook URL I have set my bot token as endpoint (?) What do I set as webhook url?

mikececco commented 4 months ago

I think it does not matter as long as webhook url finishes with /webhook...no? I did set the webhook manually as

await bot.api.setWebhook(config.BOT_WEBHOOK, {
        allowed_updates: config.BOT_ALLOWED_UPDATES,
      });

is giving me Error occurred: HttpError: Network request for 'setWebhook' failed!

deptyped commented 4 months ago

What did you set in BOT_WEBHOOK? It should be something like https://vocal-sensible-hippo.ngrok-free.app/YOUR_BOT_TOKEN or https://vocal-sensible-hippo.ngrok-free.app/webhook (if you changed the path here to /webhook)

mikececco commented 4 months ago

What did you set in BOT_WEBHOOK? It should be something like https://vocal-sensible-hippo.ngrok-free.app/YOUR_BOT_TOKEN or https://vocal-sensible-hippo.ngrok-free.app/webhook (if you changed the path here to /webhook)

The second one

{
"ok": true,
["result"](https://api.telegram.org/bot.../getWebhookInfo): {
"url": ["https://vocal-sensible-hippo.ngrok-free.app/webhook"](https://vocal-sensible-hippo.ngrok-free.app/webhook),
"has_custom_certificate": false,
"pending_update_count": 1,
"last_error_date": 1715693432,
"last_error_message": "Wrong response from the webhook: 404 Not Found",
"max_connections": 40,
"ip_address": "18.158.249.75",
["allowed_updates"](https://api.telegram.org/bot...../getWebhookInfo): [
"message",
"callback_query"
]
}
}
deptyped commented 4 months ago

Wrong response from the webhook: 404 Not Found

Looks like the /webhook path doesn't exist for some reason?

deptyped commented 4 months ago

(Also, please revoke your bot token using https://t.me/BotFather, it's leaked in edit history)

mikececco commented 4 months ago

Wrong response from the webhook: 404 Not Found

Looks like the /webhook path doesn't exist for some reason?

Webhook endpoint exists, if I return like: server.post("/endpoint", () => ({ status: true })); it works

deptyped commented 4 months ago

if I return like: server.post("/endpoint", () => ({ status: true })); it works

Are you checking through ngrok or using 127.0.0.1? Maybe some tunneling issue?

mikececco commented 4 months ago

if I return like: server.post("/endpoint", () => ({ status: true })); it works Are you checking through ngrok or using 127.0.0.1? Maybe some tunneling issue?

Through ngrok. I am also getting Error occurred: HttpError: Network request for 'setWebhook' failed! if I dont comment that code. Might be something related to bot instance?

deptyped commented 4 months ago

Error occurred: HttpError: Network request for 'setWebhook' failed!

You can enable sensitive logs to see why it happens. Change this line: https://github.com/bot-base/telegram-bot-template/blob/679ab0a73c9a4b42a1f47ff7326a765ff6748565/src/main.ts#L10 to

const bot = createBot(config.BOT_TOKEN, {
  config: {
    client: {
      sensitiveLogs: true,
    },
  },
});
mikececco commented 4 months ago

Error occurred: HttpError: Network request for 'setWebhook' failed!

You can enable sensitive logs to see why it happens. Change this line:

https://github.com/bot-base/telegram-bot-template/blob/679ab0a73c9a4b42a1f47ff7326a765ff6748565/src/main.ts#L10

to

const bot = createBot(config.BOT_TOKEN, {
  config: {
    client: {
      sensitiveLogs: true,
    },
  },
});

Apparently it is setting it to be empty to be empty:

{
"ok": true,
"result": true,
"description": "Webhook is already deleted"
}

But that is not a problem now as I set it up manually

mikececco commented 4 months ago

Are we also sure fastify middleware works @deptyped ?

deptyped commented 4 months ago

Are we also sure fastify middleware works @deptyped ?

Hmm, I can confirm that something is broken. It seems to be related to this change in grammY. Reverting it fixes the problem.

mikececco commented 4 months ago

Are we also sure fastify middleware works @deptyped ?

Hmm, I can confirm that something is broken. It seems to be related to this change in grammY. Reverting it fixes the problem.

How can I revert it on my end?

deptyped commented 4 months ago

How can I revert it on my end?

The simplest way is to manually revert the changes to the node_modules/grammy/out/convenience/frameworks.js file. Fastify-related code is near 116 line.

mikececco commented 4 months ago

I did change it:

const fastify = (req, reply) => ({
    update: Promise.resolve(req.body),
    header: req.headers[SECRET_HEADER_LOWERCASE],
    end: () => reply.status(200).send(),
    respond: (json) => reply.send(json),
    unauthorized: () => reply.code(401).send(WRONG_TOKEN_ERROR),
});

but still my request comes in, I'm sending /start and no response back:

[19:03:44.302] INFO: incoming request
    reqId: "req-3"
    req: {
      "method": "POST",
      "url": "/webhook",
      "hostname": "vocal-sensible-hippo.ngrok-free.app",
      "remoteAddress": "127.0.0.1",
      "remotePort": 46724
    }
mikececco commented 4 months ago

Any idea?

On Wed, 15 May 2024, 01:48 deptyped, @.***> wrote:

How can I revert it on my end?

The simplest way is to manually revert the changes to the node_modules/grammy/out/convenience/frameworks.js file. Fastify-related code is near 116 line.

— Reply to this email directly, view it on GitHub https://github.com/bot-base/telegram-bot-template/issues/461#issuecomment-2110690691, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVDZXOPKRQHPR26AL4H3N3TZCI57LAVCNFSM6AAAAABHV7EPD2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJQGY4TANRZGE . You are receiving this because you authored the thread.Message ID: @.***>

deptyped commented 4 months ago

I replaced Fastify with Hono in the template. If Fastify is not a requirement for you, you can fetch the latest changes to use Hono instead.

deptyped commented 4 months ago

How can I do that? (junior developer here)

If you generated a new repository, you need to add the template repository as a remote:

git remote add template git@github.com:bot-base/telegram-bot-template.git
git remote update

And then merge the changes from main branch:

git merge template/main --squash --no-commit --allow-unrelated-histories

If you just cloned this repository, you need to pull the changes:

git pull origin main
mikececco commented 4 months ago

Thank you!

On Wed, 15 May 2024, 18:49 deptyped, @.***> wrote:

How can I do that? (junior developer here)

If you generated a new repository, you need to add the template repository as a remote:

git remote add template @.***:bot-base/telegram-bot-template.git git remote update

And then merge the changes from main branch:

git merge template/main --squash --no-commit --allow-unrelated-histories

If you just cloned this repository, you need to pull the changes:

git pull origin main

— Reply to this email directly, view it on GitHub https://github.com/bot-base/telegram-bot-template/issues/461#issuecomment-2112062040, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVDZXOOHXICUPQSEPMUKDL3ZCMVTJAVCNFSM6AAAAABHV7EPD2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJSGA3DEMBUGA . You are receiving this because you authored the thread.Message ID: @.***>

mikececco commented 4 months ago

@deptyped still not working Webhook not responding to /start message

[13:40:32.972] INFO: Webhook was set
    url: "https://vocal-sensible-hippo.ngrok-free.app/webhook"
[13:40:32.975] INFO: Server started
    url: "http://127.0.0.1:3000"
[13:41:53.379] DEBUG: Incoming request
    requestId: "a02e21d2-6df4-4409-afde-7136c093d9cd"
    method: "POST"
    path: "/webhook"
deptyped commented 4 months ago

Hm, according to the logs everything is ok, the request is received on the webhook and passed to the handler. I can't reproduce the problem either. Can you please share the code or provide a minimal reproducible example?

mikececco commented 4 months ago

@deptyped I cloned the repo again, I had to remove the following code:

    // await bot.init(); --> this

    // start server
    serve(
      {
        fetch: server.fetch,
        hostname: config.BOT_SERVER_HOST,
        port: config.BOT_SERVER_PORT,
      },
      (info) => {
        const url =
          info.family === "IPv6"
            ? `http://[${info.address}]:${info.port}`
            : `http://${info.address}:${info.port}`;

        logger.info({
          msg: "Server started",
          url,
        });
      },
    );

    // set webhook --> AND THIS
    // await bot.api.setWebhook(config.BOT_WEBHOOK, {
    //   allowed_updates: config.BOT_ALLOWED_UPDATES,
    //   secret_token: config.BOT_WEBHOOK_SECRET,
    // });

but besides that npm run dev accepts requests, but still not working. I am using ngrok, on which I set the /webhook endpoint. Didnt change anything else besides that.

mikececco commented 4 months ago

Yes I can confirm there is something with the middleware

deptyped commented 4 months ago

I had to remove the following code

Why? await bot.init() is necessary to ensure that your bot token is correct before you start receiving requests from Telegram. If it doesn't work, it means your bot won't work either.

mikececco commented 4 months ago

I had to remove the following code

Why? await bot.init() is necessary to ensure that your bot token is correct before you start receiving requests from Telegram. If it doesn't work, it means your bot won't work either.

Any suggestion on that? Its a simple bot

I created a new bot with new token but same issue... bot.init() is pending


     * Information about the bot itself as retrieved from `api.getMe()`. Only
     * available after the bot has been initialized via `await bot.init()`, or
     * after the value has been set manually.
     *
     * Starting the bot will always perform the initialization automatically,
     * unless a manual value is already set.

from grammy documentation, is it the case in which we need to init automatically?

deptyped commented 4 months ago

I created a new bot with new token but same issue... bot.init() is pending

That's weird. Can you access Bot API directly?

https://api.telegram.org/botTOKEN/getMe

from grammy documentation, is it the case in which we need to init automatically?

I sometimes encountered this error without manual initialization. Probably automatic initialization doesn't work well enough if the bot receives many requests immediately after startup. However, this was about a year ago, maybe it's fixed now.

But there's another reason to initialize manually. If we have incorrect credentials, what's the point of starting the server, fetching an update, and halting operations in the middle of processing due to an incorrect token? It's much better to fail fast.

mikececco commented 4 months ago

if ( // !process.env.IS_BROWSER && // uncomment this line if you use a bundler that sets env.IS_BROWSER during build time process.versions && // check for node in case we want to use this in "exotic" JS envs process.versions.node ) { setDefaultAutoSelectFamily(false); }

this piece of code fixed it. It has somethind to do with IPv6 / IPv4

deptyped commented 4 months ago

It has somethind to do with IPv6 / IPv4

Wow 🤯
I'm glad you found a solution! So, does everything work now?

mikececco commented 4 months ago

Everything works now!

mikececco commented 4 months ago

@deptyped It is not working in production though, deployed on Vercel

deptyped commented 4 months ago

Make sure you turn off Vercel Authentication and set the NODEJS_HELPERS environment variable to 0

mikececco commented 4 months ago

Nothing to do, all endpoints work but Webhookcallback is not being called. I did not add anything besides cloning the repo and deploying it to vercel and configuring env variables. I wonder if it has something to do with IPv4 again

On Sat, May 18, 2024 at 12:04 AM deptyped @.***> wrote:

Make sure you turn off Vercel Authentication https://vercel.com/docs/security/deployment-protection/methods-to-protect-deployments/vercel-authentication#manage-using-the-dashboard and set the NODEJS_HELPERS environment variable to 0

— Reply to this email directly, view it on GitHub https://github.com/bot-base/telegram-bot-template/issues/461#issuecomment-2118433028, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVDZXON6OCMG5YXPFZ6YP5DZCZ5FJAVCNFSM6AAAAABHV7EPD2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJYGQZTGMBSHA . You are receiving this because you modified the open/close state.Message ID: @.***>

mikececco commented 4 months ago

Vercel dev works perfectly locally though.

deptyped commented 4 months ago

Try getWebhookInfo method to check if there is any error

https://api.telegram.org/botTOKEN/getWebhookInfo
mikececco commented 4 months ago

Did solve it, by changing to polling once and switching back to webhook mode. Don't ask me why.

On Sun, 19 May 2024, 04:46 deptyped, @.***> wrote:

Try getWebhookInfo method to check if there is any error

https://api.telegram.org/botTOKEN/getWebhookInfo

— Reply to this email directly, view it on GitHub https://github.com/bot-base/telegram-bot-template/issues/461#issuecomment-2118987421, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVDZXOPBZS6YTH4YARUQRTTZC6VZJAVCNFSM6AAAAABHV7EPD2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJYHE4DONBSGE . You are receiving this because you modified the open/close state.Message ID: @.***>