我在使用 Whatsapp Cloud API(5 月 22 日向公眾發布)時遇到問題。我在“設定開發人員資產和平臺訪問”部分的入門中做了所有事情,這樣我就可以在 Ubuntu 20.04.4 LTS 中發送模板hello world了:
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" } }
}'
或使用Python 3.10并請求 2.27.1:
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)
然后,我嘗試用這個發送短信:
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)
而且,即使回應是正確的,也是這樣的:
{'messaging_product': 'whatsapp', 'contacts': [{'input': 'my_reciever', 'wa_id': 'my_reciever'}], 'messages': [{'id': 'wamid.HBgMNTchangingMDYyM0I2AA=='}]}
我在 my_reciver 中沒有收到任何訊息。我不知道我做錯了什么,我可能需要配置 webhook 才能正常作業?我是否需要在收到訊息之前選擇加入(可以在入門頁面中閱讀)?
我什至嘗試在 python 中使用一些非官方的包裝器heyoo,但我得到了相同的結果。
希望有人可以幫助我,謝謝。
注意:這是一篇類似的帖子,但它是使用節點的,而不是 Python 或 Curl,所以我想這不算作轉發。
uj5u.com熱心網友回復:
我已經寫了一篇關于 WhatsApp Cloud API 的簡短文章,例如如何發送和接收 WhatsApp 訊息以及設定永不過期的訪問令牌。請看一下WhatsApp Cloud API
您需要將 WhatsApp 訊息從您的個人號碼發送到您的 WhatsApp 業務號碼,然后您才能將訊息從您的業務號碼發送到您的個人號碼。基本上,WhatsApp 在 24 小時會話內有一個模板訊息概念,根據您的問題,我認為您正試圖將正常的非會話訊息從企業號碼發送到您的個人號碼。因此,為避免這種情況,您需要先從您的個人號碼向您的公司號碼發送訊息,然后您才能將訊息接收到您的個人號碼。文章中有關模板訊息的完整詳細資訊。
這是正常訊息的 CURL 請求
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!"}
}'
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/489578.html
上一篇:通過idAPI/URL獲取鏈接
