簡體   English   中英

如何使用 Whatsapp Cloud API 發送短信

[英]How to send a text message with Whatsapp Cloud API

我在使用 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,所以我想這不算作轉發。

我寫了一篇關於 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!"}
}'

官方 META-whatsapp 文檔表明,為了發送此類消息,對話必須由用戶發起 https://developers.facebook.com/docs/whatsapp/conversation-types

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM