pywa icon indicating copy to clipboard operation
pywa copied to clipboard

Add get_templates method to retrieve WhatsApp templates

Open YonchRibak opened this issue 9 months ago โ€ข 3 comments

This PR adds a new method get_templates that allows users to retrieve all templates for their WhatsApp Business Account. The implementation:

  1. Adds the method to WhatsAppCloudApi in api.py
  2. Adds a corresponding method to WhatsApp in client.py
  3. Adds async versions of both methods
  4. Includes tests for the new functionality

This method requires a system user access token rather than the regular phone ID token, so I've implemented it to temporarily update the headers for the request and then restore them.

Note: I encountered an issue with pre-commit getting stuck on "fix end of files" and had to bypass it for this PR.

YonchRibak avatar Apr 02 '25 12:04 YonchRibak

Hey @YonchRibak , thanks for your PR! We really appreciate the contribution.

A couple of things:

In wa.get_templates(), you're returning a list of dict. Could you check if it's possible to return a list of typs.template.Template instead? You can add .from_dict() to typs.template.Template

We're experimenting with a new approach for handling responses in the results branch (e.g., get_flows() uses a Result class). You can see it here

The same applies to block_users (commits here)โ€”if possible, aligning with this structure would be great.

We're quite busy, so updates have been slow, but we hope to merge this soon. Thanks again! ๐Ÿš€

yehuda-lev avatar Apr 02 '25 19:04 yehuda-lev

I've made a new commit to pr addressing your request for typing. i've added a new type of RetrievedTemplate because what returns from META is slightly different from Template tyoe that is more suited for sending or creating a template.

On Wed, Apr 2, 2025 at 10:36โ€ฏPM Yehuda lev @.***> wrote:

Hey @YonchRibak https://github.com/YonchRibak , thanks for your PR! We really appreciate the contribution.

A couple of things:

In wa.get_templates(), you're returning a list of dict. Could you check if it's possible to return a list of typs.template.Template instead? You can add .from_dict() to typs.template.Template https://github.com/david-lev/pywa/blob/a662c1632ab7b5e32ab59ce59bd40ae71579031a/pywa/types/template.py#L214

We're experimenting with a new approach for handling responses in the results branch (e.g., get_flows() uses a Result class). You can see it here https://github.com/david-lev/pywa/compare/master...results

