【问题标题】:How to send a text message with Whatsapp Cloud API如何使用 Whatsapp Cloud API 发送短信
【发布时间】:2022-06-21 12:15:39
【问题描述】:

我在使用 Whatsapp Cloud API(5 月 22 日向公众发布)时遇到问题。我在“设置开发人员资产和平台访问”部分的getting started 中做了所有事情,这样我就可以在 Ubuntu 20.04.4 中发送模板 hello world LTS 与:

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.10requests 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)

然后,我尝试用这个发送text message

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,但得到了相同的结果。

希望有人能帮我解决这个问题,谢谢。

注意:this 是一个类似的帖子,但那是一个节点,而不是 Python 或 Curl,所以我想这不算作转发。

【问题讨论】:

  • 我有同样的问题,我找不到解决方案,我不知道这是 WhatsApp Api 的问题还是我做错了什么。我已将问题添加为书签,希望您能得到答复。

标签: python api curl request whatsapp


【解决方案1】:

我写了一篇关于 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!"}
}'

【讨论】:

    【解决方案2】:

    官方 META-whatsapp 文档指出,为了发送此类消息,对话必须由用户发起https://developers.facebook.com/docs/whatsapp/conversation-types

    【讨论】:

      猜你喜欢
      • 2015-12-15
      • 2022-06-14
      • 1970-01-01
      • 2017-06-29
      • 2022-07-13
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 2016-01-08
      相关资源
      最近更新 更多