FusionPBX SMS Integration with Bandwidth

Status
Not open for further replies.

mak_make

Member
Aug 27, 2018
53
0
6
42
HI,

I am trying to use Bandwidth as an SMS carrier and I was able to send out messages. but when I send an outbound message, immediately I get an inbound message but no content on it. it shows like below ( I believe it's some kind of reply from the carrier) :

ExtensionTime StampSourceDestinationMessage
icon_cdr_inbound_answered.png
432021-03-31 13:57:20.252985043
icon_cdr_outbound_answered.png
432021-03-31 13:57:18.181877431XXXXXXXXXXTest

Incoming messages are not working for me. In the error log I can see the message input as below:

2021/03/31 14:40:06 [error] 29818#0: *38564 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] => 385
[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] => 60462
[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] => 385
[CONTENT_TYPE] => application/json; charset=utf-8
[REQUEST_ME...
PHP message: [SMS] REQUEST: Array
(
[0] => stdClass Object
(
[time] => 2021-03-31T19:40:04.476Z
[type] => message-received
[to] => +18475823220
[description] => Incoming message received
[message] => stdClass Object
(
[id] => 6081d691-4c71-45d6-b488-64ebe053f87d
[owner] => +18475823220
[applicationId] => c52cf3d6-1932-40a7-abd5-013d98cbf62d
[time] => 2021-03-31T19:40:04.336Z
[segmentCount] => 1
[direction] => in
[to] => Array
(
[0] => +18475823220
)

[from] => +12248880042
[text] => Test
)

)

)

PHP message: TO: 0
PHP message: FROM: 0
PHP message: BODY:
PHP message: BODY:
PHP

This implies me that I am getting data from the carrier but some how it's not routing correctly to the Endpoint ( I am using Bria Iphone edition).

But in the MDR's I can see any text data, I see it as below:

ExtensionTime StampSourceDestinationMessage
icon_cdr_inbound_answered.png
432021-03-31 14:40:06.477081043
icon_cdr_inbound_answered.png
432021-03-31 14:37:04.220367043

I have enabled the email option also but I get email with no details. all I get is below:

subject: Text Message from: 0

in the Body of email:

To: 0
Msg:

I am not sure what I am doing wrong. If any of you people has faced the same issue on inbound messages or any idea on how to fix it would be helpful to me a lot.
 
Jan 9, 2018
152
16
18
54
Another user, @inder.singh has done some work on the Bandwidth integration for SMS. Maybe he will chime in here. Fair warning, I don't use Bandwidth for SMS, so I can't confirm the following.

However, based on this line in the response you got from Bandwidth:
Code:
[type] => message-received
I suspect that this is just a confirmation from Bandwidth that they've received it (or maybe that it's been sent on through to the receiving carrier). It seems likely that the \app\sms\hook\sms_hook_bandwidth.php code should be adjusted to filter out this response. Maybe add something like:
Code:
if ($data[0]['type']!="message-received") {
after the
Code:
$data = json_decode(file_get_contents("php://input"));
line.
 

mak_make

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

Thanks for your response on my post.

I am really not a php guy. I have added the code that you have provided but I am getting the syntax error:

FastCGI sent in stderr: "PHP message: PHP Parse error: syntax error, unexpected end of file in /var/www/fusionpbx/app/sms/hook/sms_hook_bandwidth.php on line 28" while reading response header from upstream, client: 52.72.24.132, 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: "XX.XXXX.XXX"

so, based on the post what I am understanding is I need to play around with sms_hook_bandwidth.php to get the inbound messages delivered properly.

if we have issues with bandwidth can you suggest a different carrier who is working perfectly with sms app?
 
Jan 9, 2018
152
16
18
54
Try this:
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));
        }
        if ($data[0]['type']!="message-received") {
            route_and_send_sms($data->from, $data->to, $data->text);
        }
    } 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");
}
?>

I know that @inder.singh does have it working with Bandwidth. In fact, he was working on making MMS work with Bandwidth, as well.

We currently use Telnyx for SMS with no issues--but you may need to set the correct version of the API in their portal--I'm using . When I started playing with this a couple of years ago, it was also working with Twilio--if you can afford it.
 
Jan 9, 2018
152
16
18
54
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
 

mak_make

Member
Aug 27, 2018
53
0
6
42
Hi,

When I use the code I got the below error:

PHP message: PHP Fatal error: Uncaught Error: Cannot use object of type s
 

mak_make

Member
Aug 27, 2018
53
0
6
42
When I have posted the error to Bandwidth support they have replied me with this:

Are you using one of the Bandwidth Sdk's, or is this error being generated by FusionPBX? The good news is that on our end we are successfully sending you inbound messages as well as outbound message callbacks to your callback server. I'm not familiar with fusionpbx nor the inner workings of this but it appears they're expecting some other type of SMS header from your request, in that case there must some be some kind of setting that must be changed. When you send SMS via the Bandwidth API we send a 202 http response, then a separate delivery callback after the SMS has been delivered to the carrier so I'm not sure what you're application is expecting.

