这是交易:
在 WhatsApp 云 API 中,如果您发起与用户的对话,则只能发送模板消息。
问题是,如果我尝试发送其他类型的消息(例如图像或纯文本),API 不会向我发送任何错误,它只是发送成功消息,但该消息并未真正传递给用户。
我如何知道我是否可以发送其他消息?
我正在使用 PHP 通过 cURL 发送消息,但我不知道这是否相关。
无论如何,这是代码:
只是为了澄清,此代码已经可以工作,我只是想知道当我无法发送短信时我需要做什么才能收到错误
class ApiWhatsapp
{
private $TOKEN = "";
private $VERSION = "";
private $PHONE_NUMBER_ID = "";
private $BUSINESS_ACCOUNT = "";
function __construct() {
global $conectado;
$select = "SELECT FIRST 1 * FROM IDSWHATS";
$arrayIds = $conectado->select($select);
foreach ($arrayIds as $ids) {
$this->TOKEN =$ids->TOKEN;
$this->VERSION =$ids->VERSION;
$this->PHONE_NUMBER_ID =$ids->PHONE_NUMBER_ID;
$this->BUSINESS_ACCOUNT =$ids->BUSINESS_ACCOUNT;
}
if (!$this->TOKEN) {
throw new Exception("credentials not found");
}
}
function sendMessageText($to, $text) {
$url = 'https://graph.facebook.com/'.$this->VERSION.'/'.$this->PHONE_NUMBER_ID.'/messages';
$data = [
"messaging_product" => "whatsapp",
"recipient_type" => "individual",
"to" => $to,
"type" => "text",
"text" => [
"preview_url" => false,
"body" => $text
]
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Authorization: Bearer " . $this->TOKEN
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
$resp = curl_exec($curl);
curl_close($curl);
return json_decode($resp);
}
}
我得到的成功响应:
{
"status": "success",
"message_id": "wamid.HBgMNTUzMTczNTgxNDUzFQIAERgSRjM3RDYzMUUyMkY2Rjk5OTJEAA==",
"response_array": {
"messaging_product": "whatsapp",
"contacts": [
{
"input": "5531973581453",
"wa_id": "553173581453"
}
],
"messages": [
{
"id": "wamid.HBgMNTUzMTczNTgxNDUzFQIAERgSRjM3RDYzMUUyMkY2Rjk5OTJEAA=="
}
]
}
}