I was hunting around the internet for a way to get my slug to text me via SMS when things happen and stumbled across the orange developer APIs and was pleasantly surprised! I was hoping for a simple gateway to send SMS messages from so that I could get a text alerts if/when things go wrong and I found a whole lot more
“The orange SMS API allows you to send and receive text messages within your web application.” is the description from the orange website
The API not only allows you to send texts, but also to receive them. This sounds a little wierd but it’s really simply, so I’ll explain with the example I built!
First you need to be an orange customer and you have to register for the developer website. Once you’ve done that, you need to get your access code – a unique number much like the google api codes. Once you’ve got it, the next thing to do is add a prefix. This is what your message should start with. The prefix you choose might have gone, so you may have to try a few.
To send a text, basically, you issue a call to the following, passing your access key, the destination mobile, the text, and which number its coming from.
http://sms.alpha.orange-api.net/sms/sendSMS.xml?id=[access key]&to=>[mobile number]&content=[your text message]&from=[mobile number]
To receive a text, when you register your prefix, you get the option to select either an email address or a URL. If you pick an email address, then that email address will receive a copy of the text. If it’s a URL, then the message will be delivered to the specified URL with information added as URL parameters. The parameters are:
|api||This is the name of the API function – receivesms.|
|content||This is the content of the text message, limited to 160 characters.|
|from||This is the origin of the text message in international format. This is either a phone number or an alias.|
|to||This is the destination number of the SMS|
So an example URL would be something like:
This is all documented in great depth on the orange developer website so I won’t go into this too much.
The next step was making this work on my NSLU2
I have the lighttp webserver with FCGI installed via IPKG running on my NSLU. This runs on port 8081 by default. In my router, I’ve port forwarded port 80 to 8081 on my NSLU2 to open lighttp up to the internet.
The next thing to do was to create a PHP page to recieve a text and do something with it. My first idea to test this was to use a shell script I have on the NSLU2. This shell script simply scans the var/log/messages file to find when the last successful login was today and how many unsuccessful logins there have been today.
The shell script looks like this:
lastLogin=`cat /var/log/messages | grep "sshd" | grep "Accepted publickey for" | tail -2 | head -1`<br />
numberInvalids=`cat /var/log/messages | grep "sshd" | grep "Invalid user" | wc -l`
echo 'Last successful login occurred on:' $dateStr' - Number of invalid user attempts: '$numberInvalids''
So my aim – to send a text to the NSLU2, and get back this message from my shell script. I created a PHP file like the following:
$content = $_GET['content'];
$from = $_GET['from'];
$fid = fopen('~xxxxxxxxxxxxxxxxxxxx/SMSdatabase.txt','a');
$toexec="~xxxxxxxxxxxxxxxxxxxx/commands/lastloginsms.sh" . $program;
$returnmessage = $exec;
$phone_number = urlencode($from);
$text_message = urlencode($returnmessage);
$url = "http://sms.alpha.orange-api.net/sms/sendSMS.xml?id=XXXXXXXXXXX&to=$phone_number&content=$text_message";
$response = file_get_contents($url);
$xml = simplexml_load_string($response);
echo "Status: ",$xml->status->status_msg;
This does a few things – firstly it gets the URL parameters, then writes out the message to a text file so I can keep a track of what messages were received.
Next, it calls the shell script lasloginsms.sh.
Finally, it takes the output of the shell script, and sends that as an SMS back to the sender!
So, to make it all work now, I simply text (from my orange phone)
And a few seconds later I get a text back with a response similar to:
Last successful login occurred on: Sep 5 09:46:01 - Number of invalid user attempts: 7
You can have 5 prefixs on the devloper website, so the next step is to create a few different PHP scripts that work with the prefixs to stop/start services on the NSLU2 via SMS and provide status reports. A good example of use would be, should I ever lock myself out of the SSH server (by denyhosts), then I could SMS my slug to enable telnet, log in, and remove the entry from the hosts.deny file!
I’ll post when I get this working!