it might shed some light on my issue.
 
Jan 9, 2018
152
16
18
54
Sorry, I can't test this, but try it with this tweak in that one line:
PHP:
        if ($data->type != "message-received") {

It's the "separate delivery callback" that is causing the error.

Also, is the outbound message being received by the recipient? All we're trying to do with these code changes is filter out that "callback". It shouldn't actually affect the message delivery.

Since Bandwidth is seeing the message, that's a good sign.
 
Jan 9, 2018
152
16
18
54
If your recipient is not getting the outbound message, you might confirm with Bandwidth that they are seeing the OB message and if they can tell you why they aren't delivering it.
 

mak_make

Member
Aug 27, 2018
53
0
6
42
Sorry, I can't test this, but try it with this tweak in that one line:
PHP:
        if ($data->type != "message-received") {

It's the "separate delivery callback" that is causing the error.

Also, is the outbound message being received by the recipient? All we're trying to do with these code changes is filter out that "callback". It shouldn't actually affect the message delivery.

Since Bandwidth is seeing the message, that's a good sign.
When I send a message out I am still getting the call back. Still inbound coming as blank message body and from and To as 0
 

mak_make

Member
Aug 27, 2018
53
0
6
42
When we send outbound I believe it won't go through the sms_hook_bandwidth.php. below is the log of the fs_cli when we send outbound, I am getting some curl response back from bandwidth:

2021-03-31 17:38:15.087472 [NOTICE] switch_cpp.cpp:1365 [sms] DIRECTION: outbound
2021-03-31 17:38:15.087472 [NOTICE] switch_cpp.cpp:1365 [sms] TO: XXXXXXXXXXX
2021-03-31 17:38:15.087472 [NOTICE] switch_cpp.cpp:1365 [sms] FROM: 43
2021-03-31 17:38:15.087472 [NOTICE] switch_cpp.cpp:1365 [sms] BODY: Have time to call?
2021-03-31 17:38:15.087472 [NOTICE] switch_cpp.cpp:1365 [sms] DOMAIN_NAME: XXXXXXXXXXXX
2021-03-31 17:38:15.087472 [NOTICE] switch_cpp.cpp:1365 [sms] SQL: SELECT domain_uuid FROM v_domains WHERE domain_name = :domain_name and domain_enabled = 'true' ; params:{"domain_name":"XXXXXXXXXXXXXX"}
2021-03-31 17:38:15.087472 [NOTICE] switch_cpp.cpp:1365 [sms] DOMAIN_UUID: 5cdc4a72-c741-4048-854e-41647ecb5e01
2021-03-31 17:38:15.087472 [NOTICE] switch_cpp.cpp:1365 [sms] SQL: SELECT outbound_caller_id_number, extension_uuid, carrier FROM v_extensions , v_sms_destinations WHERE outbound_caller_id_number = destination and v_extensions.domain_uuid = :domain_uuid and extension = :from and v_sms_destinations.enabled = 'true' and v_extensions.enabled = 'true'; params:{"from":"43","domain_uuid":"5cdc4a72-c741-4048-854e-41647ecb5e01"}
2021-03-31 17:38:15.087472 [NOTICE] switch_cpp.cpp:1365 [settings] SQL: SELECT * FROM v_default_settings WHERE default_setting_enabled = 'true' AND default_setting_category is not null AND default_setting_subcategory is not null AND default_setting_name is not null AND default_setting_value is not null ORDER BY default_setting_category, default_setting_subcategory ASC
2021-03-31 17:38:15.097475 [NOTICE] switch_cpp.cpp:1365 [settings] SQL: SELECT * FROM v_domain_settings WHERE domain_uuid = :domain_uuid AND domain_setting_enabled = 'true' AND domain_setting_category is not null AND domain_setting_subcategory is not null AND domain_setting_name is not null AND domain_setting_value is not null ORDER BY domain_setting_category, domain_setting_subcategory ASC ; params: {"domain_uuid":"5cdc4a72-c741-4048-854e-41647ecb5e01"}
2021-03-31 17:38:15.097475 [NOTICE] switch_cpp.cpp:1365 [sms] access_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
2021-03-31 17:38:15.097475 [NOTICE] switch_cpp.cpp:1365 [sms] secret_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
2021-03-31 17:38:15.097475 [NOTICE] switch_cpp.cpp:1365 [sms] api_url: "https://messaging.bandwidth.com/api/v2/users/XXXXXXXXXX/messages"
2021-03-31 17:38:15.097475 [NOTICE] switch_cpp.cpp:1365 [sms] CMD: curl -v -X POST "https://messaging.bandwidth.com/api/v2/users/XXXXXXXX/messages" -u XXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXX -H "Content-type: application/json" -d '{"from": "+1XXXXXXXXXX", "to": "+1XXXXXXXXXXX", "text": "Have time to call?", "applicationId": "XXXXXXXXXXXXXXXXXXXXXXXX"}'
2021-03-31 17:38:15.587502 [NOTICE] switch_cpp.cpp:1365 [sms] CURL Returns: {"id":"XXXXXXXXXXX","owner":"+1XXXXXXXXX","applicationId":"XXXXXXXXXXX","time":"2021-03-31T22:38:15.475Z","segmentCount":1,"direction":"out","to":["+XXXXXXXXX"],"from":"+XXXXXXX","text":"Have time to call?"}
2021-03-31 17:38:15.587502 [NOTICE] switch_cpp.cpp:1365 [sms] SQL: insert into v_sms_messages(sms_message_uuid,extension_uuid,domain_uuid,start_stamp,from_number,to_number,message,direction,response,carrier) values :)uuid,:extension_uuid,:domain_uuid,now(),:from,:to,:body,:direction,'',:carrier); params:{"body":"Have time to call?","domain_uuid":"5cdc4a72-c741-4048-854e-41647ecb5e01","from":"43","extension_uuid":"30c232e7-11bd-4b3a-a80f-997f809e0186","carrier":"bandwidth","uuid":"ea889450-55b2-4fb5-8a73-01a46c28e4b5","to":"1XXXXXXXXXX","direction":"outbound"}
 
