Working SMS Integration

Status
Not open for further replies.

MTR

Member
Oct 25, 2017
181
9
18
45
Since i did all the changes i cant add a new SMS destination all-tough i can edit old once i cant add new once

below is my final code

PHP:
<?php
/* $Id$ */
/*
    call.php
    Copyright (C) 2008, 2009 Mark J Crane
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
    Contributor(s):
    Mark J Crane <markjcrane@fusionpbx.com>
    James Rose <james.o.rose@gmail.com>

*/

include "root.php";
require_once "resources/require.php";
require_once "resources/check_auth.php";
if (permission_exists('sms_add') || permission_exists('sms_edit')) {
    //access granted
}
else {
    echo "access denied";
    exit;
}

//add multi-lingual support
    $language = new text;
    $text = $language->get();

//set the action as an add or an update
    if (isset($_REQUEST["id"])) {
        $action = "update";
        $sms_uuid = check_str($_REQUEST["id"]);
        $sql = "select * from v_sms_destinations ";
        $sql .= "where sms_destination_uuid = '" . $_REQUEST["id"] . "' ";
        $sql .= "and domain_uuid = '" . $_SESSION['domain_uuid'] . "' LIMIT 1";
        $prep_statement = $db->prepare(check_sql($sql));
        $prep_statement->execute();
        $sms_destinations = $prep_statement->fetchAll(PDO::FETCH_NAMED);
        foreach ($sms_destinations as $row) {
            $destination = check_str($row["destination"]);
            $carrier = check_str($row["carrier"]);
            $description = check_str($row["description"]);
            $enabled = check_str($row["enabled"]);
            $sms_destination_uuid = $row['sms_destination_uuid'];
            $chatplan_detail_data = $row['chatplan_detail_data'];
        }
    }
    else {
        $action = "add";
    }

//get the http values and set them as php variables
    if (count($_POST) > 0 && $action != "update") {
        //get the values from the HTTP POST and save them as PHP variables
            $destination = str_replace(' ','-',check_str($_POST["destination"]));
            $carrier = check_str($_POST["carrier"]);
            $description = check_str($_POST["description"]);
            $enabled = check_str($_POST["enabled"]);
            $sms_destination_uuid = uuid();
            $chatplan_detail_data = check_str($_POST["chatplan_detail_data"]);
        if ($action == "add") {
            $sql_insert = "insert into v_sms_destinations ";
            $sql_insert .= "(";
            $sql_insert .= "sms_destination_uuid, ";
            $sql_insert .= "carrier, ";
            $sql_insert .= "domain_uuid, ";
            $sql_insert .= "destination, ";
            $sql_insert .= "enabled, ";
            $sql_insert .= "description ";
            $sql_insert .= ")";
            $sql_insert .= "values ";
            $sql_insert .= "(";
            $sql_insert .= "'".$sms_destination_uuid."', ";
            $sql_insert .= "'".$carrier."', ";
            $sql_insert .= "'".$_SESSION['domain_uuid']."', ";
            $sql_insert .= "'".$destination."', ";
            $sql_insert .= "'".$enabled."', ";
            $sql_insert .= "description, ";
            $sql_insert .= "chatplan_detail_data ";
            $sql_insert .= ")";
            $db->exec($sql_insert);
            header( 'Location: sms.php') ;
           
        }
    } elseif (count($_POST) > 0 && $action == "update") {
            $destination = str_replace(' ','-',check_str($_POST["destination"]));
            $carrier = check_str($_POST["carrier"]);
            $description = check_str($_POST["description"]);
           
           
            $enabled = check_str($_POST["enabled"]);
            $chatplan_detail_data = check_str($_POST["chatplan_detail_data"]);

            $sql_insert = "update v_sms_destinations set";
            $sql_insert .= " ";
            $sql_insert .= "carrier = '".$carrier."', ";
            $sql_insert .= "destination = '".$destination."', ";
            $sql_insert .= "enabled = '".$enabled."', ";
            $sql_insert .= "description = '".$description."', ";
            $sql_insert .= "chatplan_detail_data = '".$chatplan_detail_data."' ";
            $sql_insert .= "where sms_destination_uuid = '" . $sms_destination_uuid . "' and domain_uuid = '" . $_SESSION['domain_uuid'] . "'";
            $db->exec($sql_insert);
            error_log($sql_insert);
            header( 'Location: sms.php') ;
    }

//include the header
    require_once "resources/header.php";
    require_once "resources/paging.php";

    echo "<form method='post' name='frm' id='frm' action=''>\n";
    echo "<table width='100%' border='0' cellpdding='0' cellspacing='0'>\n";
    echo "<tr>\n";
    if ($action == "add") {
        echo "<td width='30%' nowrap='nowrap' align='left' valign='top'><b>".$text['header-sms-add']."</b></td>\n";
    }
    if ($action == "update") {
        echo "<td width='30%' nowrap='nowrap' align='left' valign='top'><b>".$text['header-sms-edit']."</b></td>\n";
    }
    echo "<td width='70%' align='right' valign='top'>\n";
    echo "    <input type='button' class='btn' alt='".$text['button-back']."' onclick=\"window.location='sms.php'\" value='".$text['button-back']."'>\n";
    echo "    <input type='button' class='btn' value='".$text['button-save']."' onclick='submit_form();'>\n";
    echo "    <br /><br />\n";
    echo "</td>\n";
    echo "</tr>\n";

    echo "<tr>\n";
    echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
    echo "    ".$text['label-destination']."\n";
    echo "</td>\n";
    echo "<td class='vtable' align='left'>\n";
    echo "    <input class='formfld' type='text' name='destination' autocomplete='off' maxlength='255' value=\"$destination\" required='required'>\n";
    echo "<br />\n";
    echo $text['description-destination']."\n";
    echo "</td>\n";
    echo "</tr>\n";

    echo "<tr>\n";
    echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
    echo "    ".$text['label-carrier']."\n";
    echo "</td>\n";
    echo "<td class='vtable' align='left'>\n";
    if (count($_SESSION['sms']['carriers']) > 0) {
        echo "<select name='carrier' class='formfld'>\n";
        echo "    <option value=''></option>\n";
        sort($_SESSION['sms']['carriers']);
        foreach ($_SESSION['sms']['carriers'] as &$row) {
            echo "    <option value='$row'";
            if ($row == $carrier) {
                echo " selected='selected'";
            }
            echo ">$row</option>\n";
        }
        echo "</select><br />\n";
    }
    echo $text['description-carrier']."\n";
    echo "</td>\n";
    echo "</tr>\n";



    echo "<tr>\n";
    echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
    echo "    ".$text['label-chatplan_detail_data']."\n";
    echo "</td>\n";
    echo "<td class='vtable' align='left'>\n";
    echo "    <input class='formfld' type='text' name='chatplan_detail_data' autocomplete='off' maxlength='255' value=\"$chatplan_detail_data\" >\n";
    echo "<br />\n";
    echo $text['description-chatplan_detail_data']."\n";
    echo "</td>\n";
    echo "</tr>\n";

    if (permission_exists('sms_enabled')) {
        echo "<tr>\n";
        echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
        echo "    ".$text['label-enabled']."\n";
        echo "</td>\n";
        echo "<td class='vtable' align='left'>\n";
        echo "    <select class='formfld' name='enabled'>\n";
        if ($enabled == "true") {
            echo "    <option value='true' selected='selected'>".$text['label-true']."</option>\n";
        }
        else {
            echo "    <option value='true'>".$text['label-true']."</option>\n";
        }
        if ($enabled == "false") {
            echo "    <option value='false' selected='selected'>".$text['label-false']."</option>\n";
        }
        else {
            echo "    <option value='false'>".$text['label-false']."</option>\n";
        }
        echo "    </select>\n";
        echo "<br />\n";
        echo $text['description-enabled']."\n";
        echo "</td>\n";
        echo "</tr>\n";
    }
   
    echo "<tr>\n";
    echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
    echo "    ".$text['label-description']."\n";
    echo "</td>\n";
    echo "<td class='vtable' align='left'>\n";
    echo "    <textarea class='formfld' name='description' rows='4'>$description</textarea>\n";
    echo "<br />\n";
    echo $text['description-description']."\n";
    echo "</td>\n";
    echo "</tr>\n";

    if ($action == "update") {
        echo "        <input type='hidden' name='sms_destination_uuid' value='".$sms_destination_uuid."'>\n";
        echo "        <input type='hidden' name='id' id='id' value='".$sms_destination_uuid."'>";  
    }
   
    echo "</table>\n";
    echo "</form>\n";


    echo "<script>\n";
//capture enter key to submit form
    echo "    $(window).keypress(function(event){\n";
    echo "        if (event.which == 13) { submit_form(); }\n";
    echo "    });\n";
// convert password fields to
    echo "    function submit_form() {\n";
    echo "        $('input:password').css('visibility','hidden');\n";
    echo "        $('input:password').attr({type:'text'});\n";
    echo "        $('form#frm').submit();\n";
    echo "    }\n";
    echo "</script>\n";

//show the footer
    require_once "resources/footer.php";
?>

i dont see anything in my error.log
 

yukon

Member
Oct 3, 2016
138
14
18
Why don't you submit all these code changes to the projects instead of just posting them here?
 
Jan 9, 2018
140
12
18
54
Sorry, I've been out of pocket due to the holidays.

Why don't you submit all these code changes to the projects instead of just posting them here?

Part of the reason I didn't submit to the project was indeed lack of time to do proper testing, even though it's working for us. The other reason is that we're not on the master branch, so I don't know if it's fully compatible.
 

MTR

Member
Oct 25, 2017
181
9
18
45
Sorry, I've been out of pocket due to the holidays.



Part of the reason I didn't submit to the project was indeed lack of time to do proper testing, even though it's working for us. The other reason is that we're not on the master branch, so I don't know if it's fully compatible.
It’s not fully compatible with master it needs to be fixed
 
Jan 9, 2018
140
12
18
54
Also with the app I am fine, the problem is when an outside user sends sms rom him iPhone cell phone they will get the sms reply with xml data
MTR,
Regarding the problem of the XML data in the replies: I dug into this a little further. We haven't been bothered by this problem, because, for my SMS provider, Telnyx, the XML formatting causes the message to be malformed and the message send to error.

There are probably several ways to approach the problem, but if and when I get some time, here's where I would look in /app/sms/sms_hook_common.php:
PHP:
                if (count($result)) {
                    foreach ($result as &$row) {
                        $switch_cmd = "api luarun app.lua sms inbound ";
                        $switch_cmd .= $row['destination_number'] . "@" . $domain_name;
                        $switch_cmd .= " " . $from . " '" . $body . "'";
                        if ($debug) {
                            error_log(print_r($switch_cmd,true));
                        }
                        $result2 = trim(event_socket_request($fp, $switch_cmd));
                    }
                } else {
                    $switch_cmd = "api luarun app.lua sms inbound " . $match[0] . "@" . $domain_name . " " . $from . " '" . $body . "'";
                    if ($debug) {
                        error_log(print_r($switch_cmd,true));
                    }
                    $result2 = trim(event_socket_request($fp, $switch_cmd));
                }
This block is the key one that calls the lua script. This block should probably be prefaced (at this point or earlier in the code) with a check of the $body for certain key info that indicates that this is XML data of message status notification type. Exactly what that check should be will take some experimentation. If someone decides to dig into it, here's a recent example from my server, as comparison:
Code:
 BODY:  <?xml version="1.0" encoding="UTF-8"?>
<imdn xmlns="urn:ietf:params:xml:ns:imdn"><message-id>2bf521a8-0f9d-11e9-8c67-e71e0b88a897</message-id><datetime>2019-01-03T21:18:53Z</datetime><delivery-notification><status><delivered/></status></delivery-notification></imdn>
Code:
 BODY: <?xml version="1.0" encoding="UTF-8"?>
<imdn xmlns="urn:ietf:params:xml:ns:imdn"><message-id>2bf521a8-0f9d-11e9-8c67-e71e0b88a897</message-id><datetime>2019-01-03T21:18:53Z</datetime><display-notification><status><displayed/></status></display-notification></imdn>
 

MTR

Member
Oct 25, 2017
181
9
18
45
I noticed that since I enabled mod_sms my server cpu is always at 100%
 
Jan 9, 2018
140
12
18
54
Here's an update to the /freeswitch/scripts/app/sms/index.lua file. This fixes some issues created in a recent update of Groundwire, but I believe it should also fix the problem with XML delivery notifications being sent via SMS. If you've made recent changes to this file, you may want to do a diff-check and apply the differences. The changes are primarily around the Groudwire cleanup section and then adding the XML check before the large "if" block that actually triggers the sends to the various carriers.

I would have liked to have a more comprehensive check where it does the XML test, but I couldn't get the matching to work on any larger chunks.
 

Attachments

  • index.lua.txt
    18.6 KB · Views: 26
Jan 9, 2018
140
12
18
54
Sorry, I made some other significant changes back in Q3-Q4 of last year to /app/sms/index.lua that I forgot about until now. They are included above. One of the changes is that it will now allow for email delivery of inbound SMS messages to an email address. In order to enable this, you have to:
  1. Create a user.
  2. Add a user to the extension.
  3. Go into Apps->Contacts, and add an email address with a Label of "SMS" or "sms".
 
  • Like
Reactions: yukon
Jan 9, 2018
140
12
18
54
I finally have a test system on master branch (4.5.3 on FS 1.8.4), and it is working with the above /freeswitch/scripts/app/sms/index.lua.

@MTR, you might try it again, at your convenience and let me know what issues you are still seeing.
 

FuncVOIP

New Member
Apr 20, 2018
14
1
3
28
Hello Jonathan,

I am trying to implement on 4.4 and I am not getting the destinations to show. Are you meant to update index.lua first now?
 

taptech

Member
Mar 6, 2017
50
10
8
Has anyone configured this using thinQ? I see that it is sort-of implemented. There's some mentions in the code but it doesn't appear in default settings or in the carrier drop down.
 
Jan 9, 2018
140
12
18
54
Hello Jonathan,

I am trying to implement on 4.4 and I am not getting the destinations to show. Are you meant to update index.lua first now?
No, you shouldn't need to update index.lua. However, back in December, I posted a comment indicating what needed to be changed in sms.php for the destinations to show up. See attached. This should work. However, you may also need to change some things in the sms_edit.php file, if you want to do the alternative routing. You might review that post in this thread.

If life slows down a bit, I hope to be able to package these changes and do a submit to the project.
 

Attachments

  • sms.php.txt
    8.9 KB · Views: 6
  • sms_edit.php.txt
    9 KB · Views: 3

FuncVOIP

New Member
Apr 20, 2018
14
1
3
28
No, you shouldn't need to update index.lua. However, back in December, I posted a comment indicating what needed to be changed in sms.php for the destinations to show up. See attached. This should work. However, you may also need to change some things in the sms_edit.php file, if you want to do the alternative routing. You might review that post in this thread.

If life slows down a bit, I hope to be able to package these changes and do a submit to the project.
Thanks! I will try this later today! I was trying to follow the posts last night, but got lost. I will update you with my results later tonight or early tomorrow morning.
 
Jan 9, 2018
140
12
18
54
Has anyone configured this using thinQ? I see that it is sort-of implemented. There's some mentions in the code but it doesn't appear in default settings or in the carrier drop down.
I haven't set up/used it with thinQ, but then, it's tough to test with a carrier if you do not have an account with them. In my install, I have it in default settings and it shows in the drop-down as an option. If you do not have it, then something is wrong with your /app/sms/app_config.php file. It should have about five instances of the word "thinq". If it does, then you might try re-running Advanced->Upgrade->App Defaults, then log out and back in. If it doesn't, here's a copy of mine.

Once you have it showing up, just go into Default Settings and configure the username, and access key for thinQ, and don't forget to update the ACCOUNT in the api_url, as well.
 

Attachments

  • app_config.php.txt
    18.4 KB · Views: 8

FuncVOIP

New Member
Apr 20, 2018
14
1
3
28
No, you shouldn't need to update index.lua. However, back in December, I posted a comment indicating what needed to be changed in sms.php for the destinations to show up. See attached. This should work. However, you may also need to change some things in the sms_edit.php file, if you want to do the alternative routing. You might review that post in this thread.

If life slows down a bit, I hope to be able to package these changes and do a submit to the project.
Hello Jonathan,

I uploaded the php files you provided and I still get nothing when I try to submit.
Do you suggest I upgrade my app defaults?
 

FuncVOIP

New Member
Apr 20, 2018
14
1
3
28
I read on the php some sql inserts for v_sms... and I can see I have that missing on my end.
Could this be why I am not seeing the destinations"?

1553691914884.png
 

taptech

Member
Mar 6, 2017
50
10
8
I haven't set up/used it with thinQ, but then, it's tough to test with a carrier if you do not have an account with them. In my install, I have it in default settings and it shows in the drop-down as an option. If you do not have it, then something is wrong with your /app/sms/app_config.php file. It should have about five instances of the word "thinq". If it does, then you might try re-running Advanced->Upgrade->App Defaults, then log out and back in. If it doesn't, here's a copy of mine.

Once you have it showing up, just go into Default Settings and configure the username, and access key for thinQ, and don't forget to update the ACCOUNT in the api_url, as well.
Thanks Jonathan, I probably have an old git pull. I'll scope it out! I have service from thinQ so I will report back and let oyu know how it goes.
 

taptech

Member
Mar 6, 2017
50
10
8
Well that's embarrassing... I followed the instructions verbatim... but my Fusionpbx install is in /var/www and not /user/src
I must have pulled this to try it out some time ago and forgotten. I can't say this is the first time that's happened either.... :)
 
Status
Not open for further replies.