FusionPBX SMS Integration with Bandwidth

Status
Not open for further replies.

mak_make

Member
Aug 27, 2018
53
0
6
42
I found some code in the sms_email.php that has a little different syntax for array access. If the above doesn't work, try this:
PHP:
        if ($debug) {
            error_log('[SMS] REQUEST: ' .  print_r($data, true));
            if (gettype($data)=="array") {
                error_log('[SMS] data-type(from array): ' .  print_r($data[0]->type));
                //error_log('[SMS] data-type(from array): ' .  print_r($data[0]['type']));
                //error_log('[SMS] data-type: ' .  print_r($data->type, true));
            }
        }
When I use this code I am getting callbacks on outbound messages. Below is the error log:

PHP message: [SMS] REQUEST: Array
(
[0] => stdClass Object
(
[time] => 2021-03-31T23:44:16.875Z
[type] => message-delivered
[to] => +1XXXXXXXXX
[description] => ok
[message] => stdClass Object
(
[id] => 1617234255871xccipaw5tn6ey662
[owner] => +1XXXXXXXXX
[applicationId] => XXXXXXXXXXXX
[time] => 2021-03-31T23:44:15.871Z
[segmentCount] => 1
[direction] => out
[to] => Array
(
[0] => +1XXXXXXXXX
)

[from] => +1XXXXXXXXX
[text] =>
[tag] =>
)

)

)

PHP message: [SMS] data-type(from array): 1
PHP message: TO: 0
PHP message: FROM: 0
PHP me


Incoming messages I am getting blank. below is the incoming message error log:

