Giter Club home page Giter Club logo

Comments (6)

strbit avatar strbit commented on July 29, 2024 1

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.

strbit avatar strbit commented on July 29, 2024 1

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.

KnorpelSenf avatar KnorpelSenf commented on July 29, 2024

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.

strbit avatar strbit commented on July 29, 2024

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.

KnorpelSenf avatar KnorpelSenf commented on July 29, 2024

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.

KnorpelSenf avatar KnorpelSenf commented on July 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.