简体   繁体   English

如何使用 Whatsapp Cloud API 发送短信

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

I'm having troubles using Whatsapp Cloud API (which was released to the public on May 22).我在使用 Whatsapp Cloud API(5 月 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:我在“设置开发人员资产和平台访问”部分的入门中做了所有事情,这样我就可以在 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" } }
  }'

or with Python 3.10 and requests 2.27.1 with:或使用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)

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.我在 my_reciver 中没有收到任何消息。 I don't know what I'm doing wrong, I might have to configure the webhook for this to work?我不知道我做错了什么,我可能需要配置 webhook 才能正常工作? 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.我什至尝试在 python 中使用一些非官方的包装器heyoo ,但我得到了相同的结果。

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.注意: 是一篇类似的帖子,但它是使用节点的,而不是 Python 或 Curl,所以我想这不算作转发。

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.我写了一篇关于 WhatsApp Cloud API 的简短文章,比如如何发送和接收 WhatsApp 消息,以及如何设置永不过期的访问令牌。 Please have a look WhatsApp Cloud API请看一下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.您需要将 WhatsApp 消息从您的个人号码发送到您的 WhatsApp 业务号码,然后您才能将消息从您的业务号码发送到您的个人号码。 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.基本上,WhatsApp 在 24 小时会话内有一个模板消息概念,根据您的问题,我认为您正试图将正常的非会话消息从企业号码发送到您的个人号码。 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 请求

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!"}
}'

The official META-whatsapp documentation indicates that in order to send such messages, the conversation must be initiated by the user .官方 META-whatsapp 文档表明,为了发送此类消息,对话必须由用户发起 https://developers.facebook.com/docs/whatsapp/conversation-types https://developers.facebook.com/docs/whatsapp/conversation-types

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM