【问题标题】:sending message from Facebook whatsapp api to my number从 Facebook whatsapp api 发送消息到我的号码
【发布时间】:2023-02-24 19:26:40
【问题描述】:

我正在从 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;

【问题讨论】:

  • 如果您仍在寻找答案,我有一个可用的示例代码。
  • @RiyaSingh 我在找它,你能和我们分享一下它是怎么做到的吗
  • 我刚刚在下面回答了...查看下面的帖子“这是一个示例代码,希望它有所帮助。它对我有用 记住要点 必须提及模板名称。消息以批准的模板为准”
  • 是的...您需要在 developer.facebook.com > 我的应用程序上创建一个应用程序,然后单击 whatsapp > 快速启动。添加一个数字,您将获得所有访问令牌等

标签: php facebook-graph-api whatsapp


【解决方案1】:

同样的问题在这里。从开发控制台模板发送,同样的问题。 改手机号码后,就可以了。我不知道是否会在生产环境中工作,但是......

【讨论】:

  • 这看起来像评论,而不是答案答案也不清楚
  • 我有同样的问题,但是更改电话号码后有效,因此,解决方案:更改电话号码!
  • 有没有人找到解决方案?更改电话号码不是解决方案,因为到目前为止我已经更改了 5。
【解决方案2】:

嘿,我遇到了同样的问题,解决方案是将我手机上的 whatsapp 升级到最新版本。我在 Ruby 库中添加了一些 troubleshooting issues 可以帮助解决类似的问题。

【讨论】:

    【解决方案3】:

    这是一个示例代码,希望对您有所帮助。这个对我有用

    要记住的点必须提及模板名称。消息以批准的模板为准

         $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“传递组件”

    How to send the components in whatsapp cloud api?

    【讨论】:

      【解决方案4】:

      这个 Ccode 非常适合我!谢谢你里亚。

      但我正在尝试将一个参数发送到我创建的模板,但没有成功。 你能告诉我使用这段代码是如何完成的吗?

      【讨论】:

      猜你喜欢
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2013-09-04
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多