首页 > 解决方案 > twilio whatsapp api 响应状态不正确

问题描述

我正在使用 Twilio WhatsApp API。我有沙盒帐户,并且可以从 Twilio 向 WhatsApp 号码发送消息,但是我面临一个问题,即当消息未发送给收件人时,我从 API 获得的状态与我获得成功的状态相同.

这是我从 api 得到的响应,没有指定成功或失败。

{
"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"
 }
}

我正在使用这个API

如果需要,这是我的代码。

    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!' );
    }
}

标签: phpapitwilio

解决方案


我已经对其进行了排序,我得到messageSid了响应,所以我使用另一个 API 通过它的 sid 获取消息详细信息,我得到了我想要的响应。

这是获取消息详细信息的代码

    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);
}

推荐阅读