xooniverse / televerse

Your gateway to seamless Telegram Bot Development 💙
https://televerse.xooniverse.com
BSD 3-Clause "New" or "Revised" License
64 stars 11 forks source link

Missing processor for successfulPayment #158

Closed Defuera closed 12 months ago

Defuera commented 12 months ago

In payment flow (see docs)after onPreCheckoutQuery telegram sends json containing successful_payment model. The library missing a handler for this, I believe it should be added along side Televerse.onPreCheckoutQuery method.

HeySreelal commented 12 months ago

Hey @Defuera, thanks for raising this.

The SuccessfulPayment object is already available in the library. When a payment is successful, Telegram sends a "service message" containing the successful_payment parameter. You can listen for this message by adding a listener using the Televerse.onMessage method, or by specifically filtering for successful payment messages using the Televerse.filter method, as shown below.

  bot.filter((ctx) => ctx.message.successfulPayment != null, (ctx) async {
    // Do your stuff here...
  });

Hope this helps. ✨

HeySreelal commented 12 months ago

Also, as you raised this we can may be add a filter method just for listening this service messages. Something like Televerse.onSuccessfulPayment. Hoping that could make your code little more smoother.

Will that be something you're interested in?

Defuera commented 12 months ago

Oh, that's great. Thx @HeySreelal And thanks for the great library in general. I'm developing a framework around it, that allows to build a complex dialog flows keeping context. https://pub.dev/packages/chatterbox It's very early stage. But just FYI :)

HeySreelal commented 12 months ago

Wow! I'm so excited to hear that! Tbh, I'm super excited to contribute as well. If you have any issues with the Televerse package or need some tweaks in this package to make your tool better, feel free to reach out to me or in our Telegram group. I will be more than happy to help you out on this. ❤️

When building Televerse, extensions to this package was in my mind. But I was not sure how much feasible it is until I heard this from you. Feel free to let us know if you need to tweak this library at any point of your extension development.

Have a good time Televersing! 🚀

Defuera commented 12 months ago

Here's the implementation according to suggestion above.

Usage:

bot.onSuccessfulPayment((ctx) {
      //todo your code
      print('Successful payment ${ctx.update.message?.successfulPayment}');
    });

televerse_extensions.dart

import 'dart:async';

import 'package:televerse/telegram.dart';
import 'package:televerse/televerse.dart';

typedef SuccessfulPaymentHandler = FutureOr<void> Function(MessageContext ctx, SuccessfulPayment successfulPayment);

extension TeleverseExtensions on Televerse {
  void onSuccessfulPayment(SuccessfulPaymentHandler handler) {
    filter(
      (ctx) => ctx.message.successfulPayment != null,
      (ctx) => handler(ctx, ctx.message.successfulPayment!),
    );
  }
}
HeySreelal commented 12 months ago

Yeah yeah! Consider this done! Btw, I meant if you need further tweaks in the project for extensions compatibility.

Peace ✌️