2021/03/31 18:44:44 [error] 582#0: *185 FastCGI sent in stderr: "PHP message: [SMS] REQUEST: Array
(
[USER] => www-data
[HOME] => /var/www
[HTTP_CONTENT_TYPE] => application/json; charset=utf-8
[HTTP_USER_AGENT] => bandwidth-api
[HTTP_ACCEPT_ENCODING] => gzip
[HTTP_CONNECTION] => Keep-Alive
[HTTP_HOST] => call.teleprime.com
[HTTP_CONTENT_LENGTH] => 395
[SCRIPT_FILENAME] => /var/www/fusionpbx/app/sms/hook/sms_hook_bandwidth.php
[REDIRECT_STATUS] => 200
[SERVER_NAME] => fusionpbx
[SERVER_PORT] => 443
[SERVER_ADDR] => 158.69.31.48
[REMOTE_PORT] => 44680
[REMOTE_ADDR] => 52.72.24.132
[SERVER_SOFTWARE] => nginx/1.6.2
[GATEWAY_INTERFACE] => CGI/1.1
[HTTPS] => on
[SERVER_PROTOCOL] => HTTP/1.1
[DOCUMENT_ROOT] => /var/www/fusionpbx
[DOCUMENT_URI] => /app/sms/hook/sms_hook_bandwidth.php
[REQUEST_URI] => /app/sms/hook/sms_hook_bandwidth.php
[SCRIPT_NAME] => /app/sms/hook/sms_hook_bandwidth.php
[CONTENT_LENGTH] => 395
[CONTENT_TYPE] => application/json; charset=utf-8
[REQUEST_ME...
PHP message: [SMS] REQUEST: Array
(
[0] => stdClass Object
(
[time] => 2021-03-31T23:44:43.209Z
[type] => message-received
[to] => +1XXXXXXXXXXXXXXXXXXXX
[description] => Incoming message received
[message] => stdClass Object
(
[id] => 6eee7880-6b12-4cfa-b149-475a67ceda9f
[owner] => +1XXXXXXXXXXXXXXX
[applicationId] => XXXXXXXXXXXXXXXXX
[time] => 2021-03-31T23:44:43.112Z
[segmentCount] => 1
[direction] => in
[to] => Array
(
[0] => +XXXXXXXXXX
)

[from] => +1XXXXXX
[text] => Code 2 inbound
)

)

)

PHP message: [SMS] data-type(from array): 1
PHP message: TO: 0
PHP message: FR
2021/03/31 18:44:45 [error] 582#0: *180 FastCGI sent in stderr: "PHP message: SQL: select domain_name, extension, sms_message_uuid,start_stamp,from_number,to_number,message,direction from v_sms_messages, v_domains, v_extensions where v_sms_messages.domain_uuid = v_domains.domain_uuid and v_sms_messages.extension_uuid = v_extensions.extension_uuid and v_domains.domain_uuid = :domain_uuid order by start_stamp DESC" while reading response header from upstream, client: 49.204.180.26, server: fusionpbx, request: "GET /app/sms/sms_mdr.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.1-fpm.sock:", host: "call.teleprime.com", referrer: "https://call.teleprime.com/app/sms/sms.php"
 
Jan 9, 2018
152
16
18
54
After looking a little closer at what you are getting from Bandwidth, I'm amazed that inbound is working at all. It looks like they've changed the data layout from the way this was set up originally. In theory, the following code should match what they are sending now (make sure to make a copy of what you are using now, just in case this breaks something):
PHP:
<?php

include "../root.php";

require_once "resources/require.php";
require_once "../sms_hook_common.php";

if ($debug) {
    error_log('[SMS] REQUEST: ' .  print_r($_SERVER, true));
}

if (check_acl()) {
    if  ($_SERVER['CONTENT_TYPE'] == 'application/json; charset=utf-8') {
        $data = json_decode(file_get_contents("php://input"));
        if ($debug) {
            error_log('[SMS] REQUEST: ' .  print_r($data, true));
            error_log('[SMS] data-type: ' .  print_r($data[0]->type, true));
            error_log('[SMS] data-from: ' .  print_r($data[0]->message->from, true));
            error_log('[SMS] data-to: ' .  print_r($data[0]->to, true));
            error_log('[SMS] data-text: ' .  print_r($data[0]->message->text, true));
        }
        if ($data[0]->type != "message-delivered") {
            //route_and_send_sms($data->from, $data->to, $data->text);
            route_and_send_sms($data[0]->message->from, $data[0]->to, $data[0]->message->text); //--looks like this one should be correct
        }
    } else {
        error_log('[SMS] REQUEST: No SMS Data Received');
        die("no");
    }
} else {
    error_log('ACCESS DENIED [SMS]: ' .  print_r($_SERVER['REMOTE_ADDR'], true));
    die("access denied");
}
?>

If this works, you'll probably want to comment out the extra logging--all but the first error_log line in the debug section.

Incidentally, this raises the question of whether Bandwidth will send multiple inbound messages in the same operation, since the inbound payload is now an array. The code above just extracts the first message in the array for processing. I'm thinking in particular if you send a very long message inbound and it gets broken into multiple SMS messages. It might arrive in a multi-member array and with the code as it is, you would lose all but the first part. You could test this by sending yourself a 200 character message and see if it all shows up.
 

mak_make

Member
Aug 27, 2018
53
0
6
42
Hi Jonathan,

Thanks for helping me out. The code works perfectly. inbound and outbound sms is working now. Also when I send an outbound message 200 character it was delivering to the recipient as two messages.

When I test the inbound with 200 character message I got the full text in single message it self. I have tested the inbound with Bria softphone, I will have to check inbound with Yealink T54.

I don't know why but an incoming message (short message) which I have sent through vitelity is keep delivering to me multiple times. I am checking with Bandwidth and Vitelity on it. still here is the error log I see in the server:

2021/04/01 09:08:07 [error] 583#0: *1685 FastCGI sent in stderr: "PHP message: [SMS] REQUEST: Array
(
[USER] => www-data
[HOME] => /var/www
[HTTP_CONTENT_TYPE] => application/json; charset=utf-8
[HTTP_USER_AGENT] => bandwidth-api
[HTTP_ACCEPT_ENCODING] => gzip
[HTTP_CONNECTION] => Keep-Alive
[HTTP_HOST] => call.teleprime.com
[HTTP_CONTENT_LENGTH] => 418
[SCRIPT_FILENAME] => /var/www/fusionpbx/app/sms/hook/sms_hook_bandwidth.php
[REDIRECT_STATUS] => 200
[SERVER_NAME] => fusionpbx
[SERVER_PORT] => 443
[SERVER_ADDR] => 158.69.31.48
[REMOTE_PORT] => 45158
[REMOTE_ADDR] => 18.233.250.246
[SERVER_SOFTWARE] => nginx/1.6.2
[GATEWAY_INTERFACE] => CGI/1.1
[HTTPS] => on
[SERVER_PROTOCOL] => HTTP/1.1
[DOCUMENT_ROOT] => /var/www/fusionpbx
[DOCUMENT_URI] => /app/sms/hook/sms_hook_bandwidth.php
[REQUEST_URI] => /app/sms/hook/sms_hook_bandwidth.php
[SCRIPT_NAME] => /app/sms/hook/sms_hook_bandwidth.php
[CONTENT_LENGTH] => 418
[CONTENT_TYPE] => application/json; charset=utf-8
[REQUEST_...
PHP message: [SMS] REQUEST: Array
(
[0] => stdClass Object
(
[time] => 2021-04-01T14:00:28.204Z
[type] => message-received
[to] => +1XXXXXXXXXXXX
[description] => Incoming message received
[message] => stdClass Object
(
[id] => a727d0b9-91bf-44aa-818a-196bdf04e81a
[owner] => +1XXXXXXXXXXXXXXXX
[applicationId] => XXXXXXXXXXXXXXXXXXX
[time] => 2021-04-01T14:00:28.129Z
[segmentCount] => 1
[direction] => in
[to] => Array
(
[0] => +1XXXXXXXXX
)

[from] => +1XXXXXXXX
[text] => Test short message for multi delivery
)

)

)

PHP message: [SMS] data-type: message-received
PHP mes
2021/04/01 09:08:07 [error] 583#0: *1685 upstream sent too big header while reading response header from upstream, client: 18.233.250.246, server: fusionpbx, request: "POST /app/sms/hook/sms_hook_bandwidth.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.1-fpm.sock:", host: "call.teleprime.com"


I have also received an incoming short message from a T-mobile cell but till now I have not got any duplicate message delivery.
 

mak_make

Member
Aug 27, 2018
53
0
6
42
I have asked the T-Mobile subscriber to send a long text message and for that also single word (Long Message) keep coming as duplicate message.
 

ict2842

Member
Mar 2, 2021
140
11
18
Wichita, KS
Sorry, I'm using Telnyx API v1. But I thought I remembered someone in that other very long thread mentioning that they got it working with Telnyx v2 API
Sorry to butt in. Did you put your settings & files somewhere? I tried toying with Telnyx and I ended up giving up, putting it on the "to mess with later" list.
 

mak_make

Member
Aug 27, 2018
53
0
6
42
Jonathan,

below is the response I got from Bandwidth about duplicate delivery:

2021-04-01 00:16:16.151 ERROR [callback-consumer-worker-406] [CallbackSender.java:49] Temporary HTTP error sending callback: httpStatusCode=[500], rateLimit(/sec)=[1000.0], callback=[ImmutableCallback{uri=https://XXXXXXXXXXX/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=92f15299-3d4a-4d0a-99b3-f1367d1b5ed5, eventType=message-received}, headers=[]}]

If you notice, the log has httpStatusCode=[500] error. In my experience, that happens when a message was delivered, but your callback server failed to return 200 OK status. In such a scenario, Bandwidth thinks that there was some issue, so try to deliver the same message again hoping to get a 200 OK from the server. Until we get 200 OK from the server, we keep retrying for a 24 hour period. In your case, you are receiving a message on a handset, but the callback server is not responding 200 OK, so we are trying to send the message again, causing duplicate messages on your handset.

I hope the above information helps. If you need me to send you logs for the examples you have provided, please send me messageIDs for them and I can send the logs your way, but most likely they will also have httpStatusCode=[500] in them. If you find any discrepancy from your side and you can't locate the server error (500), please let me know and we can also dig deeper. In such scenario, having your post URL along with body would really help so we can try making calls to your callback URL and see what happens.
 
Jan 9, 2018
152
16
18
54
Sorry to butt in. Did you put your settings & files somewhere? I tried toying with Telnyx and I ended up giving up, putting it on the "to mess with later" list.
The current version that is available on GitHub should work. Just follow the directions in the readme. You do need to add your secret key to the default setting for "telnyx_secret_key" in the SMS section and enable all the telnyx settings, also in that section.
 
Jan 9, 2018
152
16
18
54
Jonathan,

below is the response I got from Bandwidth about duplicate delivery:

2021-04-01 00:16:16.151 ERROR [callback-consumer-worker-406] [CallbackSender.java:49] Temporary HTTP error sending callback: httpStatusCode=[500], rateLimit(/sec)=[1000.0], callback=[ImmutableCallback{uri=https://XXXXXXXXXXX/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=92f15299-3d4a-4d0a-99b3-f1367d1b5ed5, eventType=message-received}, headers=[]}]

If you notice, the log has httpStatusCode=[500] error. In my experience, that happens when a message was delivered, but your callback server failed to return 200 OK status. In such a scenario, Bandwidth thinks that there was some issue, so try to deliver the same message again hoping to get a 200 OK from the server. Until we get 200 OK from the server, we keep retrying for a 24 hour period. In your case, you are receiving a message on a handset, but the callback server is not responding 200 OK, so we are trying to send the message again, causing duplicate messages on your handset.

I hope the above information helps. If you need me to send you logs for the examples you have provided, please send me messageIDs for them and I can send the logs your way, but most likely they will also have httpStatusCode=[500] in them. If you find any discrepancy from your side and you can't locate the server error (500), please let me know and we can also dig deeper. In such scenario, having your post URL along with body would really help so we can try making calls to your callback URL and see what happens.
In your sms_hook_bandwidth.php, just before the
PHP:
} else {
line, add this:
PHP:
return $response->withStatus(200)
    ->write("OK");

Again, this isn't something that I can test, so you'll have to just try it and see if that gives back the response that Bandwidth is expecting.
 

mak_make

Member
Aug 27, 2018
53
0
6
42
In your sms_hook_bandwidth.php, just before the
PHP:
} else {
line, add this:
PHP:
return $response->withStatus(200)
    ->write("OK");

Again, this isn't something that I can test, so you'll have to just try it and see if that gives back the response that Bandwidth is expecting.
I will try this... Also bandwidth is saying they are seeing HTTP error 502 for some of the duplicate message. I am trying to find out the cause.
 

mak_make

Member
Aug 27, 2018
53
0
6
42
below is the log from bandwidth for 502 error:

I am seeing 502 errors on the IDs you have provided. Please see the error logs below. There are many more errors like these for the message IDs, but here are just examples:

2021-04-01 14:20:32.374 ERROR [callback-consumer-worker-344] [CallbackSender.java:49] Temporary HTTP error sending callback: httpStatusCode=[502], rateLimit(/sec)=[250.0], callback=[ImmutableCallback{uri=https://XXXXXXXXXXXXX/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=f11d4f40-c1af-4301-b1cd-572bd508b763, eventType=message-received}, headers=[]}]
2021-04-01 14:24:26.663 ERROR [callback-consumer-worker-383] [CallbackSender.java:49] Temporary HTTP error sending callback: httpStatusCode=[502], rateLimit(/sec)=[1000.0], callback=[ImmutableCallback{uri=https://call.teleprime.com/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=f11d4f40-c1af-4301-b1cd-572bd508b763, eventType=message-received}, headers=[]}]

2021-04-01 14:54:51.137 ERROR [callback-consumer-worker-45] [CallbackSender.java:49] Temporary HTTP error sending callback: httpStatusCode=[502], rateLimit(/sec)=[500.0], callback=[ImmutableCallback{uri=https://XXXXXXXXXX/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=bc4636b4-66a1-4e02-9434-bf32d2748d72, eventType=message-received}, headers=[]}]
2021-04-01 15:01:01.766 ERROR [callback-consumer-worker-131] [CallbackSender.java:49] Temporary HTTP error sending callback: httpStatusCode=[502], rateLimit(/sec)=[63.125], callback=[ImmutableCallback{uri=https://XXXXXXXXXXXX/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=bc4636b4-66a1-4e02-9434-bf32d2748d72, eventType=message-received}, headers=[]}]
 

mak_make

Member
Aug 27, 2018
53
0
6
42
In your sms_hook_bandwidth.php, just before the
PHP:
} else {
line, add this:
PHP:
return $response->withStatus(200)
    ->write("OK");

Again, this isn't something that I can test, so you'll have to just try it and see if that gives back the response that Bandwidth is expecting.
When I tried it I am getting below:

2021/04/01 18:13:58 [error] 582#0: *6636 FastCGI sent in stderr: "PHP message: [SMS] REQUEST: Array
(
[USER] => www-data
[HOME] => /var/www
[HTTP_CONTENT_TYPE] => application/json; charset=utf-8
[HTTP_USER_AGENT] => bandwidth-api
[HTTP_ACCEPT_ENCODING] => gzip
[HTTP_CONNECTION] => Keep-Alive
[HTTP_HOST] => call.teleprime.com
[HTTP_CONTENT_LENGTH] => 619
[SCRIPT_FILENAME] => /var/www/fusionpbx/app/sms/hook/sms_hook_bandwidth.php
[REDIRECT_STATUS] => 200
[SERVER_NAME] => fusionpbx
[SERVER_PORT] => 443
[SERVER_ADDR] => 158.69.31.48
[REMOTE_PORT] => 39850
[REMOTE_ADDR] => 3.82.123.96
[SERVER_SOFTWARE] => nginx/1.6.2
[GATEWAY_INTERFACE] => CGI/1.1
[HTTPS] => on
[SERVER_PROTOCOL] => HTTP/1.1
[DOCUMENT_ROOT] => /var/www/fusionpbx
[DOCUMENT_URI] => /app/sms/hook/sms_hook_bandwidth.php
[REQUEST_URI] => /app/sms/hook/sms_hook_bandwidth.php
[SCRIPT_NAME] => /app/sms/hook/sms_hook_bandwidth.php
[CONTENT_LENGTH] => 619
[CONTENT_TYPE] => application/json; charset=utf-8
[REQUEST_MET...
PHP message: [SMS] REQUEST: Array
(
[0] => stdClass Object
(
[time] => 2021-04-01T23:13:57.474Z
[type] => message-received
[to] => +1XXXXXXXXXXXX
[description] => Incoming message received
[message] => stdClass Object
(
[id] => 98ea8554-af03-46bb-a402-f6a334aca1cf
[owner] => +1XXXXXXXXXX
[applicationId] => XXXXXXXXXXXXXXXX
[time] => 2021-04-01T23:13:57.351Z
[segmentCount] => 2
[direction] => in
[to] => Array
(
[0] => +1XXXXXXXXXX
)

[from] => +1XXXXXXXXXX
[text] => This is to test the long message inbound from sms app with new bandwidth settings that Jonathan has provided to me on March 3
2021/04/01 18:13:58 [error] 582#0: *6636 FastCGI sent in stderr: "omain_uuid, carrier from v_sms_destinations, v_domains where v_sms_destinations.domain_uuid = v_domains.domain_uuid and destination like :to
PHP message: Email Sender: From: teleprimevoice@gmail.com

PHP message: headers: From: XXXXXXXXXXXXXXXX@gmail.com
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x09b3e614cc12651c6e4d4c1f5f7bf54cx"
PHP message: [sms] Email Sent Successfully.
PHP message: SQL: select destination_number from v_ring_groups, v_ring_group_destinations where v_ring_groups.ring_group_uuid = v_ring_group_destinations.ring_group_uuid and ring_group_extension = :extension and v_ring_groups.domain_uuid = :domain_uuid
PHP message: RG RESULT: Array
(
)

PHP message: api luarun app.lua sms inbound 43@XXXXXXXXX.com 1XXXXXXXXX 'This is to test the long message inbound from sms app with new bandwidth settings that Jonathan has provided to me on March 31,2021 evening. Simple text working perfectly now check the 200 test inbound8' 1
PHP message: RESULT: +OK
PHP message: PHP Fatal error: Uncaught Error: Call to a member function withStatus() on null in /var/www/fusionpbx/app/sms/hook/sms_hook_bandwidth.php:27
Stack trace:
#0 {main}
thrown in /var/www/fusionpbx/app/sms/hook/sms_hook_bandwidth.php on line 27" while reading response header from upstream, client: 3.82.123.96, server: fusionpbx, request: "POST /app/sms/hook/sms_hook_bandwidth.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.1-fpm.sock:", host: "XXXXXXXXXX.com"
 

inder.singh

New Member
Aug 26, 2020
3
0
1
28
When I tried it I am getting below:

2021/04/01 18:13:58 [error] 582#0: *6636 FastCGI sent in stderr: "PHP message: [SMS] REQUEST: Array
(
[USER] => www-data
[HOME] => /var/www
[HTTP_CONTENT_TYPE] => application/json; charset=utf-8
[HTTP_USER_AGENT] => bandwidth-api
[HTTP_ACCEPT_ENCODING] => gzip
[HTTP_CONNECTION] => Keep-Alive
[HTTP_HOST] => call.teleprime.com
[HTTP_CONTENT_LENGTH] => 619
[SCRIPT_FILENAME] => /var/www/fusionpbx/app/sms/hook/sms_hook_bandwidth.php
[REDIRECT_STATUS] => 200
[SERVER_NAME] => fusionpbx
[SERVER_PORT] => 443
[SERVER_ADDR] => 158.69.31.48
[REMOTE_PORT] => 39850
[REMOTE_ADDR] => 3.82.123.96
[SERVER_SOFTWARE] => nginx/1.6.2
[GATEWAY_INTERFACE] => CGI/1.1
[HTTPS] => on
[SERVER_PROTOCOL] => HTTP/1.1
[DOCUMENT_ROOT] => /var/www/fusionpbx
[DOCUMENT_URI] => /app/sms/hook/sms_hook_bandwidth.php
[REQUEST_URI] => /app/sms/hook/sms_hook_bandwidth.php
[SCRIPT_NAME] => /app/sms/hook/sms_hook_bandwidth.php
[CONTENT_LENGTH] => 619
[CONTENT_TYPE] => application/json; charset=utf-8
[REQUEST_MET...
PHP message: [SMS] REQUEST: Array
(
[0] => stdClass Object
(
[time] => 2021-04-01T23:13:57.474Z
[type] => message-received
[to] => +1XXXXXXXXXXXX
[description] => Incoming message received
[message] => stdClass Object
(
[id] => 98ea8554-af03-46bb-a402-f6a334aca1cf
[owner] => +1XXXXXXXXXX
[applicationId] => XXXXXXXXXXXXXXXX
[time] => 2021-04-01T23:13:57.351Z
[segmentCount] => 2
[direction] => in
[to] => Array
(
[0] => +1XXXXXXXXXX
)

[from] => +1XXXXXXXXXX
[text] => This is to test the long message inbound from sms app with new bandwidth settings that Jonathan has provided to me on March 3
2021/04/01 18:13:58 [error] 582#0: *6636 FastCGI sent in stderr: "omain_uuid, carrier from v_sms_destinations, v_domains where v_sms_destinations.domain_uuid = v_domains.domain_uuid and destination like :to
PHP message: Email Sender: From: teleprimevoice@gmail.com

PHP message: headers: From: XXXXXXXXXXXXXXXX@gmail.com
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x09b3e614cc12651c6e4d4c1f5f7bf54cx"
PHP message: [sms] Email Sent Successfully.
PHP message: SQL: select destination_number from v_ring_groups, v_ring_group_destinations where v_ring_groups.ring_group_uuid = v_ring_group_destinations.ring_group_uuid and ring_group_extension = :extension and v_ring_groups.domain_uuid = :domain_uuid
PHP message: RG RESULT: Array
(
)

PHP message: api luarun app.lua sms inbound 43@XXXXXXXXX.com 1XXXXXXXXX 'This is to test the long message inbound from sms app with new bandwidth settings that Jonathan has provided to me on March 31,2021 evening. Simple text working perfectly now check the 200 test inbound8' 1
PHP message: RESULT: +OK
PHP message: PHP Fatal error: Uncaught Error: Call to a member function withStatus() on null in /var/www/fusionpbx/app/sms/hook/sms_hook_bandwidth.php:27
Stack trace:
#0 {main}
thrown in /var/www/fusionpbx/app/sms/hook/sms_hook_bandwidth.php on line 27" while reading response header from upstream, client: 3.82.123.96, server: fusionpbx, request: "POST /app/sms/hook/sms_hook_bandwidth.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.1-fpm.sock:", host: "XXXXXXXXXX.com"
could you please share full code of sms_hook_bandwidth.php.
 

mak_make

Member
Aug 27, 2018
53
0
6
42
PHP:
Hi Inder,



below is the code:



<?php

include "../root.php";

require_once "resources/require.php";
require_once "../sms_hook_common.php";

if ($debug) {
    error_log('[SMS] REQUEST: ' .  print_r($_SERVER, true));
}

if (check_acl()) {
    if  ($_SERVER['CONTENT_TYPE'] == 'application/json; charset=utf-8') {
        $data = json_decode(file_get_contents("php://input"));
        if ($debug) {
            error_log('[SMS] REQUEST: ' .  print_r($data, true));
            //error_log('[SMS] data-type: ' .  print_r($data[0]->type, true));
            //error_log('[SMS] data-from: ' .  print_r($data[0]->message->from, true));
           //error_log('[SMS] data-to: ' .  print_r($data[0]->to, true));
           //error_log('[SMS] data-text: ' .  print_r($data[0]->message->text, true));
        }
        if ($data[0]->type != "message-delivered") {
            //route_and_send_sms($data->from, $data->to, $data->text);
            route_and_send_sms($data[0]->message->from, $data[0]->to, $data[0]->message->text); //--looks like this one should be correct
        }
        
return $response->withStatus(200)
    ->write("OK");
    
    } else {
        error_log('[SMS] REQUEST: No SMS Data Received');
        die("no");
    }
} else {
    error_log('ACCESS DENIED [SMS]: ' .  print_r($_SERVER['REMOTE_ADDR'], true));
    die("access denied");
}
?>
 

inder.singh

New Member
Aug 26, 2020
3
0
1
28
PHP:
Hi Inder,



below is the code:



<?php

include "../root.php";

require_once "resources/require.php";
require_once "../sms_hook_common.php";

if ($debug) {
    error_log('[SMS] REQUEST: ' .  print_r($_SERVER, true));
}

if (check_acl()) {
    if  ($_SERVER['CONTENT_TYPE'] == 'application/json; charset=utf-8') {
        $data = json_decode(file_get_contents("php://input"));
        if ($debug) {
            error_log('[SMS] REQUEST: ' .  print_r($data, true));
            //error_log('[SMS] data-type: ' .  print_r($data[0]->type, true));
            //error_log('[SMS] data-from: ' .  print_r($data[0]->message->from, true));
           //error_log('[SMS] data-to: ' .  print_r($data[0]->to, true));
           //error_log('[SMS] data-text: ' .  print_r($data[0]->message->text, true));
        }
        if ($data[0]->type != "message-delivered") {
            //route_and_send_sms($data->from, $data->to, $data->text);
            route_and_send_sms($data[0]->message->from, $data[0]->to, $data[0]->message->text); //--looks like this one should be correct
        }
      
return $response->withStatus(200)
    ->write("OK");
  
    } else {
        error_log('[SMS] REQUEST: No SMS Data Received');
        die("no");
    }
} else {
    error_log('ACCESS DENIED [SMS]: ' .  print_r($_SERVER['REMOTE_ADDR'], true));
    die("access denied");
}
?>
just replace. it will stop receiving duplicate messages


PHP:
return $response->withStatus(200)
        ->withHeader('Content-Type', 'application/json')
        ->write(json_encode($data));
 

mak_make

Member
Aug 27, 2018
53
0
6
42
just replace. it will stop receiving duplicate messages


PHP:
return $response->withStatus(200)
        ->withHeader('Content-Type', 'application/json')
        ->write(json_encode($data));
Inder,

Thanks for the help. In the earlier posts Jonathan mentioned to me that you are working on MMS to work with Bandwidth do you have any luck with it.
 

mak_make

Member
Aug 27, 2018
53
0
6
42
Hi, Inder,

I have tried the code but still getting duplicate inbound messages. below is the error log:

2021/04/02 08:18:56 [error] 587#0: *878 FastCGI sent in stderr: "PHP message: [SMS] REQUEST: Array
(
[USER] => www-data
[HOME] => /var/www
[HTTP_CONTENT_TYPE] => application/json; charset=utf-8
[HTTP_USER_AGENT] => bandwidth-api
[HTTP_ACCEPT_ENCODING] => gzip
[HTTP_CONNECTION] => Keep-Alive
[HTTP_HOST] => call.teleprime.com
[HTTP_CONTENT_LENGTH] => 422
[SCRIPT_FILENAME] => /var/www/fusionpbx/app/sms/hook/sms_hook_bandwidth.php
[REDIRECT_STATUS] => 200
[SERVER_NAME] => fusionpbx
[SERVER_PORT] => 443
[SERVER_ADDR] => 158.69.31.48
[REMOTE_PORT] => 32666
[REMOTE_ADDR] => 3.82.123.96
[SERVER_SOFTWARE] => nginx/1.6.2
[GATEWAY_INTERFACE] => CGI/1.1
[HTTPS] => on
[SERVER_PROTOCOL] => HTTP/1.1
[DOCUMENT_ROOT] => /var/www/fusionpbx
[DOCUMENT_URI] => /app/sms/hook/sms_hook_bandwidth.php
[REQUEST_URI] => /app/sms/hook/sms_hook_bandwidth.php
[SCRIPT_NAME] => /app/sms/hook/sms_hook_bandwidth.php
[CONTENT_LENGTH] => 422
[CONTENT_TYPE] => application/json; charset=utf-8
[REQUEST_MET...
PHP message: [SMS] REQUEST: Array
(
[0] => stdClass Object
(
[time] => 2021-04-02T13:07:16.973Z
[type] => message-received
[to] => +1XXXXXXXXX
[description] => Incoming message received
[message] => stdClass Object
(
[id] => 55986d60-ccab-419e-812d-f09acd1f6aff
[owner] => +18475823220
[applicationId] => c52cf3d6-1932-40a7-abd5-013d98cbf62d
[time] => 2021-04-02T13:07:16.895Z
[segmentCount] => 1
[direction] => in
[to] => Array
(
[0] => +1XXXXXXXXX
)

[from] => +1XXXXXXXXX
[text] => collect clothes, etc.
)

)

)

PHP message: TO: 1XXXXXXXXX
PHP message: FROM: 184
2021/04/02 08:18:56 [error] 587#0: *878 upstream sent too big header while reading response header from upstream, client: 3.82.123.96, server: fusionpbx, request: "POST /app/sms/hook/sms_hook_bandwidth.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.1-fpm.sock:", host: "XXXXXXXXXXXXX"
 

mak_make

Member
Aug 27, 2018
53
0
6
42
for the duplicate messages Bandwidth is seeing HTTP 502 error. Below is the log from Bandwidth:

I am seeing a pattern where we will send the call back and get that 502 error. Then if you look, the last attempt for each does not return an error.

cdff182e-95c3-4a32-9e0a-20c5919d73c4
2021-04-02 08:50:03.237 INFO [callback-consumer-worker-428] [CallbackSender.java:41] Sending callback [ImmutableCallback{uri=https://XXXXXXXXXXXXX.com/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=cdff182e-95c3-4a32-9e0a-20c5919d73c4, eventType=message-received}, headers=[]}]
2021-04-02 08:42:58.203 ERROR [callback-consumer-worker-170] [CallbackSender.java:49] Temporary HTTP error sending callback: httpStatusCode=[502], rateLimit(/sec)=[2000.0], callback=[ImmutableCallback{uri=https://XXXXXXXXXXXXX.com/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=cdff182e-95c3-4a32-9e0a-20c5919d73c4, eventType=message-received}, headers=[]}]
2021-04-02 08:42:56.659 INFO [callback-consumer-worker-170] [CallbackSender.java:41] Sending callback [ImmutableCallback{uri=https://XXXXXXXXXXXX.com/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=cdff182e-95c3-4a32-9e0a-20c5919d73c4, eventType=message-received}, headers=[]}]

4f4bdd74-caa3-4afb-bb92-98f4b77ef65a


2021-04-02 08:50:25.858 INFO [callback-consumer-worker-484] [CallbackSender.java:41] Sending callback [ImmutableCallback{uri=https://XXXXXXXXXXXXXX.com/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=4f4bdd74-caa3-4afb-bb92-98f4b77ef65a, eventType=message-received}, headers=[]}]
2021-04-02 08:45:02.816 ERROR [callback-consumer-worker-35] [CallbackSender.java:49] Temporary HTTP error sending callback: httpStatusCode=[502], rateLimit(/sec)=[1000.0], callback=[ImmutableCallback{uri=https://XXXXXXXX.com/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=4f4bdd74-caa3-4afb-bb92-98f4b77ef65a, eventType=message-received}, headers=[]}]
2021-04-02 08:45:01.515 INFO [callback-consumer-worker-35] [CallbackSender.java:41] Sending callback [ImmutableCallback{uri=https://XXXXXXXXXXXXXcom/app/sms/hook/sms_hook_bandwidth.php, metadata={messageId=4f4bdd74-caa3-4afb-bb92-98f4b77ef65a, eventType=message-received}, headers=[]}]
 

kobyhud

New Member
Mar 1, 2021
24
3
3
44
With the new Bandwidth v2 API from mid 2020 the code in the sms_hook_bandwidth.php is no longer correct nor is the lua in index.lua.

The index.lua will require an additional parameter namely the applicationId

And the sms_hook_bandwidth.php will get a slightly different object structure returned. Bandwidth now reserves the right to send multiple messages at the same time, and within each of those messages is now a sub message.
 

mak_make

Member
Aug 27, 2018
53
0
6
42
Hi,

I have worked on the index.lua and added the applicationId filed to it.

I am able to send out messages and inbound message mostly I am getting repeatedly and when I query bandwidth they are seeing HTTP 502 from the server side. Do you have the working sms_hook_bandwidth.php so that I can send 200 to bandwidth on every incoming message.
 
Jan 9, 2018
152
16
18
54
While that's probably the better approach long-term (updating the code to work with new version of the API), one other option might be to set the API version that you are using to V1.
 
Status
Not open for further replies.