繁体   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