Closed strbit closed 1 month ago
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?
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?
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.
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)!));
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.
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.
I'm not really sure if it's an issue with grammY or with my code but i'm currently adding a payment system to my bot and i've run into an issue where (I don't really know how to put it into words) data would be "overwritten" with the old data. For example:
In the screenshot you can see how I have first selected the "100 Credits" option (and have not bought it), then I selected the "50 Credits" option (and have bought it). After the purchase, the bot has responded with the UUID of the "100 Credits" option when it should've been the UUID of the "50 Credits" option (
504b2ac...
).Here's the code that handles these purchases:
All products are simply stored in an array:
These products (which are loaded into a custom keyboard) are then hooked up to the invoice function (
hearsFull
is just a slightly modified version of thehears
function which allows me to listen for messages with custom variables).Any help would be greatly appreciated as I honestly have no clue where the issue could be coming from.