Comments (6)
Got it working by moving all of the handlers to my createBot
function within the index.ts
file (which runs prior to the bot start) and the :successful_payment
to its own function. It's quite messy but it works like expected. Big thanks for the help!
/**
* Answers the checkout query which is required to continue with the payment.
* @see https://core.telegram.org/bots/api#answerprecheckoutquery
*/
protectedInstance.on('pre_checkout_query', async (ctx) => ctx.answerPreCheckoutQuery(true));
/**
* Responds to a successful payment and updates the user's balance.
*/
protectedInstance.on(':successful_payment', async (ctx) => processSuccessfulPayment(ctx, getProductInfo(getUpdateInfo(ctx).message?.successful_payment!.invoice_payload as AvailableProducts)!));
from grammy.
Thanks for the suggestion, I believe i'm already doing this with most of my modules but i'll check out the docs to make sure.
from grammy.
I am not sure that this will fix all of your code, but there is one mistake that stands out: you register further handlers on your composer inside an existing handler by calling composer.on
a few times. This means that you alter the behaviour of your entire bot whenever an update is handled. Basically, whenever you run the code above, a new handler is added, so if you handle 5000 payments, then your bot now has 5000 handlers for ':successful_payment'
. That surely is not what you meant, is it?
from grammy.
Yeah, that is definitely not the intended behaviour. Is there a way to see which handlers are added so I can perhaps try and see what's going on? Or do they not provide any useful information?
from grammy.
There is no way to see this, but in general, you should never add more handlers after your bot has started. You may want to use something like https://grammy.dev/plugins/conversations for this.
from grammy.
You can improve your code structure by defining things in modules, but registering things centrally. That is better than passing around composer instances to register middleware in many places. https://grammy.dev/advanced/structuring elaborates on this.
from grammy.
Related Issues (20)
- [feature] How do I listen for every messages in a group chat? HOT 2
- Can't deploy conversations plugin on Cloudflare Workers HOT 1
- Property 'wait' does not exist on type 'ConversationControls'.ts(2339) HOT 2
- feat: ctx.message.message_thread_id presumably should not be | undefined in message:is_topic_message HOT 6
- Conditionally enrich context and change it's type for all subsequent midleware. HOT 7
- [Proposal] Expose type FilteredContext. HOT 1
- HttpError: Network request for 'getMe' failed! HOT 5
- HttpError: Network request for 'sendMessage' failed! HOT 3
- Add error type to `GrammyError` HOT 2
- Problem with editing files (editMessageMedia and editMessageMediaInline) HOT 3
- menu plugin with multiple language translations error HOT 2
- Bot not full width on mobile HOT 4
- Allow using AbortController in globalThis if available
- use_independent_chat_permissions is missing in setChatPermissions HOT 1
- How to use message_auto_delete_time HOT 2
- GrammyError: Call to 'setMyCommands' failed! (404: Not Found) HOT 10
- Bot - Better way of sending multiple message with array of objects. HOT 3
- Didn't receive any messages HOT 3
- Crashing on calling webhook with empty JSON body using Oak HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grammy.