dsoy71058 2019-01-08 07:27
浏览 124
已采纳

twilio whatsapp api响应状态不正确

I am working with Twilio WhatsApp API. I have sandbox account and I am able to send messages to WhatsApp numbers from Twilio but, I am facing an issue that when the messages don't send to the recipient the status I am getting from the API is same as I am getting on success.

This is the response I am getting from the api, there is nothing specified about the success or failure.

{
"sid": "xxxxx",
"date_created": "Tue, 08 Jan 2019 09:42:38 +0000",
"date_updated": "Tue, 08 Jan 2019 09:42:38 +0000",
"date_sent": null,
"account_sid": "xxxxxx",
"to": "Whatsapp:xxxxxxx",
"from": "Whatsapp:+xxxxxxx",
"messaging_service_sid": null,
"body": "test",
"status": "queued",
"num_segments": "1",
"num_media": "0",
"direction": "outbound-api",
"api_version": "2010-04-01",
"price": null,
"price_unit": null,
"error_code": null,
"error_message": null,
"uri": "/2010-04-01/Accounts/xxxxxxxxxxxxxx/Messages/xxxxxxxxxxxxxxxxxx.json",
"subresource_uris": {
    "media": "/2010-04-01/Accounts/xxxxxxxxxxxxxxxxxx/Messages/xxxxxxxxxxxxxxxxxxxx/Media.json"
 }
}

I am using this API.

Here is my code if need.

    public function sendWhatsappMsg() {
    if ( ! empty( $this->apiKey ) && $this->apiKey != null && $this->apiKey != '' ) {
        if($this->apiKey == $_POST['apiKey']) {
            try {
                $to  = $_POST['to'];
                $msg = $_POST['msg'];
                if ( empty( $to ) && empty( $msg ) ) {
                    return array( 'Error' => 'Phone number and message is required!' );
                }
                if ( empty( $to ) ) {
                    return array( 'Error' => 'Phone number is required!' );
                }
                if ( empty( $msg ) ) {
                    return array( 'Error' => 'Message is required!' );
                }
                if(!empty($to) && !empty($msg)){
                    $isVaid = $this->verifyPhoneNumber($to);
                    if($isVaid['valid'] == false){
                        return array( 'Error' => 'Phone number should be valid!' );
                    }else{
                        $twilio = new Client( $this->sid, $this->token );
                        $message = $twilio->messages
                            ->create( "whatsapp:" . $isVaid['international_format'], // to
                                array(
                                    "from"           => "whatsapp:+14155238886",
                                    "body"           => $msg,
                                    "statusCallback" => "http://adwtpoc.digitalgravity.ae/Api.php?method=sendWhatsappMsgCallback"
                                )
                            );
                        return (array('accountSid'=>$message->accountSid,'messageServiceSid'=>$message->messagingServiceSid,'sid'=>$message->sid,'status'=>$message->status));
                    }
                }
            } //catch exception
            catch ( Exception $e ) {
                echo 'Exception: ' . $e->getMessage();
            }
        }else{
            header("HTTP/1.1 403 Forbidden" );
            die('403 Forbidden!');
        }
    }else{
        return array( 'Error' => 'No Api Key Defined!' );
    }
}
  • 写回答

1条回答 默认 最新

  • dongliantong3229 2019-01-09 06:10
    关注

    I have sorted it, I was getting messageSid in response so I used another API to get message details by its sid and I get the response what I want.

    Here is the code to get the message details

        public function getMessageStatus($messageSid){
        // Initialize CURL:
        $ch = curl_init('https://api.twilio.com/2010-04-01/Accounts/'.$this->AccountSid.'/Messages/'.$messageSid.'.json');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERPWD, $this->AccountSid . ":" . $this->token);
    
        // Store the data:
        $json = curl_exec($ch);
        curl_close($ch);
    
        // Decode JSON response:
        return json_decode($json, true);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条