Syed Jahanzaib Personal Blog to Share Knowledge !

July 30, 2015

playSMS Send SMS via Script/CLI using webservices token and PHP

Filed under: Linux Related — Tags: , , — Syed Jahanzaib / Pinochio~:) @ 10:56 AM

playSMS_logo_full

If you want to send SMS via playSMS using bash script or CLI, use the WEB SERVICES options in playSMS to facilitate this function. Its very useful in many scenarios.

Example:

I wanted to schedule a bash script which should run daily twice , this script collects various information of different servers, routers and then summarize it and send to admin via SMS. I could do it with GAMMU or other sms tools too, but the issue was that the server already have KANNEL with gsm modem configured and (in general standard) Kannel dont accept message with special characters, spaced, and strange formats that this script output, (with urlencoding possible but i dont want to do it) or I had to use sed awk and other tools to proper format them which was overhead work. and I wanted to utilized KANNEL or playSMS.

Mr. Anton from playSMS suggested to use web-services of playSMS and it worked beautifully ūüôā

This is how it was done.


 

1- Enable WEB SERVICES & GENERATE TOKEN

Goto My Account > User Configuration

Select YES from drop down menu for “Enable webservices” and “Renew webservices token

Click on SAVE.

As showed in the following image below …

 

1- token


 

Now on same page you will now see the TOKEN number as showed in the following image below …

2 token

Copy / note down this this number, It will serve as a kind of password to let us send sms via php script.


 

2- Create & schedule  PHP script to execute your shell script

Now we have to create a PHP script which will use the above TOKEN to let us run the bash script and send it’s output to admin mobile number ūüôā

mkdir /temp
touch /temp/dailysms.php
chmod +x /temp/dailysms.php
nano /temp/dailysms.php

now paste the following date , but be sure to modify the token number as per your own setup, and the script which you want to execute via php.

#!/usr/bin/php -q
# Script provided by mr.anton
# https://forum.playsms.org/t/schedule-message-to-run-sms-command/194/2
# https://aacable.wordpress.com
# Dated: 30th July, 2015
# Syed Jahanzaib

<?php

/**
 * cli2sms.php by Anton Raharja (antonrd@gmail.com)
 * Example script to get data from shell script and send it as SMS via playSMS
 * You need to have a configured and working playSMS
 * In this example playSMS is accessible from http://localhost/playsms
 * Don't forget to chmod +x cli2sms.php to use it from Linux shell
 *
 * You may modify this script to suit your needs
 *
 * Example usage:
 * - get stat data (eg: uptime) and send it periodically (using cron) to admin's mobile phones
 * - https://aacable.wordpress.com/2015/07/30/playsms-send-sms-via-scriptcli-using-webservices-token/
 */

// suppress error message
error_reporting(0);

// playSMS username/account for sending SMS
$username = 'admin';

// Webservices token for above username
$token = '1194df9e20d06c3790f0c6fef49f174a';

// playSMS Webservices URL
$playsms_ws = 'http://localhost/playsms/index.php?app=ws';

// destination numbers, comma seperated or use #groupcode for sending to group
// $destinations = '#devteam,+6200123123123,+6200456456456';
// $destinations = '+6200123123123,+6200456456456'; # for multiple recipients</pre>
<pre>$destinations = '03333021909';</pre>
<pre>
// get message to send from another shell script or Linux command, for example 'uptime'
// $message = trim(shell_exec('uptime'));
$message = trim(shell_exec('/temp/dailysms.sh'));</pre>
<pre>
// send via playSMS HTTP API
if ($message) {
    $ws = $playsms_ws . '&u=' . $username . '&h=' . $token . '&op=pv';
    $ws .= '&to=' . urlencode($destinations) . '&msg='.urlencode($message) . '&nofooter=1';
    $ret = @file_get_contents($ws);

    // echo $ret;
    echo "OK: message sent" . PHP_EOL;
} else {
    echo "ERROR: message is empty" . PHP_EOL;
}

// end of script

 

 


TEST

Now test by running the php file we just created above.

/temp/dailysms.php

and you should see something like below if every thing goes smoothly as planned

root@radius:/temp# ./dailysms.php

OK: message sent

 

and you will soon receive the SMS on your Mobile.

3- mobile


Update: / TIPS
Later I found howto submit sms via kannel CLI as well. Described in the below link.

https://aacable.wordpress.com/2012/11/26/howto-configure-your-local-http-gateway-using-kannel-on-ubuntu/

How to Send SMS via HTTP command

Open your browser and paste the following command.

http://101.11.11.250:13013/cgi-bin/sendsms?username=kannel&password=kannel&to=03333021909&text=ZAIB+KANNEL+WORK

Howto send SMS via GAMMU CLI

gammu sendsms TEXT 03333021909 -text "Test Msg from GAMMU  .."

Howto send USSD CODES via GAMMU CLI

For balance check (below example is for zong balacne check)

gammu --getussd *222#

Howto send sms via CLI using KANNEL

curl "http://localhost:13013/cgi-bin/sendsms?username=kannel&password=KANNELPASS&to=03333021909&text=Welcome+Jahanzaib+Test+Message"

Sending mesage with new line using %0A code using KANNEL

curl "http://localhost:13013/cgi-bin/sendsms?username=kannel&password=KANNELPASS&to=03333021909&text=Welcome+Jahanzaib+Test+Message%0ANew+line"

Howto send sms using FILE via CLI using KANNEL

curl "http://127.0.0.1:13013/cgi-bin/sendsms?username=kannel&password=kannelpassword&to=03333333333" -G --data-urlencode text@/temp/message.txt

Howto send command output  sms via CLI using KANNEL

ls -l | curl "http://127.0.0.1:13013/cgi-bin/sendsms?username=kannel&password=kannelpassword&to=03333333333" -G --data-urlencode text@-

 

Regard’s

Syed Jahanzaib

July 16, 2015

%d bloggers like this: