david-lev / pywa

🤖 Build WhatsApp Bots in Python
https://pywa.readthedocs.io
MIT License
221 stars 33 forks source link

adding support for whatapp button messages i.e. message callback on s… #17

Closed bcombes closed 11 months ago

bcombes commented 11 months ago

…electing a template button

bcombes commented 11 months ago

@david-lev I created this commit to provide support for the whatsapp cloud button message type

yehuda-lev commented 11 months ago

I think it is no longer considered a message and you will not receive it in on_message you will receive it in on_callback and therefore there is no point in putting on a filter

bcombes commented 11 months ago

Hi David,

The reason why I included this was because I wasn't receiving the call back for this. I suspect this for quick reply buttons defined in templates. I then used the raw update callback handler to examine the payload and this is what I found.

{

'object': 'whatsapp_business_account', 'entry': [ { 'id': '148844698304491', 'changes': [ { 'value': { 'messaging_product': 'whatsapp', 'metadata': { 'display_phone_number': 'XXXXXXXXXXXXXX', 'phone_number_id': 'XXXXXXXXXXXXX' }, 'contacts': [ { 'profile': { 'name': 'bcombes' }, 'wa_id': 'XXXXXXXXXXXXXXXXX' } ], 'messages': [ { 'context': { 'from': 'XXXXXXXXXX', 'id': 'wamid.HBgNMjM0ODAzMzgyNzIxMBUCABEYEkFBQ0UxRjdFMzk0MDU0NzRGRQA=' }, 'from': 'XXXXXXXXXXX', 'id': 'wamid.HBgNMjM0ODAzMzgyNzIxMBUCABIYIDAzQzAxNDkyMjFCOEUwMjg1MTlEOTJDNkYwNDEwQkVGAA==', 'timestamp': '1697488362', 'type': 'button', 'button': { 'payload': 'Adire', 'text': 'Shop African Fabrics' } } ] }, 'field': 'messages' } ] } ] }

As you can see, this is not a conventional button call-back. It just generates a text reply based on the user's selection as shown in the sample here; [image: image.png]

On Tue, Oct 17, 2023 at 12:51 AM Yehuda lev @.***> wrote:

I think it is no longer considered a message and you will not receive it in on_message you will receive it in on_callback and therefore there is no point in putting on a filter

— Reply to this email directly, view it on GitHub https://github.com/david-lev/pywa/pull/17#issuecomment-1765441937, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMWLNPJDT535DN3JVHH3F3X7XB67AVCNFSM6AAAAAA6C4RYZSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRVGQ2DCOJTG4 . You are receiving this because you authored the thread.Message ID: @.***>

david-lev commented 11 months ago

Hi @bcombes First thanks for your contribution! I assumed that the callback that comes from the Template's QuickReplyButton is the same as the one that comes from a normal Button. But like everything - WhatsApp likes to complicate things...

I understand that from the user and developer point of view, there should be no separation between Button (on_callback_button) and the new update that comes when you click QuickReplyButton in Template. Right?

If so, I think you should just combine them. Perform a test on 'button' and return a CallbackButtonHandler, and in the CallbackButton constructor perform another check on the type and take the appropriate fields for data and title.

Maybe we'll add a boolean field called is_quick_reply that indicates that the callback comes from QuickReplyButton and not from Button. But I don't really see it as necessary - I'd like to hear your opinion.

Again, all this is assuming that there is indeed no difference between QuickReplyButton and Button (the image you attached was not sent)

bcombes commented 11 months ago

Oops, let me resend. Your suggestion sounds good. Honestly, I haven't really checked the regular button, but I'm pretty sure we can make it work. I'm a bit of a python noob, but fairly experienced software engineer. I like the quality of your project overall (although I don't quite get it all) and this is also a good learning opportunity for me.

Regards

P.S: Resending the screen shot

[image: WhatsApp Image 2023-10-17 at 06.18.11.jpeg]

On Tue, Oct 17, 2023 at 8:34 AM David Lev @.***> wrote:

Hi @bcombes https://github.com/bcombes First thanks for your contribution! I assumed that the callback that comes from the Template's QuickReplyButton is the same as the one that comes from a normal Button. But like everything - WhatsApp likes to complicate things...

I understand that from the user and developer point of view, there should be no separation between Button (on_callback_button) and the new update that comes when you click QuickReplyButton in Template. Right?

If so, I think you should just combine them. Perform a test https://github.com/david-lev/pywa/blob/master/pywa/webhook.py#L99 on 'button' and return a CallbackButtonHandler, and in the CallbackButton constructor https://github.com/david-lev/pywa/blob/master/pywa/types/callback.py#L217 perform another check on the type and take the appropriate fields for data and title.

Maybe we'll add a boolean field called is_quick_reply that indicates that the callback comes from QuickReplyButton and not from Button. But I don't really see it as necessary - I'd like to hear your opinion.

Again, all this is assuming that there is indeed no difference between QuickReplyButton and Button (the image you attached was not sent)

— Reply to this email directly, view it on GitHub https://github.com/david-lev/pywa/pull/17#issuecomment-1765833794, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMWLNP5HJT76FQIVDHNTUTX7YYGTAVCNFSM6AAAAAA6C4RYZSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRVHAZTGNZZGQ . You are receiving this because you were mentioned.Message ID: @.***>

yehuda-lev commented 11 months ago

@david-lev If so, then you should define Callback.data as str or None

yehuda-lev commented 11 months ago

P.S: Resending the screen shot

[image: WhatsApp Image 2023-10-17 at 06.18.11.jpeg]

The picture was not sent again

david-lev commented 11 months ago

@david-lev If so, then you should define Callback.data as str or None

Why? QuickReply has data (payload) too.

david-lev commented 11 months ago

@bcombes I'm glad you enjoyed the project and I would love any improvement you can offer.

Your report has already been very helpful, I'll add the changes later and merge your PR.

yehuda-lev commented 11 months ago

@david-lev If so, then you should define Callback.data as str or None

Why? QuickReply has data (payload) too.

As you can see here, you don't get any information except the text

'from': 'XXXXXXXXXXX', 'id': 'wamid.HBgNMjM0ODAzMzgyNzIxMBUCABIYIDAzQzAxNDkyMjFCOEUwMjg1MTlEOTJDNkYwNDEwQkVGAA==', 'timestamp': '1697488362', 'type': 'button', 'button': { 'payload': 'Adire', 'text': 'Shop African Fabrics' } } ] }, 'field': 'messages' } ] } ] }

edit maybe 'payload': 'Adire', is the data 🤔

david-lev commented 11 months ago

edit maybe 'payload': 'Adire', is the data 🤔

Yes.

david-lev commented 11 months ago

Got fixed in 1.7.3