Last active
December 31, 2015 00:39
-
-
Save Rudis1261/7908705 to your computer and use it in GitHub Desktop.
SMS Tools Event Handler. So the idea to to create an event handler for SMS Tools, which will work in a similar way to a USSD session. User get's the menu with options. We SMS the options back to them. Then they issue a subsequent message to get the information they need back via SMS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
# This function will be used to get the information from the inbound SMS | |
function decode($string) | |
{ | |
# Split the text into lines | |
$lines = explode("\n", $string); | |
if (!empty($lines)) | |
{ | |
# The actual message content would be the last entry | |
$message = end($lines); | |
# Return the message | |
return $message; | |
} | |
# Default to a failure | |
return false; | |
} | |
# This function will take the inbound text and dispatch it to a command | |
function menu($message=false) | |
{ | |
# Ensure that we have a message | |
if ($message !== false) | |
{ | |
# Should the message contain a space, it will be a command => value format | |
if (strstr($message, " ")) | |
{ | |
# The Command will be the first word | |
$explode = explode(' ', $message); | |
$command = array_shift($explode); | |
# The context the remaining word / words | |
$context = implode(' ', $explode); | |
# Log the data | |
logger("OK: COMMAND: " . $command); | |
logger("OK: CONTEXT: " . $context); | |
} | |
# Otherwise the command is the message | |
else | |
{ | |
$command = $message; | |
logger("OK: COMMAND: " . $command); | |
} | |
# Switch through the message options | |
switch($command) | |
{ | |
# Just in case we recei | |
case "": | |
logger("ERROR: No command provided"); | |
break; | |
default: | |
logger("ERROR: Command " . $command . " not found in system"); | |
break; | |
} | |
} | |
} | |
# I would also like to make the Logger generic | |
function logger($message=false) | |
{ | |
if ($message) | |
echo time() . " " . date('d M Y H:i', time()) . " | " . $message . PHP_EOL; | |
} | |
# We received more than just one arguments | |
if (count($argv) > 1) | |
{ | |
logger("=============================== START ================================="); | |
# We need to remove the first index as it's always irrelevant. | |
array_shift($argv); | |
# Attempt to write the last arguments the event handler received | |
file_put_contents("/tmp/inbound_sms_arguments.txt", implode(', ', $argv)); | |
# We received an SMS | |
if ($argv[0] == "RECEIVED") | |
{ | |
// Logger | |
logger("OK: Received Message"); | |
# Does the file exist? | |
if (file_exists($argv[1])) | |
{ | |
// Logger | |
logger("OK: File exists, attempting to read it"); | |
# Attempt to read the SMS, and decode it | |
$readFile = file_get_contents($argv[1]); | |
# Cool we could read it | |
if ($readFile !== false) | |
{ | |
// Logger | |
logger("OK: Attempting to decode the message"); | |
$decode = decode($readFile); | |
# Should the decode be successful | |
if ($decode !== false) | |
{ | |
// Logger | |
logger("OK: Message decoded, sending to menu for dispatch"); | |
# send the data to the menu function to dispatch the action | |
menu(strtolower($decode)); | |
} | |
# Couldn't decode the file | |
else | |
{ | |
// Logger | |
logger("ERROR: We were not able to decode the message: " . $readFile); | |
} | |
} | |
# PHP Couldn't get the files content | |
else | |
{ | |
// Logger | |
logger("ERROR: Weird, PHP couldn't read the inbound SMS file's content: " . $argv[1]); | |
} | |
# Write the last SMS we received | |
file_put_contents("/tmp/inbound_sms_content.txt", $readFile); | |
} | |
# Weird PHP could not pick the file up | |
else | |
{ | |
// Logger | |
logger("ERROR: Weird, PHP couldn't read the inbound SMS file: " . $argv[1]); | |
} | |
} | |
# We received something else | |
else | |
{ | |
// Logger | |
logger("INFO: Not inbound message, ignoring: " . implode(', ', $argv)); | |
} | |
logger("=============================== END ==================================\n"); | |
} | |
# Exit with a good state | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey thanks for the gist but i don't know where to put this. I don't know where to check my receive messages please help 😄