The same applies to block_users (commits here https://github.com/david-lev/pywa/commits/block-users/)โ€”if possible, aligning with this structure would be great.

We're quite busy, so updates have been slow, but we hope to merge this soon. Thanks again! ๐Ÿš€

โ€” Reply to this email directly, view it on GitHub https://github.com/david-lev/pywa/pull/111#issuecomment-2773531894, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2HCAVJLQXKVMTB7EP257T32XQ32TAVCNFSM6AAAAAB2JNVJTGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONZTGUZTCOBZGQ . You are receiving this because you were mentioned.Message ID: @.***> [image: yehuda-lev]yehuda-lev left a comment (david-lev/pywa#111) https://github.com/david-lev/pywa/pull/111#issuecomment-2773531894

Hey @YonchRibak https://github.com/YonchRibak , thanks for your PR! We really appreciate the contribution.

A couple of things:

In wa.get_templates(), you're returning a list of dict. Could you check if it's possible to return a list of typs.template.Template instead? You can add .from_dict() to typs.template.Template https://github.com/david-lev/pywa/blob/a662c1632ab7b5e32ab59ce59bd40ae71579031a/pywa/types/template.py#L214

We're experimenting with a new approach for handling responses in the results branch (e.g., get_flows() uses a Result class). You can see it here https://github.com/david-lev/pywa/compare/master...results

The same applies to block_users (commits here https://github.com/david-lev/pywa/commits/block-users/)โ€”if possible, aligning with this structure would be great.

We're quite busy, so updates have been slow, but we hope to merge this soon. Thanks again! ๐Ÿš€

โ€” Reply to this email directly, view it on GitHub https://github.com/david-lev/pywa/pull/111#issuecomment-2773531894, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2HCAVJLQXKVMTB7EP257T32XQ32TAVCNFSM6AAAAAB2JNVJTGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONZTGUZTCOBZGQ . You are receiving this because you were mentioned.Message ID: @.***>

YonchRibak avatar Apr 04 '25 09:04 YonchRibak

i eventually created a new pr and a new branch because i lost a battle to git https://github.com/david-lev/pywa/pull/113

โ€ชOn Fri, Apr 4, 2025 at 12:45โ€ฏPM โ€ซื™ื•ื ืชืŸ ืจื™ื‘ืงโ€ฌโ€Ž @.***> wrote:โ€ฌ

I've made a new commit to pr addressing your request for typing. i've added a new type of RetrievedTemplate because what returns from META is slightly different from Template tyoe that is more suited for sending or creating a template.

On Wed, Apr 2, 2025 at 10:36โ€ฏPM Yehuda lev @.***> wrote:

Hey @YonchRibak https://github.com/YonchRibak , thanks for your PR! We really appreciate the contribution.

A couple of things:

In wa.get_templates(), you're returning a list of dict. Could you check if it's possible to return a list of typs.template.Template instead? You can add .from_dict() to typs.template.Template https://github.com/david-lev/pywa/blob/a662c1632ab7b5e32ab59ce59bd40ae71579031a/pywa/types/template.py#L214

We're experimenting with a new approach for handling responses in the results branch (e.g., get_flows() uses a Result class). You can see it here https://github.com/david-lev/pywa/compare/master...results

The same applies to block_users (commits here https://github.com/david-lev/pywa/commits/block-users/)โ€”if possible, aligning with this structure would be great.

We're quite busy, so updates have been slow, but we hope to merge this soon. Thanks again! ๐Ÿš€

โ€” Reply to this email directly, view it on GitHub https://github.com/david-lev/pywa/pull/111#issuecomment-2773531894, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2HCAVJLQXKVMTB7EP257T32XQ32TAVCNFSM6AAAAAB2JNVJTGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONZTGUZTCOBZGQ . You are receiving this because you were mentioned.Message ID: @.***> [image: yehuda-lev]yehuda-lev left a comment (david-lev/pywa#111) https://github.com/david-lev/pywa/pull/111#issuecomment-2773531894

Hey @YonchRibak https://github.com/YonchRibak , thanks for your PR! We really appreciate the contribution.

A couple of things:

In wa.get_templates(), you're returning a list of dict. Could you check if it's possible to return a list of typs.template.Template instead? You can add .from_dict() to typs.template.Template https://github.com/david-lev/pywa/blob/a662c1632ab7b5e32ab59ce59bd40ae71579031a/pywa/types/template.py#L214

We're experimenting with a new approach for handling responses in the results branch (e.g., get_flows() uses a Result class). You can see it here https://github.com/david-lev/pywa/compare/master...results

The same applies to block_users (commits here https://github.com/david-lev/pywa/commits/block-users/)โ€”if possible, aligning with this structure would be great.

We're quite busy, so updates have been slow, but we hope to merge this soon. Thanks again! ๐Ÿš€

โ€” Reply to this email directly, view it on GitHub https://github.com/david-lev/pywa/pull/111#issuecomment-2773531894, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2HCAVJLQXKVMTB7EP257T32XQ32TAVCNFSM6AAAAAB2JNVJTGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONZTGUZTCOBZGQ . You are receiving this because you were mentioned.Message ID: @.***>

YonchRibak avatar Apr 04 '25 10:04 YonchRibak

https://pywa.readthedocs.io/en/v3/content/client/client_reference.html#pywa.client.WhatsApp.get_templates

david-lev avatar Jul 31 '25 18:07 david-lev

#113

david-lev avatar Aug 04 '25 11:08 david-lev