Tuesday, April 1, 2008

gammu sms cgi daemon




Did anyone try to process sms request and send a response upon that ? I believe the gammu library + cgi idea is the way to freedom of sms processing. I have written a small code to implement the idea. Here you can grab the source(let me know if it goes broken). You can get the svn version at svn://svn.cihar.com/gammu/trunk/other/ (thanks to Michal Čihař).

Here is the work-flow to install and use,

1. Attach an appropriate phone to your system where you want to execute the server application.
2. Install gammu-1.17 or upper(Not sure if lower version may work too, I did not try with lower versions :-P ).
3. Get the smscgi from the link above, uncompress and make.
This will create "smscgi" executable file.

4. To make it work please edit the gammu config file
and add a config like,

cgi-bin = /your/script/path

5. Now write a script and keep it in the cgi-bin directory, Here is an example script,


#!/usr/bin/php
# filename echo
<?php

$_HEADER = array();
while($data = fgets(STDIN, 4096)) {
$data = trim($data);
if($data == "") {
# empty line means from next line we shall get the sms body
break;
}
$matches = array();
if(preg_match("/^(.*)\:(.*)/", $data, $matches)) {
$_HEADER[$matches[1]] = $matches[2];
}
}
$_REQUEST = "";
while($data = fgets(STDIN, 4096)) {
$_REQUEST .= $data;
}

# So,
# $_REQUEST is the sms data.
# $_HEADER is the header array. For example $_HEADER["SMS_FROM"] is the sender number.

unset($data);
unset($matches);

# Now echo the message ..
echo $_REQUEST;



Note, you can use the above script as include file to your new script . Be sure to remove the
echo() in that case.

6. You can test your script running it in shell like,


shell#> ./echo


It will wait for your input. You can write the headers like,

SMS_FROM: +1234567


The headers are key value pairs. Currently it gives the number from, and date of receiving the message.

The you put two new lines(or one blank line), this will indicate that there is no more headers. It will then treat the further data as sms message. Finally you can press EOF( in unix systems it is Ctrl-D).


SMS_FROM: +1234567
(note blank line here)
I am fine


The above input will output "I am fine" and exit.


6. Now run the server. When the user sends sms like,

echo I am fine


The server will find executable file named "echo" at
cgi-bin path. If "echo" does not exist then the it will
execute "default" script.

7. If your script has any error or if you do any output to stderr
it will be logged to cgi-bin/yourfilename.err . This may help you debug your
script.

8. Now write your own scripts and enjoy !

You can get more information on mobile setup for gammu at http://www.gammu.org/ .
Finally all we are using is opensource .. it is under GPL-3 :)

1 comment:

Sahill said...

How I can get mysql data from remote machine? as i don't want to engage my local server with MySQL. I've install Gammu sms gateway from here:

http://www.syednetworks.com/category/gammu-sms-gateway

but they mentioned mysql method on local machine but not on remote one.

please help