Search code examples
darttelegramtelegram-bot

How to listen to successful_payment messages in Telegram Bot in Televerse?


I am using Televerse to build Telegram bot with Dart. I'm using televerse: ^1.11.4

dependencies:
  televerse: ^1.11.4
  # ...

When the bot successfully completes payments Telegram will send a SuccessfulPayment object. Here is the Telegram Bot API documentation about it: https://core.telegram.org/bots/api#successfulpayment

I am not sure how to listen for this particular message. How do access the successfulPayment object?

I tried looking in the Televerse documentation and couldn't find anything related to SuccessfulPayment object. I also checked Telegram Bot API documentation and couldn't understand much.


Solution

  • I can see that you're using the latest version of Televerse already.

    Hence, there's a method just for this. You can call the Televerse.onSuccessfulPayment callback register to attach a listener for the SuccessfulPayment service messages.

    Here's the code:

      bot.onSuccessfulPayment((ctx) {
        final successfulPayment = ctx.message.successfulPayment!;
        // Do something with the successful payment
    
      });
    

    This callback will only be executed when the successfulPayment object is not null. Hope this is what you're looking for.

    Happy Coding!