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