SMS Server Tools 3
This site is hosted by Kekekasvi.com
 Menu
Basic information:
Additional information:
Support:
Get SMS Server Tools 3:
Additional Options

 Sponsored links

 Search
Custom Search

 Visitor locations
 
 SMS Server Tools 3 Community
Welcome, Guest. Please login or register. Sat May 25, 2024 10:35
SMSTools3 Community » Help and support Bottom

[answered] Eventhandler that executes PHP scripts

  This topic is locked

Page:  1

Author Post
Member
Registered:
Dec 2010
Location: United States of America
Operating system name and version: Ubuntu 10.10
Version of smsd: 3.1.14
Name and model of a modem / phone: Option ICON 322 Unlocked
Interface: USB

I got my modem sending and receiving messages without a problem, but now I'd like to do something with those incoming and outgoing messages. I've searched on the forums and have messed around with the eventhandler function but cannot get it to work.

My goal is to receive a message, have the eventhandler extract all the data from the text and turn it into variables (from_number, date, message, etc.) and then have the shell script execute the php files that I have in /var/www/sms/sms.php so that when the php files are executed they run through a series of checks and update my SQL database.

So i guess i would like an example of the shell script that would be called and then what would the smsd.conf file look like with the eventhandler called?

I've also messed around with a popular program called "NowSMS" for windows and they allow to post the incoming messages to HTTP with a bunch of GET variables but I don't know if this linux program could do something like this.

Thanks in advanced!!
Chad

Administrator
Registered:
May 2009
Location: Jyväskylä, Finland
This sample shows how data can be extracted using a shell script: Sample eventhandler to store messages into SQL database.

This sample is also a shell script and it calls PHP script: [answered] SMS to execute a script howto.

As your PHP script will do the checks and update the DB, you probably could use the PHP only, without any shell scripts. In this case the definition in smsd.conf will be:
eventhandler = /var/www/sms/sms.php

In this case more correct place for the script could be /usr/local/bin, not /var/www. The sms.php should be executable for smsd and look like:

#!/usr/bin/php
<?php

$sms_type = $argv[1];
$sms_file = $argv[2];

// Your code here...

?>
 
'php' Syntax Highlight powered by GeSHi


Here is how to extract headers and message body from the SMS file:

#!/usr/bin/php
<?php

$sms_type = $argv[1];
$sms_file = $argv[2];

$sms_file_content = file_get_contents($sms_file);
$i = strpos($sms_file_content, "\n\n");
$sms_headers_part = substr($sms_file_content, 0, $i);
$sms_message_body = substr($sms_file_content, $i + 2);
$sms_header_lines = split("\n", $sms_headers_part);
$sms_headers = array();

foreach ($sms_header_lines as $header)
{
  $i = strpos($header, ":");
  if ($i !== false)
    $sms_headers[substr($header, 0, $i)] = substr($header, $i + 2);
}

// Your code here...

?>
 
'php' Syntax Highlight powered by GeSHi


You can then do with headers and message body whatever is needed, like store to the DB, forward, or anything. This sample stores some data to file:

if ($sms_type == "RECEIVED")
{
  $data = "Got SMS from " .$sms_headers['From'] ."\n"
          ."Message:\n"
          .$sms_message_body;

  file_put_contents("/tmp/sms_test", $data);
}
 
'php' Syntax Highlight powered by GeSHi


Incoming messages can be forwarded to HTTP using wget and PHP's exec function, for example:

if ($sms_type == "RECEIVED")
{
  $to_exec = "wget -q \"http://host/script.php"
             ."?from=" .$sms_headers['From']
             ."&message=" .urlencode($sms_message_body)
             ."\" -O /dev/null";

  exec($to_exec);
}
 
'php' Syntax Highlight powered by GeSHi


Member
Registered:
Dec 2010
Location: United States of America
Topic owner
Thanks for the response!

This is what I have so far:

smsd.conf


index.php file that is called from eventhandler


When I send a text message it seems to not go to my apache server with the GET variables to run my script. I've restarted the service multiple times with no errors and all my files & folders are executable. Do I have something wrong?

Administrator
Registered:
May 2009
Location: Jyväskylä, Finland
Does the log of apache show that the script sms/sms.php is called properly?

If it does, you could show the sms/sms.php script here.

