question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async/await works with polling, but not with webHooks

See original GitHub issue

Context

I have a bot and I’m running it with the different update approaches - long polling and webHooks. For some reasons, the same code works with the first option but doesn’t work with the second one. After some investigation, I found out that this piece of code breaks webHooks:

myStage.enter(async ctx => {
  const someData = await makeRequest();
  ctx.reply('Hello');
});

What’s strange about that, if I’ll have 2 ctx.reply(), then it will work and bot will send both messages. But if there is only one ctx.reply(), it won’t send anything.

  • Telegraf.js Version: 3.25.0
  • Node.js Version: 9.7.0
  • Operating System: Ubuntu 16.04

Expected Behavior

The same code should work both with long polling and webHooks.

Current Behavior

Apparently, it doesn’t.

Failure Information (for bugs)

There are no any errors.

Steps to Reproduce

  1. Create a stage;
  2. Use promise in enter callback.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
tva10commented, Dec 6, 2018

@dmbaranov, Hello!

Not sure how your await works, but I think that here your promise chain is broken Try to check your problem code with enabled Webhook and disabled Webhook reply

const config = {
	telegram: { webhookReply: true } // default true, but need to set false
};
const app = new Telegraf( /*token*/, config);

And so another moment, that reply method is async too. I think that it needs set await prefix

myStage.enter(async ctx => {
  const someData = await makeRequest();
  await ctx.reply('Hello');
});

ref: #320 #497

1reaction
dmbaranovcommented, Dec 6, 2018

@tva10 thanks for reply. Setting webhookReply: false fixes the problem indeed. Thanks for the explanation, though it’s still not clear enough for me why this is happening… And thanks for the notice about asyncWrapper function as well 😃

I’ll mark ticket as closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Moving Beyond Polling to Async APIs
This article will show you how to keep your API up-to-date by replacing polling with more modern and effective methods.
Read more >
Polling with async/await
This article is based on my real experience with refactoring a small piece of code with polling function so I won't be starting...
Read more >
Asynchronous Request-Reply pattern - Azure
One solution to this problem is to use HTTP polling. Polling is useful to client-side code, as it can be hard to provide...
Read more >
Expose public API for async system - rest
Short polling. In this pattern, you have a "start" HTTP API which returns an HTTP 202 and a unique identifier of the async...
Read more >
Recommendations on how to implement a sync / async use ...
My API client is not able to do polling, but my API backend ... You can make use a library with built-in async/await...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found