将消息从Facebook WhatsApp API发送到我的电话号码

发布于 2025-02-08 04:37:10 字数 1309 浏览 2 评论 0原文

我将来自Facebook WhatsApp API的消息发送到我的电话号码进行测试。消息是从PHP发送的,但未在移动设备上收到。我必须在Facebook上注册业务吗?

{"messaging_product":"whatsapp","contacts":[{"input":"923040165804","wa_id":"923040165804"}],"messages":[{"id":"wamid.HBgMOTIzMDQwMTY1ODA0FQIAERgSOEM3RDJDRDMyMkFENkIzMTgyAA==" }]}

我正在使用的此PHP代码。

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://graph.facebook.com/v13.0/********/messages/',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS =>json_encode(array("to"=> $number, "messaging_product" => 'whatsapp', "recipient_type" => 'individual',"type"=>"text", 'text'=> array('body'=>'hello_wo332233rld','preview_url'=>'false'))),
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$chatApiToken,
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);
        echo $response;

I am sending message from Facebook whatsapp api to my number for testing. Message is sent from php but not recieved on mobile. Did I must register business on facebook?

{"messaging_product":"whatsapp","contacts":[{"input":"923040165804","wa_id":"923040165804"}],"messages":[{"id":"wamid.HBgMOTIzMDQwMTY1ODA0FQIAERgSOEM3RDJDRDMyMkFENkIzMTgyAA=="}]}

this php code that I am using.

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://graph.facebook.com/v13.0/********/messages/',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS =>json_encode(array("to"=> $number, "messaging_product" => 'whatsapp', "recipient_type" => 'individual',"type"=>"text", 'text'=> array('body'=>'hello_wo332233rld','preview_url'=>'false'))),
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$chatApiToken,
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);
        echo $response;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年 2025-02-15 04:37:10

同样的问题。并从Dev Console模板发送,也是同一问题。
更改电话号码后,它起作用了。我不知道是否会在生产环境中工作,但是...

Same issue here. And sending from dev console template, same issue too.
After change the phone number, it has worked. I don't know if will work at the production environment, but...

眉目亦如画i 2025-02-15 04:37:10

嘿,我遇到了同样的问题,解决方案是将WhatsApp升级到手机上的最新版本。我添加了一些故障排除问题 可能会帮助遇到类似问题的Ruby库。

Hey I came across the same problem and the solution was to upgrade whatsapp to the latest version on my phone. I added some troubleshooting issues to the Ruby library that could help with similar problems.

夏の忆 2025-02-15 04:37:10

这是示例代码,希望它有帮助。它对我有用

要记住
提及模板名称是强制性的。消息受批准的模板的约束,

     $number = $_GET['number']; //you can use POST, I tried GET for testing
     $template = array(
       'name'=>'hello_world', //your your own or any default template. The names and samples are listed under message templates
       'language'=>array('code'=>'en_us') //you can use yours
       );

     $endpoint = 'https://graph.facebook.com/v15.0/USE_YOUR_OWN/messages';
     $params = array('messaging_product'=>'whatsapp', 'to'=>$number, 'type'=>'template', 'from'=>'91xxxxxxxxxx', 'access_token'=>'YOUR_ACCESS_TOKEN','template'=>json_encode($template));

       $headers = array('Authorization'=>'YOUR_ACCESS_TOKEN','Content-Type'=>'application/json', 'User-Agent'=>'(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36');
       $url = $endpoint . '?' . http_build_query($params);
  //echo $params.'<br>';
       $ch = curl_init();
       curl_setopt( $ch,CURLOPT_URL, $endpoint);
       curl_setopt( $ch,CURLOPT_POST, true );
       curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
       curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
       curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
       curl_setopt( $ch,CURLOPT_POSTFIELDS, $params);
       $result = curl_exec($ch );
       echo $result; //you can skip this, I did it to check the results
       curl_close( $ch );

这最终与结果相呼应,

{"messaging_product":"whatsapp","contacts":[{"input":"15645446547","wa_id":"15645446547"}],"messages":[{"id":"wamid.HBgLMTU2NDU0NDY1NDcVAgARGBIwNjJEMjc3NzIzQzA2RjY2QTYA"}]}

并收到消息。

请参阅有关如何传递参数aka的以下线程,也就是“传递组件”

如何在WhatsApp Cloud API中发送组件?

Here is a sample code, hope it helps. It works for me

Point to remember
Mentioning a template name is mandatory. The messages are subject to the approved templates

     $number = $_GET['number']; //you can use POST, I tried GET for testing
     $template = array(
       'name'=>'hello_world', //your your own or any default template. The names and samples are listed under message templates
       'language'=>array('code'=>'en_us') //you can use yours
       );

     $endpoint = 'https://graph.facebook.com/v15.0/USE_YOUR_OWN/messages';
     $params = array('messaging_product'=>'whatsapp', 'to'=>$number, 'type'=>'template', 'from'=>'91xxxxxxxxxx', 'access_token'=>'YOUR_ACCESS_TOKEN','template'=>json_encode($template));

       $headers = array('Authorization'=>'YOUR_ACCESS_TOKEN','Content-Type'=>'application/json', 'User-Agent'=>'(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36');
       $url = $endpoint . '?' . http_build_query($params);
  //echo $params.'<br>';
       $ch = curl_init();
       curl_setopt( $ch,CURLOPT_URL, $endpoint);
       curl_setopt( $ch,CURLOPT_POST, true );
       curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
       curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
       curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
       curl_setopt( $ch,CURLOPT_POSTFIELDS, $params);
       $result = curl_exec($ch );
       echo $result; //you can skip this, I did it to check the results
       curl_close( $ch );

This finally echos the result as

{"messaging_product":"whatsapp","contacts":[{"input":"15645446547","wa_id":"15645446547"}],"messages":[{"id":"wamid.HBgLMTU2NDU0NDY1NDcVAgARGBIwNjJEMjc3NzIzQzA2RjY2QTYA"}]}

And the message is getting received.

refer the following thread on how to pass a parameter AKA "passing components"

How to send the components in whatsapp cloud api?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文