skip to Main Content

I’m having troubles using Whatsapp Cloud API (which was released to the public on May 22). I did everything in the getting started in "Set up Developer Assets and Platform Access" section, that way I was able to send the template hello world in Ubuntu 20.04.4 LTS with:

curl -i -X POST 
https://graph.facebook.com/v14.0/my_number/messages 
-H 'Authorization: Bearer my_token' 
-H 'Content-Type: application/json' 
-d '{ "messaging_product": "whatsapp",
  "to": "my_reciever",
  "type": "template",
  "template": { "name": "hello_world", "language": { "code": "en_US" } }
  }'

or with Python 3.10 and requests 2.27.1 with:

from requests import Session
import json
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects

BASE_URL = "https://graph.facebook.com/"
API_VERSION = "v13.0/"
SENDER = "my_number/"
ENDPOINT = "messages"
URL = BASE_URL + API_VERSION + SENDER + ENDPOINT
API_TOKEN = "my_token"
TO = "my_reciever"
headers = {
    "Authorization": f"Bearer {API_TOKEN}",
    "Content-Type": "application/json"
}
parameters = {
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": TO,
    "type": "template",
    "template": {"name": "hello_world", "language": {"code": "en_US"}}
}
session = Session()
session.headers.update(headers)
try:
    response = session.post(URL, json=parameters)
    data = json.loads(response.text)
    print(f"data: {data}")
except (ConnectionError, Timeout, TooManyRedirects) as e:
    print(e)

Then, I tried to send a text message with this:

from requests import Session
import json
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects

BASE_URL = "https://graph.facebook.com/"
API_VERSION = "v13.0/"
SENDER = "my_number/"
ENDPOINT = "messages"
URL = BASE_URL + API_VERSION + SENDER + ENDPOINT
API_TOKEN = "my_token"
TO = "my_reciever"
headers = {
    "Authorization": f"Bearer {API_TOKEN}",
    "Content-Type": "application/json"
}
parameters = {
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": TO,
    "type": "text",
    "text": {
        "preview_url": "false",
        "body": "MESSAGE_CONTENT"
    }
}
session = Session()
session.headers.update(headers)
try:
    response = session.post(URL, json=parameters)
    data = json.loads(response.text)
    print(f"data: {data}")
except (ConnectionError, Timeout, TooManyRedirects) as e:
    print(e)

And, even though the response is correct, something like this:

{'messaging_product': 'whatsapp', 'contacts': [{'input': 'my_reciever', 'wa_id': 'my_reciever'}], 'messages': [{'id': 'wamid.HBgMNTchangingMDYyM0I2AA=='}]}

I don’t recieve any message in my_reciver. I don’t know what I’m doing wrong, I might have to configure the webhook for this to work? Do I need to opt-in before recieve the message (this can be read in get-started page)?

I even tried using some unofficial wrappers in python like heyoo, but I got the same result.

Hope someone can help me with this, thanks.

Note: this is a similar post, but that one is with node, not Python or Curl, so I guess this does not count as repost.

2

Answers


  1. I have written a brief article on WhatsApp Cloud API like how to send and receive WhatsApp messages and also set up a never expiry access token. Please have a look WhatsApp Cloud API

    You need to send the WhatsApp message from your personal number to your WhatsApp business number after that you can send the message from your business number to your personal number. Basically, WhatsApp has a template message concept within 24h session and according to your question, I think you are trying to send a normal unsession message from a business number to your personal number. So, to avoid this case you need to first message from your personal to your business number then you can receive the message to your personal number. Full details in the article regarding template message.

    Here is the CURL request for normal message

    curl --location --request POST 'https://graph.facebook.com/v13.0/<Your Phone number ID>/messages' 
    --header 'Authorization: Bearer <Your Temporary access token>' 
    --header 'Content-Type: application/json' 
    --data-raw '{"messaging_product":"whatsapp","recipient_type":"individual",
    "to":"918587808915","type":"text","text": {"body":"Hello Rishabh!"}
    }'
    
    Login or Signup to reply.
  2. The official META-whatsapp documentation indicates that in order to send such messages, the conversation must be initiated by the user. https://developers.facebook.com/docs/whatsapp/conversation-types

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search