question

Theresa Theresa avatar image
Theresa Theresa asked Phong Vu answered

PHP Webhook Subscription Sandbox vs Production Different Behavior on Sending and Receiving SMS

Hi There,

We have been able to successfully create, delete webhook subscriptions using PHP and REST API. Everything was working perfectly fine until we had our app published for production and we have noticed a strange behavior or unexpected behavior which is not present in Sandbox mode.

In Sandbox: We sent a sms which works fine and once someone replies we get that replies from Webhook and process it as required.

In Production: Once we sent an sms our webhook automatically gets called for the sent SMS and is responding back with the data of sent SMS not received. That behavior is not present in Sandbox mode and our webbook is only being called once we have a reply from someone.

Can you please help me understand why is that and where is something wrong? Why we have different behavior from the same app and account.

Shouldn't this be the same as Sandbox. Webhook should only be called once we have replies against registered events of instant sms?


Thanks,

Yasir

sandboxphpproductionrestapiwebhook subscription
1 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered

Your question is clear except the problem described in the Production paragragh.

In Production: Once we sent an sms our webhook automatically gets called for the sent SMS and is responding back with the data of sent SMS not received. That behavior is not present in Sandbox mode and our webbook is only being called once we have a reply from someone.

Can you clarify this "responding back with the data of sent SMS not received ?" and "and our webbook is only being called once we have a reply from someone."

If you can't explain differently, it would be helpful to post the code where you implement the incoming message handling and sending a message back to the sender etc.

1 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Theresa Theresa avatar image
Theresa Theresa answered Phong Vu edited

Hi @Phong Vu,

I hope you are doing good. Let me explain here.

Sandbox Behavior:

  1. We can send a sms and our webhook we have created is not called once we sent an sms.
  2. We can receive a SMS. Here our webbook is called with incoming message data.

That is expected beviour and working fine. We can send and receive as many SMS we want. Webhook is only being called/invoked once someone replies to any of the sent sms. Make sense?


Production Behavior:

  1. We can send a sms and our webhook gets called and sends the data back to us which we sent as a "Sent SMS". Should that happen? We are not receiving the sent SMS as is back to us on our call back once we send it during Sandbox mode.
  2. We can receive a SMS. Here our webhook is called with incoming message data.

That is expected beviour and working fine in case of incoming/receiving message.

Below is the working code of Webhook:

<?php 
if (isset($_REQUEST['webhookcallback'])) { 
  if (array_key_exists('HTTP_VALIDATION_TOKEN', $_SERVER)) { 
    $response = new Response(); 
    $response->setStatusCode(Response::STATUS_CODE_200); 
    $response->getHeaders()->addHeaders(array( 'Validation-Token' => $_SERVER['HTTP_VALIDATION_TOKEN'], )); 
    return $response->setContent("Validation-Token: Returned"); 
}else{ 
  //We have are processing the Received Data here 
  // This portion should be called only if we have an incoming/Receiving SMS? 
  //Processing the webhook Received Data $firstParam = ""; 
  // Query Params we sent while we created the subscription $secondParam = "";
  // Query Params we sent while we created the subscription 
  $jsonStr = file_get_contents('php://input'); 
  $jsonObj = json_decode($jsonStr, TRUE); 
  // Below is our business Logic to deal with SMS. 
}


Below is the working code of Creating the Webhook Subscription:

$body = array(
 'eventFilters' => array(
 '/restapi/v1.0/account/~/extension/~/message-store/instant?type=SMS' 
 ),
 'deliveryMode' => array(
 'transportType' => 'WebHook',
 'address' => $webhookUrl,
 'encryption' => false,
 ),
 'expiresIn' => 630720000
 );
 $params = json_encode($body);
 
 try {
   $part_uri = '/restapi/v1.0/subscription';
   //Set the content type to application/json
   $headers[] = 'Authorization: Bearer ' . $_SESSION['rAccessCode'];
   $headers[] = 'Content-Type: application/json';
 
   //Initiate cURL.
   $ch = curl_init($this->_api_uri . $part_uri);
   //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
   curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_TIMEOUT, 20);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

   //Execute the request
   $result = curl_exec($ch);
   return json_decode($result
 } catch (Zend_Rest_Client_Exception $e) {
   $errors[] = '[' . $e->getCode() . ']:' . $e->getMessage();
 } catch (Exception $e) {
   $errors[] = '[' . $e->getCode() . ']:' . $e->getMessage();
 }
 if ($errors) {
   print_r($errors);
   exit;
 }

Please guide us through this and let me know if you have any more questions or suggestions?

thanks

Yasir

1 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Theresa Theresa avatar image
Theresa Theresa answered Phong Vu commented

I'm Sorry while Posting your editor seemed to be working fine and was displaying code as if its in coding editor. But once its posted. I see the weiredness of the view now. But you can copy it and see it. As its working example.

1 comment
1 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image Phong Vu ♦♦ commented ·

I reformat your code to make it readable. Next time, click the </> code block sign and post the code within code block.

See my answer in the next answer thread.

0 Likes 0 ·
Phong Vu avatar image
Phong Vu answered

Based on the filter you specified for your webhook notification below:

$body = array(
 'eventFilters' => array(
 '/restapi/v1.0/account/~/extension/~/message-store/instant?type=SMS' 
 ),

You will receive SMS notification (with the message body) whenever there is an incoming SMS to ANY PHONE NUMBERS belong to the "user extension" (the user who logged in the app).

I can confirm that this filter will not subscribe for outgoing SMS notification. => That is why you confirmed it works correctly on your Sandbox account.

I suspect that in your production test, you sent a test message to one of the phone numbers which belongs to the same user who is being logged in your app.

To double check this, use your personal mobile number, or even use the phone number of your sandbox to send an SMS message to the phone number of the user (who is logged in the app). And reply to the sandbox number.

If this still does not work. Give me your app client id and the user extension number (for example 102, not the phone number) so I can have a look at it.

1 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.