pywa icon indicating copy to clipboard operation
pywa copied to clipboard

Invalid Parameter error on create_template function, Buttons section

Open pablomarin opened this issue 1 year ago • 2 comments

Describe the bug

When calling:

from pywa.types import NewTemplate as NewTemp

wa.create_template(
    template=NewTemp(
        name='buy_new_iphone_x_with_buttons',
        category=NewTemp.Category.MARKETING,
        language=NewTemp.Language.ENGLISH_US,
        header=NewTemp.Text('The New iPhone {15} is here!'),
        body=NewTemp.Body('Buy now and use the code {WA_IPHONE_15} to get {15%} off!'),
        footer=NewTemp.Footer('Powered by PyWa'),
        buttons=[
            NewTemp.UrlButton(title='Buy Now', url='https://example.com/shop/{iphone15}'),
            NewTemp.QuickReplyButton('Unsubscribe from marketing messages'),
        ],
    ),
    waba_id=MY_WABA_ID
)

I'm getting this error:

2025-01-22 14:22:14,457 - httpx - INFO - HTTP Request: POST https://graph.facebook.com/v21.0/335760796291207/message_templates "HTTP/1.1 400 Bad Request"
---------------------------------------------------------------------------
WhatsAppError                             Traceback (most recent call last)
Cell In[44], line 3
      1 from pywa.types import NewTemplate as NewTemp
----> 3 wa.create_template(
      4     template=NewTemp(
      5         name='buy_new_iphone_x_with_buttons',
      6         category=NewTemp.Category.MARKETING,
      7         language=NewTemp.Language.ENGLISH_US,
      8         header=NewTemp.Text('The New iPhone {15} is here!'),
      9         body=NewTemp.Body('Buy now and use the code {WA_IPHONE_15} to get {15%} off!'),
     10         footer=NewTemp.Footer('Powered by PyWa'),
     11         buttons=[
     12             NewTemp.UrlButton(title='Buy Now', url='[https://example.com/shop/{iphone15](https://example.com/shop/%7Biphone15)}'),
     13             NewTemp.QuickReplyButton('Unsubscribe from marketing messages'),
     14         ],
     15     ),
     16     waba_id=MY_WABA_ID
     17 )

File /anaconda/envs/GPTSearch3/lib/python3.12/site-packages/pywa/client.py:1904, in WhatsApp.create_template(self, template, placeholder, waba_id)
   1827 def create_template(
   1828     self,
   1829     template: NewTemplate,
   1830     placeholder: tuple[str, str] | None = None,
   1831     waba_id: str | int | None = None,
   1832 ) -> TemplateResponse:
   1833     """
   1834     `'Create Templates' on developers.facebook.com
   1835     <[https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates>`_](https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates%3E%60_).
   (...)
   1901         The template created response. containing the template ID, status and category.
   1902     """
   1903     return TemplateResponse(
-> 1904         **self.api.create_template(
   1905             waba_id=helpers.resolve_waba_id_param(self, waba_id),
   1906             template=template.to_dict(placeholder=placeholder),
   1907         )
   1908     )

File /anaconda/envs/GPTSearch3/lib/python3.12/site-packages/pywa/api.py:786, in WhatsAppCloudApi.create_template(self, waba_id, template)
    764 def create_template(
    765     self,
    766     waba_id: str,
    767     template: dict[str, str | list[str]],
    768 ) -> dict[str, str]:
    769     """
    770     Create a message template.
    771 
   (...)
    784         }
    785     """
--> 786     return self._make_request(
    787         method="POST",
    788         endpoint=f"/{waba_id}/message_templates",
    789         json=template,
    790     )

File /anaconda/envs/GPTSearch3/lib/python3.12/site-packages/pywa/api.py:63, in WhatsAppCloudApi._make_request(self, method, endpoint, **kwargs)
     61 res = self._session.request(method=method, url=endpoint, **kwargs)
     62 if res.status_code >= 400:
---> 63     raise WhatsAppError.from_dict(error=res.json()["error"], response=res)
     64 return res.json()

