SMS app adding new carrier

Status
Not open for further replies.

Mikeme

Member
Apr 26, 2021
186
4
18
39
After doing a huge amount of research i cant find a way how to add new carrier to SMS app.
here is the sample code from carrier, wish to have some help how to add it well.

$url = "https://carrier.com/api";
$key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$xml ='
<?xml version="1.0" encoding="UTF-8"?>
<sms>
<user>
<username>UserNmae</username>
</user>
<source>'SourceNumber'</source>
<destinations>
<phone>'DestionationNumber'</phone>

</destinations>
<message>'Message'</message>
</sms>';
$CR = curl_init();
curl_setopt($CR, CURLOPT_URL, $url);
curl_setopt($CR, CURLOPT_POST, 1);
curl_setopt($CR, CURLOPT_FAILONERROR, true);
curl_setopt($CR, CURLOPT_POSTFIELDS, $xml);
curl_setopt($CR, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($CR, CURLOPT_HTTPHEADER, array(
'charset=utf-8',
'Content-Type: application/json',
'Authorization: Bearer ' . $key
)
);
$result = curl_exec($CR);
$error = curl_error($CR);
if (!empty($error))
die("Error: " . $error);
else
 

Dast

Member
Nov 11, 2019
57
10
8
To add a new carrier you need to add support for sending and receiving via that carrier.

Sending via new carrier:
You can edit `/usr/share/freeswitch/scripts/app/sms/index.lua` to add a new carrier, around line 400~ you will find a bunch of if-else statements.
Add a new if-else for your new carrier to perform the webhook or whatever action is required to actually send the SMS.

Receiving via new carrier:
In the `/var/www/fusionpbx/app/sms/hook` directory you will find the scripts used to process inbound SMS for each carrier.
Copy and paste an existing script. The name of the carrier included in the filename should match the carrier name specified in the Sms/carrier default settings array.
 

Mikeme

Member
Apr 26, 2021
186
4
18
39
Thank you Dest.
i have made the change and added to new carrier to `/usr/share/freeswitch/scripts/app/sms/index.lua`
and once I'm trying to send message I'm facing the following info in fs_cli log , message not send.
i set up default settings according this carrier.

where can i see what actualy sent to carrier?
here is the line i add in 'index.lua' for new carrier:
cmd = "curl -u " .. api_url .." <sms> <user> <username>".. username .."</username><password>".. secret_key .. "</password></user><source>" .. outbound_caller_id_number .. "</source> <destinations> <phone>" .. to .."</phone></destinations><message>" .. body .. "</message> <response>0</response> </sms> ";
 

Attachments

  • ca.png
    ca.png
    73.2 KB · Views: 9

Dast

Member
Nov 11, 2019
57
10
8
As far as i know, there's no real sms log other than which is shown in fs_cli.

I would start out by confirming your curl command works successfully, does it work if you manually run that curl message from cli?
My guess would be your strings are not escaped/quoted properly.

Try something like this, note how the variables are now contained within single quotes.
Code:
cmd = "curl -u  '" .. api_url .."'  <sms> <user> <username>'".. username .."'</username><password>'".. secret_key .. "'</password></user><source>'" .. outbound_caller_id_number .. "'</source> <destinations> <phone>'" .. to .."'</phone></destinations><message>'" .. body .. "'</message> <response>0</response> </sms> ";
 
  • Like
Reactions: falk

Mikeme

Member
Apr 26, 2021
186
4
18
39
Thanks Dest, i change the code to yours - same status.. :(
how should i run this code / curl in cli?
if i just past the code in cli i'm facing an error message...
 

Dast

Member
Nov 11, 2019
57
10
8
To test the curl command manually, use something like this and replace the variables (signified by a $) with your test values.


Bash:
curl -u 'api_url' '<sms> <user> <username>$username</username><password>$secret_key</password></user><source>$outbound_caller_id_number</source> <destinations> <phone>$to</phone></destinations><message>$body</message> <response>0</response> </sms>'


However looking at your curl command, it doesn't seem right.
What http method are you trying to use, POST?
At the very least, I think your command is more likely to be something like this;


Bash:
curl -X POST -H 'Content-Type: application/xml' -H 'Accept: application/xml' -d '<sms> <user> <username>$username</username><password>$secret_key</password></user><source>$outbound_caller_id_number</source> <destinations> <phone>$to</phone></destinations><message>$body</message> <response>0</response> </sms>' 'api_url'
 

Mikeme

Member
Apr 26, 2021
186
4
18
39
As far as i know, there's no real sms log other than which is shown in fs_cli.

I would start out by confirming your curl command works successfully, does it work if you manually run that curl message from cli?
My guess would be your strings are not escaped/quoted properly.

Try something like this, note how the variables are now contained within single quotes.
Code:
cmd = "curl -u  '" .. api_url .."'  <sms> <user> <username>'".. username .."'</username><password>'".. secret_key .. "'</password></user><source>'" .. outbound_caller_id_number .. "'</source> <destinations> <phone>'" .. to .."'</phone></destinations><message>'" .. body .. "'</message> <response>0</response> </sms> ";
ok so i run the code via cli here is the issue, carrier respond that there is some problem with xml
 

Attachments

  • ca1.png
    ca1.png
    19.8 KB · Views: 8

Mikeme

Member
Apr 26, 2021
186
4
18
39
To test the curl command manually, use something like this and replace the variables (signified by a $) with your test values.


Bash:
curl -u 'api_url' '<sms> <user> <username>$username</username><password>$secret_key</password></user><source>$outbound_caller_id_number</source> <destinations> <phone>$to</phone></destinations><message>$body</message> <response>0</response> </sms>'


However looking at your curl command, it doesn't seem right.
What http method are you trying to use, POST?
At the very least, I think your command is more likely to be something like this;


Bash:
curl -X POST -H 'Content-Type: application/xml' -H 'Accept: application/xml' -d '<sms> <user> <username>$username</username><password>$secret_key</password></user><source>$outbound_caller_id_number</source> <destinations> <phone>$to</phone></destinations><message>$body</message> <response>0</response> </sms>' 'api_url'
when try to run both command i'm facing "-ERR no reply".

when running attach command there is replay from carrier but with an XML problem...
 

Attachments

  • ca1.png
    ca1.png
    19.8 KB · Views: 8

Dast

Member
Nov 11, 2019
57
10
8
Hard to see what is going on from the screenshot, but the error suggests the receiving server is unable to parse your data as valid XML.
Start by confirming your curl command is correct, and you have properly quoted/escaped everything.

If you post your code from the `/usr/share/freeswitch/scripts/app/sms/index.lua` file I can have a better look.
 

Mikeme

Member
Apr 26, 2021
186
4
18
39
well, the issue seems to be according carrier request to have this line in command "<?xml version="1.0" encoding="UTF-8"?>"
the qustion is in which format to put <?xml version="1.0" encoding="UTF-8"?> in this command? -
Bash:
curl https://carrier_url.com/api post "<?xml version="1.0" encoding="UTF-8"?>" <sms><user><username></username><password></password></user><source></source><destinations> <phone></phone> </destinations> <message>this is a
sample message</message> <response>0</response> </sms>
 

Mikeme

Member
Apr 26, 2021
186
4
18
39
Hard to see what is going on from the screenshot, but the error suggests the receiving server is unable to parse your data as valid XML.
Start by confirming your curl command is correct, and you have properly quoted/escaped everything.

If you post your code from the `/usr/share/freeswitch/scripts/app/sms/index.lua` file I can have a better look.
your idea to use fs_cli to run and test the curl post is great!
this is the command i'm sending -
Bash:
curl https://carrier_url.com/api post "<?xml version="1.0" encoding="UTF-8"?>" <sms><user><username></username><password></password></user><source></source><destinations> <phone></phone> </destinations> <message>this is a
sample message</message> <response>0</response> </sms>

and this is the replay from carrier -
Bash:
<?xml version="1.0" encoding="utf-8"?>
<sms><status>1</status><message>There was a problem parsing your XML</message></sms>
 

Mikeme

Member
Apr 26, 2021
186
4
18
39
so i manage to make it work via cli i'm able to send SMS!
Bash:
curl  https://carrier.com/api -H content-type 'application/xml; charset=utf-8' post  '<?xml version="1.0" encoding="UTF-8"?> <sms> <user> <username>1234</username><password>1234</password></user><source>test</source> <destinations> <phone>68654213</phone></destinations><message>asdasd</message> <response>0</response> </sms>'
when trying to copy command with '<?xml version="1.0" encoding="UTF-8"?> i'm facing an error, i believe lua cannot use this format. how can i make it work?
 

Dast

Member
Nov 11, 2019
57
10
8
I am guessing the issue is likely related to escaping the string when used in the lua script.
Because in the lua script the cmd is within double-quotes, example;
Code:
cmd = "curl -u  ....................... <response>0</response> </sms> ";

This means if you need to use any double-quotes within your actual command you need to escape them with a backslash.

Try using the following in your lua, you will note each double-quote is first escaped with a backslash before it;
Code:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
 

falk

Member
Feb 2, 2019
76
5
8
54
I am guessing the issue is likely related to escaping the string when used in the lua script.
Because in the lua script the cmd is within double-quotes, example;
Code:
cmd = "curl -u  ....................... <response>0</response> </sms> ";

This means if you need to use any double-quotes within your actual command you need to escape them with a backslash.

Try using the following in your lua, you will note each double-quote is first escaped with a backslash before it;
Code:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
hi it possible to run this code from dial plan ?
To test the curl command manually, use something like this and replace the variables (signified by a $) with your test values.


Bash:
curl -u 'api_url' '<sms> <user> <username>$username</username><password>$secret_key</password></user><source>$outbound_caller_id_number</source> <destinations> <phone>$to</phone></destinations><message>$body</message> <response>0</response> </sms>'


However looking at your curl command, it doesn't seem right.
What http method are you trying to use, POST?
At the very least, I think your command is more likely to be something like this;


Bash:
curl -X POST -H 'Content-Type: application/xml' -H 'Accept: application/xml' -d '<sms> <user> <username>$username</username><password>$secret_key</password></user><source>$outbound_caller_id_number</source> <destinations> <phone>$to</phone></destinations><message>$body</message> <response>0</response> </sms>' 'api_url'
 

Mikeme

Member
Apr 26, 2021
186
4
18
39
I am guessing the issue is likely related to escaping the string when used in the lua script.
Because in the lua script the cmd is within double-quotes, example;
Code:
cmd = "curl -u  ....................... <response>0</response> </sms> ";

This means if you need to use any double-quotes within your actual command you need to escape them with a backslash.

Try using the following in your lua, you will note each double-quote is first escaped with a backslash before it;
Code:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
Thank's Dest, using this format does not appear errors anymore..
but unfortunately SMS's are still not sending..

without any log or monitor it will be hard to troubleshot problem...
anyway to find curl output logs to see whats actually has been sent to carrier ?
 

Mikeme

Member
Apr 26, 2021
186
4
18
39
so this is the last command i'm using in SMS index.lua -

Bash:
cmd = " curl  " .. api_url .. " -H \"content-type: application/xml; charset=utf-8'\"  <?xml version=\"1.0\" encoding=\"UTF-8\"?> <sms><user><username>".. username .."</username><password>".. secret_key .. "</password></user><source>test</source><destinations><phone>".. to .."</phone></destinations><message>".. body .."</message><response>0</response></sms> ";

this is the curl command which send SMS successfully -
Bash:
curl  https://carrier.com/api -H content-type 'application/xml; charset=utf-8' post  '<?xml version="1.0" encoding="UTF-8"?> <sms> <user> <username>1234</username><password>1234</password></user><source>test</source> <destinations> <phone>68654213</phone></destinations><message>asdasd</message> <response>0</response> </sms>'

does anyone can see reason why it will not work? backslash or space issue?
 
Last edited:

Mikeme

Member
Apr 26, 2021
186
4
18
39
ca2.png

this is the debug log from cli while trying to send sms via index.lua , it is not showing any CURL Returns...
 
Last edited:

Dast

Member
Nov 11, 2019
57
10
8
so this is the last command i'm using in SMS index.lua -

Bash:
cmd = " curl  " .. api_url .. " -H \"content-type: application/xml; charset=utf-8'\"  <?xml version=\"1.0\" encoding=\"UTF-8\"?> <sms><user><username>".. username .."</username><password>".. secret_key .. "</password></user><source>test</source><destinations><phone>".. to .."</phone></destinations><message>".. body .."</message><response>0</response></sms> ";

this is the curl command which send SMS successfully -
Bash:
curl  https://carrier.com/api -H content-type 'application/xml; charset=utf-8' post  '<?xml version="1.0" encoding="UTF-8"?> <sms> <user> <username>1234</username><password>1234</password></user><source>test</source> <destinations> <phone>68654213</phone></destinations><message>asdasd</message> <response>0</response> </sms>'

does anyone can see reason why it will not work? backslash or space issue?

Looks like you're missing a single quote in your lua curl command, however it is not missing in your cli curl command, as illustrated in following screenshot;
1666595202673.png

Try adding a single quote before the "application/xml" part, as follows;
Bash:
cmd = " curl  " .. api_url .. " -H \"Content-Type: 'application/xml; charset=utf-8'\"  <?xml version=\"1.0\" encoding=\"UTF-8\"?> <sms><user><username>".. username .."</username><password>".. secret_key .. "</password></user><source>test</source><destinations><phone>".. to .."</phone></destinations><message>".. body .."</message><response>0</response></sms> ";
 
Status
Not open for further replies.