Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createWebhook always respond 404 with Koa #1889

Open
mahnunchik opened this issue Oct 7, 2023 · 9 comments
Open

createWebhook always respond 404 with Koa #1889

mahnunchik opened this issue Oct 7, 2023 · 9 comments

Comments

@mahnunchik
Copy link

Context

  • Telegraf.js Version: 4.14.0
  • Node.js Version: v18.17.1
  • Operating System: macOS

Minimal Example Code Reproducing the Issue

import Koa from 'koa';
const app = new Koa();
const bot = new Telegraf(token);

const webhook = await bot.createWebhook({ domain });
app.use((ctx, next) => {
  return webhook(ctx.request, ctx.response, next);
});

Expected Behavior

200 status code

Current Behavior

404 status code

Error Message and Logs (export DEBUG='telegraf:*')

No errors in debug log

@hypernova7
Copy link
Collaborator

@mahnunchik
Copy link
Author

@hypernova7 example is completely broken.

app.use(async (ctx, next) =>
	(await bot.createWebhook({ domain: webhookDomain }))(ctx.req, ctx.res, next),
);
  1. Creates webhook on each request
  2. Doesn't handle updates
  3. Responds with 404

@MKRhere
Copy link
Member

MKRhere commented Oct 7, 2023

The original code posted above seems fine. It can only respond 404 if for whatever reason webhook calls next, which means it is not going to handle the request. I'll test and update the example.

@mahnunchik
Copy link
Author

No, I've tested on koa 2.14.2

Handler called and server responds webhook with right body but 404 status.

@hypernova7
Copy link
Collaborator

Try with bot.launch

import 'dotenv/config'
import Koa from 'koa'
import ngrok from 'ngrok';
import { Telegraf } from 'telegraf'


const app = new Koa();
const bot = new Telegraf(process.env.BOT_TOKEN);
const url = await ngrok.connect(8080); // for development


bot.start((ctx) => {
  ctx.reply('Hi')
})

bot.launch({
  webhook: {
    domain: url,
    port: 8080,
    cb: app
  }
})

@mahnunchik
Copy link
Author

koa integration is still broken

Error [ERR_HTTP_HEADERS_SENT]: Cannot write headers after they are sent to the client

@mahnunchik
Copy link
Author

@MKRhere

The original code posted above seems fine.

The original code creates webhook on each request. But the main problem is that it doesn't work

@mahnunchik
Copy link
Author

ctx.res
Node's response object.
Bypassing Koa's response handling is not supported. Avoid using the following node properties:
res.statusCode
res.writeHead()
res.write()
res.end()

https://koajs.com/#ctx-res

@mahnunchik
Copy link
Author

// create webhook only ONCE
const webhook = await bot.createWebhook({ domain: webhookDomain });
app.use(async (ctx, next) => {
  // use ctx.request to have access to body
  await webhook(ctx.request, ctx.res, next);
});

But still it responds 404...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants