Send an SMS/Text with mass actions

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
dunger
Posts: 2
Joined: 2020-01-06 17:30

Send an SMS/Text with mass actions

Post by dunger » 2020-01-11 11:56

We had a need to contact individuals in our database quickly during our bushfire crises downunder.
The most reliable way was via text messaging and the mass action function menu in appgini is perfect for a quick implementation!
We wanted to use our own devices rather than a third party text message service for sending the SMS.
I thought the following simple scripts may be useful to others in the appgini community.
jquery is more compact, but to make it readable for those not used to using the framework I've used JavaScript.

First, add a menu item to your '*TableName*.php' file in the hooks folder (remember to replace *TableName* with yours!):

find this function: function *TableName*_batch_actions(&$args)

In the return array inside this function, make it look like this if you haven't added any other batch functions:

Code: Select all

return array(array(
           'title' => 'Text Contacts',
           'function' => 'text-contacts',
           'icon' => 'th-list'
			)
	);
Second, append this javascript function to handle the menu item to '*TableName*-tv.js': (create this file if it doesn't exist)

Code: Select all

function text_contacts(table_name, ids){
// get parameters to send
    var url = 'text-contacts.php?table=' + table_name;
    for(var i = 0; i < ids.length; i++){
        url = url + '&'
            + encodeURI('ids[]') + '='
            + encodeURIComponent(ids[i]);
    }
// Ajax call	
var phonenumbers = '';	// response text
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {					// Ajax call back function
	if(xhr.readyState == 4 && xhr.status ==200) {
		phonenumbers = xhr.responseText;
		url = "sms://open?addresses=" + phonenumbers + "&body="; //works in safari on mac & ios. change for android or get fancy and handle both!
		location.href = url; //  open messages app
		
	}
}
xhr.open("GET", url ,true);
xhr.send();

};
Third, create a php file called 'text-contacts.php' on your server to process the selected contacts and return a list of mobile phone numbers:

Code: Select all

<?php
    /*
      Including the following files allows us to use many shortcut
      functions provided by AppGini. Here, we'll be using the
      following functions:
        makeSafe()
            protect against malicious SQL injection attacks
        sql()
            connect to the database and execute a SQL query
        db_fetch_assoc()
            same as PHP built-in mysqli_fetch_assoc() function
    */
    $curr_dir = dirname(__FILE__);
    include("{$curr_dir}/defaultLang.php");
    include("{$curr_dir}/language.php");
    include("{$curr_dir}/lib.php");
     
    /* receive calling parameters */
    $table = $_REQUEST['table'];
    $ids = $_REQUEST['ids']; /* this is an array of IDs */
     
    /* a comma-separated list of IDs to use in the query */
    $cs_ids = '';
    foreach($ids as $id){
        $cs_ids .= "'" . makeSafe($id) . "',";
    }
    $cs_ids = substr($cs_ids, 0, -1); /* remove last comma */
				
    $phone = sql( "SELECT Lastname, Firstname, mobile from Contacts " . //Replace 'Contacts' with your table name and 'mobile' with your field name!
				"WHERE Contacts.id in ({$cs_ids}) " .					// LastName and FirstName are not necessary unless you want to compile records without mobile numbers.
				"ORDER by LastName", $eo);

$phonelist = ''; //numbers to be returned from Ajax query
$nonumberlist = ''; // names selected without phone numbers

while($row = db_fetch_assoc($phone)){

	if($row['mobile'] != '') {
		$phonelist .= $row['mobile'].',';
	}
	else {
		$nonumberlist .= $row['Firstname'].' '.$row['Lastname'].',';
	}

}

$phonelist = substr($phonelist, 0, -1); /* remove last comma */
$nonumberlist = substr($nonumberlist, 0, -1); /* remove last comma */

echo $phonelist; // send those numbers back. Use json if you want to add the $nonumberlist
These scripts could be improved considerably to make them more general. For example the url call is for safari on mac and ios systems.
We didn't have to check in the script for the user agent and adjust for android devices. Also a list of names without numbers is generated that can be returned if needed to alert the user, perhaps using json encode.

lectura
Posts: 28
Joined: 2015-01-10 13:29

Re: Send an SMS/Text with mass actions

Post by lectura » 2020-02-12 18:59

I have done so, where do i get the area to send the sms? is it in admin area?

dunger
Posts: 2
Joined: 2020-01-06 17:30

Re: Send an SMS/Text with mass actions

Post by dunger » 2020-02-12 20:03

Hi lectura,

When you are in table view for the table action you have created, you will have selection boxes next to each record.

Selecting one or more records will make the mass action button "More" appear to the right. Click this to access the new texting function you have created.

Here is a screenshot from my application:
textscreenshot.png
textscreenshot.png (198.68 KiB) Viewed 6115 times

Mahmud
Posts: 12
Joined: 2017-03-12 08:53

Re: Send an SMS/Text with mass actions

Post by Mahmud » 2022-06-01 23:58

function text_contacts(table_name, ids){
// get parameters to send
var url = 'text-contacts.php?table=' + table_name;
for(var i = 0; i < ids.length; i++){
url = url + '&'
+ encodeURI('ids[]') + '='
+ encodeURIComponent(ids);
}
// Ajax call
var phonenumbers = ''; // response text
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() { // Ajax call back function
if(xhr.readyState == 4 && xhr.status ==200) {
phonenumbers = xhr.responseText;
url = "sms://open?addresses=" + phonenumbers + "&body="; //works in safari on mac & ios. change for android or get fancy and handle both!
location.href = url; // open messages app

}
}
xhr.open("GET", url ,true);
xhr.send();

};


where i input this file not clear
please give me 3 demo file
and file location

User avatar
crnjak
Veteran Member
Posts: 37
Joined: 2021-12-29 20:25

Re: Send an SMS/Text with mass actions

Post by crnjak » 2022-08-26 07:50

Can you create simple tutorial how to integrate in our apps this funcion sending SMS.
I have strugle with horisen API over a half year...

Post Reply