If it does not, can you verify that you have wget available (# which wget)?

Member
Registered:
Dec 2010
Location: United States of America
Topic owner
Apache did not log anything for the script to be called. And yes wget is installed as i have Ubuntu 10.10

Administrator
Registered:
May 2009
Location: Jyväskylä, Finland
You could test your eventhandler manually. To see how wget works, remove -q and -O /dev/null. Check what is your latest incoming message file: ls -ltr /var/spool/sms/incoming. Run:
/usr/local/bin/sms/index.php RECEIVED /var/spool/sms/incoming/GSM1.<random>

What is the result?

Member
Registered:
Dec 2010
Location: United States of America
Topic owner
I get:



Member
Registered:
Dec 2010
Location: United States of America
Topic owner
I got it to work! Thanks for your help! The problem was that the index.php file that was being called from the eventhandler was orginally created in windows so I did some research and used dos2unix command to convert it and then it worked and now not only does the wget command work in terminal, it also works when I send a text to the modem from my phone!! :)

One more thing! lol Sorry :/
Since I am running the PHP script from localhost how might I post back a message to the the same GSM modem that recieved the message for example:

Text sent to GMS1 "Hello"
Runs through PHP scripts
Updates sql database
Then at the end of the script it automatically replies back to the number that sent it
GSM1 replies: "Thanks for texting me"

Is that done through another eventhandler or is it done through PHP on the Apache side?

Administrator
Registered:
May 2009
Location: Jyväskylä, Finland
chadpriddle wrote
Since I am running the PHP script from localhost how might I post back a message to the the same GSM modem that recieved the message for example:

Text sent to GMS1 "Hello"
Runs through PHP scripts
Updates sql database
Then at the end of the script it automatically replies back to the number that sent it
GSM1 replies: "Thanks for texting me"

Is that done through another eventhandler or is it done through PHP on the Apache side?

I assume that you have SMSTools3, PHP and SQL running on the same server, and Apache on the another server. After SQL database is updated successfully, you can simply create an answer SMS file which contains appropriate message and is sent to $sms_headers['From'] number.

On how to create SMS files with PHP, see these samples: smstest.php, Multiple recipients in single SMS file.

This site is also useful: http://www.php.net/.

Member
Registered:
Dec 2010
Location: United States of America
Topic owner
I know this is really basic:



I'm sure I'm going to have to so some string conversion to make sure everything pass through correctly.

Everything works perfectly tho! I'm so stoked :mrgreen:

Administrator
Registered:
May 2009
Location: Jyväskylä, Finland
You should write to the "*.LOCK" file as those samples do. If smsd sees incomplete file (which is not locked) in the outgoing directory, bad things may happen.

And on Linux do not use carriage return ("\r").

Member
Registered:
Dec 2010
Location: United States of America
Topic owner
So the files in the outgoing folder should have ".LOCK" at the end? Example: "sms_send_581204.LOCK"

I modified my script and now the files won't send out, they just stay in the outgoing folder. Could you please clarify?

Member
Registered:
Dec 2010
Location: United States of America
Topic owner
Do you mean I should add the LOCK and then when the file is done being created then I should rename it after is is complete?! Because that makes sense to me

Administrator
Registered:
May 2009
Location: Jyväskylä, Finland
chadpriddle wrote
Do you mean I should add the LOCK and then when the file is done being created then I should rename it after is is complete?! Because that makes sense to me

Yes.

Like the sample does:

$application = "web_sendsms";
$outgoing = "/var/spool/sms/outgoing";
$error = 0;
 
...

//$filename = $outgoing ."/". $application ."-" .mt_rand(1, 99999);
$filename = $outgoing ."/". $application ."-" .str_replace(".", "", uniqid(mt_rand(), true));
if (($handle = fopen($filename .".LOCK", "w")) != false)
{
  fwrite($handle, "To: " .trim($dest) ."\n");
  fwrite($handle, "\n");
  fwrite($handle, $message);
  fclose($handle);
 
  if (rename($filename .".LOCK", $filename) != true)
    $error = 3;
}
else
  $error = 2;

...
 
'php' Syntax Highlight powered by GeSHi


EDIT: I also have changed the way how random filename is generated. The new one is better.


« Last edit by keke on Tue Dec 14, 2010 12:48, 163 months ago. »
Member
Registered:
Dec 2010
Location: United States of America
Topic owner
This is software is amazing! Thanks for everything

FYI
I have Ubuntu server running SMS Server Tools 3, PHP and Apache with my scripts to tell what the incoming/outgoing texts should do. My business website with the SQL database is hosted separately. I'm looking into hosting everything all on one server or at lease on the same local network.


« Last edit by chadpriddle on Tue Dec 14, 2010 20:23, 163 months ago. »

  This topic is locked

Page:  1

SMSTools3 Community » Help and support Top

 
Time in this board is UTC.  

Privacy Policy   SMS Server Tools 3 Copyright © Keijo Kasvi.