Last edited:
Jan 9, 2018
152
16
18
54
No, it does not go through that file on outbound. The "hook" file is only for inbound. But the "callback" from Bandwidth is coming back just like an inbound message, so it DOES go through the "hook" file. All you need to do is to have that "if" statement properly catch the message type and skip processing on it.

You might try adding some extra logging to your "hook" file, in the "if ($debug)" block. Something like:
PHP:
error_log('[SMS] $data->type: ' .  print_r($data->type, true));

Someone with better PHP array skills might be able to clarify the syntax.
 

mak_make

Member
Aug 27, 2018
53
0
6
42
Thanks for Clarification. I think I have to get PHP skills;), when I tried the code this is what I am seeing is below but still getting the response back

2021/03/31 18:01:59 [error] 583#0: *7 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] => 362
[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] => 24134
[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] => 362
[CONTENT_TYPE] => application/json; charset=utf-8
[REQUEST_...
PHP message: [SMS] REQUEST: Array
(
[0] => stdClass Object
(
[time] => 2021-03-31T23:01:57.386Z
[type] => message-delivered
[to] => +1XXXXXXXXX
[description] => ok
[message] => stdClass Object
(
[id] => 16172317166295tcy5fuet2hhw5lh
[owner] => +1XXXXXX
[applicationId] => cXXXXXXXXXX
[time] => 2021-03-31T23:01:56.629Z
[segmentCount] => 1
[direction] => out
[to] => Array
(
[0] => +1XXXXXXXXXXX
)

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

)

)

PHP message: [SMS] $data->type:
PHP message: TO: 0
PHP message: FROM: 0
PHP message: BODY:
 

mak_make

Member
Aug 27, 2018
53
0
6
42
This is what I have in hook file:

<?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->type, true));
}
if ($data->type != "message-received") {
route_and_send_sms($data->from, $data->to, $data->text);
}
} 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");
}
?>
 
Jan 9, 2018
152
16
18
54
Replace your debug section with 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: ' .  print_r($data->type, true));
            }
        }
This might give you an idea of what value is in the "type" field. From what you posted above, when we try to access it with "$data->type" it's blank. Maybe "$data[0]['type']" will work.

Also, when you post code in a message, it's good to get in the habit of using the Code button (it's under More Options in the toolbar).
 
Jan 9, 2018
152
16
18
54
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));
            }
        }
 

mak_make

Member
Aug 27, 2018
53
0
6
42
Hi,

I have tried both codes. when I use the code:
Replace your debug section with 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: ' .  print_r($data->type, true));
            }
        }
This might give you an idea of what value is in the "type" field. From what you posted above, when we try to access it with "$data->type" it's blank. Maybe "$data[0]['type']" will work.

Also, when you post code in a message, it's good to get in the habit of using the Code button (it's under More Options in the toolbar).
I will use the code button in further. sorry for inconvenience. when I use this code and send outbound messages I can see the callbacks coming from bandwdith in error logs but they are not delivered to the end point :).

As the incoming is concern, now I am not even receiving the blank messages this is the error log I see in the system:

2021/03/31 18:41:16 [error] 582#0: *174 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] => 52352
[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:41:16.476Z
[type] => message-received
[to] => +1XXXXXXXXX
[description] => Incoming message received
[message] => stdClass Object
(
[id] => ea09c4d9-d7a7-44b1-8fef-c3ab17e99c62
[owner] => +1XXXXXXXX
[applicationId] => XXXXXXXXXXXXXXXXXXXXXXX
[time] => 2021-03-31T23:41:16.334Z
[segmentCount] => 1
[direction] => in
[to] => Array
(
[0] => +1XXXXXXXX
)

[from] => +1XXXXXXXXX
[text] => Are you there?
)

)

)

PHP message: PHP Fatal error: Uncaught Error: Cannot use object of type stdCl
 
Status
Not open for further replies.