WhatsAppError: WhatsAppError(message='Invalid parameter', details=None, code=100)

To Reproduce

Using Python 3.12

Expected behavior

No response

pywa version

latest

python version

3.12

os

linux ubuntu

Additional context

When removing the buttons section, it works fine

pablomarin avatar Jan 22 '25 15:01 pablomarin

Hi,

One thing you could do to try to get a bit more information is to run it using a post request:

ie

endpoint = f'https://graph.facebook.com/v22.0/{waba_id}/message_templates'
headers = {
    'Content-Type': 'application/json',
    'Authorization': f"Bearer {os.environ['TOKEN']}"
}
resp = requests.post(endpoint, headers=headers, json=carousel)

print(resp.json())

When having a similar problem I found this error message to provide a lot more detail, and helped identify the problem:

{'error': {'message': 'Invalid parameter',
  'type': 'OAuthException',
  'code': 100,
  'error_subcode': 2388043,
  'is_transient': False,
  'error_user_title': 'Message template "components" param is missing expected field(s)',
  'error_user_msg': 'component of type HEADER is missing expected field(s) (example)',
  'fbtrace_id': '...'}}

It looks like the PyWa WhatsAppError exception only pulls out the message from the error, which in this particular case is not that informative.

mhaugestad avatar Feb 08 '25 17:02 mhaugestad

I see. About the problem itself, I'll need to check it first. And about the WhatsAppError, I see what you mean. I'll try to think on something, but in the meantime you can access the raw response from the exception. I think it's the raw_response attr. The httpx response.

On Sat, Feb 8, 2025, 19:48 Mathias Haugestad @.***> wrote:

Hi,

One thing you could do to try to get a bit more information is to run it using a post request:

ie

endpoint = f'https://graph.facebook.com/v22.0/{waba_id}/message_templates' headers = { 'Content-Type': 'application/json', 'Authorization': f"Bearer {os.environ['TOKEN']}" } resp = requests.post(endpoint, headers=headers, json=carousel)

print(resp.json())

When having a similar problem I found this error message to provide a lot more detail, and helped identify the problem:

{'error': {'message': 'Invalid parameter', 'type': 'OAuthException', 'code': 100, 'error_subcode': 2388043, 'is_transient': False, 'error_user_title': 'Message template "components" param is missing expected field(s)', 'error_user_msg': 'component of type HEADER is missing expected field(s) (example)', 'fbtrace_id': '...'}}

It looks like the PyWa WhatsAppError exception only pulls out the message from the error, which in this particular case is not that informative.

— Reply to this email directly, view it on GitHub https://github.com/david-lev/pywa/issues/100#issuecomment-2645871060, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKHBMIBJ6EWY47O45PYLN5T2OY7NJAVCNFSM6AAAAABVVFF2H6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNBVHA3TCMBWGA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

david-lev avatar Feb 24 '25 10:02 david-lev

Try V3 https://pywa.readthedocs.io/en/v3/content/templates/overview.html

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

Still got the same problem while creating template (video+body) template { "error": { "message": "Invalid parameter", "type": "OAuthException", "code": 100, "error_subcode": 2388043, "is_transient": false, "error_user_title": "Message template "components" param is missing expected field(s)", "error_user_msg": "component of type BODY is missing expected field(s) (example)", "fbtrace_id": "v7lcM1EEramdsCVemxJKDOD" } }

gaurdeveloper1916 avatar Oct 22 '25 00:10 gaurdeveloper1916

Still got the same problem while creating template (video+body) template { "error": { "message": "Invalid parameter", "type": "OAuthException", "code": 100, "error_subcode": 2388043, "is_transient": false, "error_user_title": "Message template "components" param is missing expected field(s)", "error_user_msg": "component of type BODY is missing expected field(s) (example)", "fbtrace_id": "v7lcM1EEramdsCVemxJKDOD" } }

@gaurdeveloper1916 what is your code?

yehuda-lev avatar Oct 22 '25 06:10 yehuda-lev