Reputation: 140
Testing with Postman, I'm trying to send a message per the glitch example project from docs
I'm trying to write an API endpoint I can hit with a webhook when people send a message to my Org's whatsapp number. The API would send an automated response.
When I send the POST, with the following body to https://graph.facebook.com/v14.0/redacted/messages
it comes back with the following response:
{
"error": {
"message": "(#131030) Recipient phone number not in allowed list",
"type": "OAuthException",
"code": 131030,
"error_data": {
"messaging_product": "whatsapp",
"details": "Recipient phone number not in allowed list: Add recipient phone number to recipient list and try again."
},
"error_subcode": 2655007,
"fbtrace_id": "A5YKQbpB0PEaaA-gIROEv-n"
}
}
The error code isn't list one the error codes page, nor can I find anything about adding a recipient phone number anyway (it doesn't make sense to require a pre-defined list of recipient phone numbers to which I can send messages).
Here's the message body:
{
"messaging_product": "whatsapp",
"to": "redacted",
"text": {
"body": "Ack: Hello world"
}
}
How do I get the message sent? I'm not able to proceed with development of my app until I can send a message.
Upvotes: 4
Views: 12518
Reputation: 11
If the number you're trying to send a message to is on the recipient list, the issue might be with the number's format. In the Meta panel, you should enter the recipient's number in the following format:
format: +{countryCode}{regionalCode}{yourNumber}
For Brazil, we need to insert a '9' before our number. Therefore, a valid recipient number for us would be: +5551912365478 where the country code is +55, the regional code is 51, and the addition of the digit '9' after the recipient number which is 12365478.
When I prompt my WhatsApp test number, my phone number arrives at my webhook without the digit 9. So, it arrives like this: 555112365478.
I then have to add the digit '9' to make it look like this: 5551912365478
After that, that is the right number to send the messages.
Check if this approach would solve your problem.
Upvotes: 1
Reputation: 111
If it is a business initiated conversation then only template messages work. Meta must approve these templates before you can send them. There are two panels to consider:
You can find curl examples here, however you may only need a subpart of the code shown in those examples.
NOTE that for images,audio,etc you must first upload the media to Meta's cloud and then send the message with the respective media_id
Finally, since it was not easy to find, this is a working python sample code of how to upload and send images and which hopefully portrays up to which point of the curl examples i used as reference.
import requests
import json
# Define constants
WHATSAPP_ACCESS_TOKEN = 'EAAQjVNmUkZAYBO5YEnEYIALAQZCXwZDZD'
WHATSAPP_TEMPLATE_NAME = 'sexytemplate'
WHATSAPP_TEXT_LANGUAGE_CODE = 'es' #spanish
WHATSAPP_PHONE_ID = '328666666600774'
contacts = ['14433555555', '13476666666'] #up to 5 recipients if test number
image_path = "/path/to/image.jpg"
#Upload Media
url = f'https://graph.facebook.com/v20.0/{WHATSAPP_PHONE_ID}/media'
headers ={"Authorization": "Bearer "+WHATSAPP_ACCESS_TOKEN}
files ={'file': ('irrelevantsexyname', open(image_path, 'rb'), "image/jpeg")}
data ={'messaging_product': 'whatsapp'}
upload_response = requests.post(url, headers=headers, files=files, data=data)
print("[UPLOAD] Status Code:", upload_response.status_code)
print("[UPLOAD] Response Body:", upload_response.text)
upload_response = upload_response.json() #text section is now a python dict
#Send Media
url = f'https://graph.facebook.com/v20.0/{WHATSAPP_PHONE_ID}/messages'
headers = {
"Authorization": "Bearer "+WHATSAPP_ACCESS_TOKEN,
"Content-Type": "application/json"
}
data = { #must be JSON String!!
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": contacts[0], #first contact only
"type": "template",
"template": {
"name": WHATSAPP_TEMPLATE_NAME,
"language": {
"code": WHATSAPP_TEXT_LANGUAGE_CODE
},
"components": [
{
"type": "header",
"parameters": [
{
"type": "image",
"image": {
"id": upload_response['id']
}
}
]
}
]
}
}
msg_response = requests.post(url, headers=headers, data=json.dumps(data))
print("[SEND] Status Code:", msg_response.status_code)
print("[SEND] Response Body:", msg_response.text)
Upvotes: 0
Reputation: 409
I faced similar error, Found out that. I generated token and then assigned assets to it.
It should be the other way round. First assign assets (your app) and then generate token.
Upvotes: 0
Reputation: 21
The error for Postman comes from a mislabelling in the template file for the variable {{Recipient-WA-ID}}. The environment variable should be renamed to this from {{Recipient-Phone-Number}} (or vice-versa)
Upvotes: 2
Reputation: 1
I think you need to Add the Recipient number to the allowed number list before you send a message.
You Can find it at the following path: Your App> WhatsApp> Getting Started> To (Drop down)
Upvotes: 0
Reputation: 71
Just Remove + from your mobile number and add you country code before your number.
Then its working fine.
Request Body:
{
"messaging_product": "whatsapp",
"to": "91xxxxxxxxxx",
"type": "template",
"template": {
"name": "hello_world",
"language": {
"code": "en_US"
}
}
}
Response Body:
{
"messaging_product": "whatsapp",
"contacts": [
{
"input": "91xxxxxxxxxx",
"wa_id": "91xxxxxxxxxx"
}
],
"messages": [
{
"id": "wamid.HBgMOTE3NTM4OTE4MzIyFQIAERgSOERCM0ZDOTJFMDk1RjBFNURBAA=="
}
]
}
Upvotes: 7
Reputation: 16
I got the same error code and message here.
I was debugging it and see that the from number is a little different than the number I had registered when I will echo the message back.
You can put the number of phone hard coded that you have registered before to see it working.
Upvotes: 0