Неверный параметр для WhatsApp Cloud API?Python

Программы на Python
Ответить
Anonymous
 Неверный параметр для WhatsApp Cloud API?

Сообщение Anonymous »

Я хоть убей не могу понять, что я делаю не так. Я тестировал с помощью Graph Explorer, и та же самая полезная нагрузка не работает в Python. Он ссылается на параметр '', которого нет в теле.
Созданный мною шаблон служебного сообщения состоит из двух текстовых компонентов:
def send_template_message(
self,
phone_number: str,
template_name: str,
language_code: str,
template_args: list = [],
):
# API endpoint
url = (
f"https://graph.facebook.com/v21.0/{self. ... }/messages"
)

# Construct the payload
payload = {
"messaging_product": "whatsapp",
"to": phone_number,
"type": "template",
"template": {
"name": template_name,
"language": {"code": language_code},
},
}

if template_args:
payload["template"]["components"] = [
{
"type": "body",
"parameters": [json.dumps(arg) for arg in template_args],
}
]

# Headers
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.whatsapp_api_token}",
}

# Log payload and headers for debugging
logging.info(f"Payload being sent: {payload}")
logging.info(f"Headers being sent: {headers}")

# Send the POST request
response = requests.post(url, headers=headers, json=payload)

# Handle response
if response.status_code != 200:
logging.error(
f"Error sending template message: {response.text}", exc_info=True
)
raise MessageSendError(f"Error sending template message: {response.text}")
return response.json()

Отправляемая полезная нагрузка:
{'messaging_product': 'whatsapp', 'to': '525555555555', 'type': 'template', 'template': {'name': 'rsvp_1', 'language': {'code': 'es'}, 'components': [{'type': 'body', 'parameters': ['{"type": "text", "text": "Hello"}', '{"type": "text", "text": "Hello"}']}]}}

Отправляемые заголовки:
{'Content-Type': 'application/json', 'Authorization': 'Bearer token'}

Ответ:
Ошибка отправки шаблонного сообщения:
{"error":{
"message":"(#100) Invalid parameter",
"type":"OAuthException",
"code":100,
"error_data":{
"messaging_product":"whatsapp","details":"For component BODY, parameter '' does not have a valid phone_number format. Length Limit is 20 characters and detailed format requirements can be found at https:\/\/developers.facebook.com\/docs\/whatsapp\/cloud-api\/guides\/send-message-templates\/utility-templates#template-parameters-and-restrictions."},
"fbtrace_id":"APKhyoUByZY40ni5OA6Nz9o"}
}

Точно такой же запрос в Graph Explorer возвращает это
{
"messaging_product": "whatsapp",
"contacts": [
{
"input": "525555555555",
"wa_id": "525555555555"
}
],
"messages": [
{
"id": "wamid.HBgLMTk3O2323TI1MDc1MzIVAgARGBIwMDQ0NzM3MzQ4MjNERDA2MjIA",
"message_status": "accepted"
}
]
}


Подробнее здесь: https://stackoverflow.com/questions/792 ... -cloud-api
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»