-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Perceptlab Server
committed
Feb 14, 2011
1 parent
a1d468e
commit f508778
Showing
6 changed files
with
538 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
class ambientInboundSms { | ||
|
||
public $transaction; | ||
public $id; | ||
public $sender; | ||
public $destination; | ||
public $message; | ||
public $in_reply_to; | ||
public $received_at; | ||
public $options; | ||
|
||
function __construct($sender, $destination, $message, $received_at, $in_reply_to, $conf) { | ||
$this->sender = $sender; | ||
$this->destination = $destination; | ||
$this->message = $message; | ||
$this->received_at = $received_at; | ||
$this->in_reply_to = $in_reply_to; | ||
|
||
//Configuration | ||
$this->options = $conf['options']; | ||
|
||
//Transaction log | ||
$this->transaction = new stdClass(); | ||
} | ||
|
||
static function parseInbound() { | ||
|
||
$POST = file_get_contents("php://input"); | ||
|
||
// msisdn=27826941134&shortcode=2782007210000006&keyword=Hi&msg=Hi+dude&message_id=1 | ||
$ret = new ambientInboundSms($_POST['msisdn'], $_POST['shortcode'], $_POST['msg'], time(), $_POST['message_id'], array()); | ||
$ret->transaction->request = $POST; | ||
return $ret; | ||
|
||
} | ||
|
||
function logTransaction() { | ||
if($this->id) { | ||
drupal_write_record("ambient_sms_gateway_inbox", $this, "id"); | ||
drupal_write_record("ambient_sms_gateway_inbox_transaction", $this->transaction, "id"); | ||
} else { | ||
drupal_write_record("ambient_sms_gateway_inbox", $this); | ||
|
||
//Set the transaction id if it isnt yet set | ||
if($this->id && !$this->transaction->id) { $this->transaction->id = $this->id; } | ||
drupal_write_record("ambient_sms_gateway_inbox_transaction", $this->transaction); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
; $Id$ | ||
name = Ambient sms gateway | ||
description = Send and receive SMS messages using the Ambient Gateway | ||
core = 6.x | ||
|
||
package = SMS Framework |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
// $Id$ | ||
/** | ||
* @file ambient_sms_gateway.install | ||
* Supporting ScrumyCamp schemas | ||
*/ | ||
|
||
/** | ||
* Implementation of hook_schema(). | ||
*/ | ||
function ambient_sms_gateway_schema() { | ||
$schema['ambient_sms_gateway_outbox'] = array( | ||
'description' => 'Stores information about sent sms messages', | ||
'fields' => array( | ||
'id' => array( | ||
'description' => 'The record ID', | ||
'type' => 'serial', | ||
), | ||
'destination' => array( | ||
'description' => 'The destination number', | ||
'type' => 'text', | ||
), | ||
'message' => array( | ||
'description' => 'The message', | ||
'type' => 'text', | ||
), | ||
'sent_at' => array( | ||
'description' => 'The time the message was sent', | ||
'type' => 'int', | ||
), | ||
'response_code' => array( | ||
'description' => 'The time the message was sent', | ||
'type' => 'int', | ||
), | ||
'response_message' => array( | ||
'description' => 'The message', | ||
'type' => 'text', | ||
), | ||
'response_message_id' => array( | ||
'description' => 'The message id', | ||
'type' => 'int', | ||
), | ||
), | ||
'primary key' => array('id'), | ||
); | ||
|
||
$schema['ambient_sms_gateway_outbox_transaction'] = array( | ||
'description' => 'Stores information about sent sms messages transactions', | ||
'fields' => array( | ||
'id' => array( | ||
'description' => 'The record ID', | ||
'type' => 'int', | ||
), | ||
'request' => array( | ||
'type' => 'text', | ||
), | ||
'response' => array( | ||
'type' => 'text', | ||
), | ||
), | ||
); | ||
|
||
// msisdn=27826941134&shortcode=2782007210000006&keyword=Hi&msg=Hi+dude&message_id=1 | ||
$schema['ambient_sms_gateway_inbox'] = array( | ||
'description' => 'Stores information about received sms messages', | ||
'fields' => array( | ||
'id' => array( | ||
'description' => 'The record ID', | ||
'type' => 'serial', | ||
), | ||
'sender' => array( | ||
'description' => 'The destination number', | ||
'type' => 'text', | ||
), | ||
'destination' => array( | ||
'description' => 'The destination number', | ||
'type' => 'text', | ||
), | ||
'message' => array( | ||
'description' => 'The message', | ||
'type' => 'text', | ||
), | ||
'in_reply_to' => array( | ||
'description' => 'Will be set to an outbox id if this is a reply', | ||
'type' => 'int', | ||
), | ||
'received_at' => array( | ||
'description' => 'The time the message was received', | ||
'type' => 'int', | ||
), | ||
'processed_at' => array( | ||
'description' => 'The time the message was processed', | ||
'type' => 'int', | ||
), | ||
), | ||
'primary key' => array('id'), | ||
); | ||
|
||
$schema['ambient_sms_gateway_inbox_transaction'] = array( | ||
'description' => 'Stores information about received sms messages transactions', | ||
'fields' => array( | ||
'id' => array( | ||
'description' => 'The record ID', | ||
'type' => 'int', | ||
), | ||
'request' => array( | ||
'type' => 'text', | ||
), | ||
'response' => array( | ||
'type' => 'text', | ||
), | ||
), | ||
); | ||
|
||
return $schema; | ||
} | ||
|
||
|
||
/** | ||
* Implementation of hook_install(). | ||
*/ | ||
function ambient_sms_gateway_install() { | ||
drupal_install_schema("ambient_sms_gateway"); | ||
} | ||
|
||
|
||
/** | ||
* Implementation of hook_uninstall(). | ||
*/ | ||
function ambient_sms_gateway_uninstall() { | ||
drupal_uninstall_schema("ambient_sms_gateway"); | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
<?php | ||
// $Id$ | ||
/** | ||
* @file ambient_sms_gateway.module | ||
* Implementation of the Ambient sms gateway | ||
*/ | ||
|
||
require_once("ambient_sms_gateway.outbound.inc"); | ||
require_once("ambient_sms_gateway.inbound.inc"); | ||
|
||
/** | ||
* Implementation of hook_menu(). | ||
*/ | ||
function ambient_sms_gateway_menu() { | ||
// Inbound endpoint for SMS | ||
$items['gateway/ambient/inbound.php'] = array( | ||
'page callback' => 'ambient_sms_gateway_incoming_callback', | ||
'page arguments' => array(), | ||
'access callback' => 'ambient_sms_gateway_incoming_access_callback', | ||
'access arguments' => array(), | ||
'type' => MENU_CALLBACK, | ||
); | ||
|
||
return $items; | ||
} | ||
|
||
|
||
/** | ||
* Implementation of hook_gateway_info(). | ||
*/ | ||
function ambient_sms_gateway_gateway_info() { | ||
return array( | ||
'ambient_sms_gateway' => array( | ||
'name' => 'Ambient Gateway', | ||
'send' => 'ambient_sms_gateway_send', | ||
'receive' => TRUE, | ||
'configure form' => 'ambient_sms_gateway_admin_form', | ||
'send form' => 'ambient_sms_gateway_send_form', | ||
'validate number' => 'ambient_sms_gateway_validate_number', | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Return and administration form for configuring the gateway | ||
*/ | ||
function ambient_sms_gateway_admin_form($configuration) { | ||
$form['ambient_api_key'] = array( | ||
'#type' => 'textfield', | ||
'#title' => t('Ambient API key'), | ||
'#default_value' => $configuration['ambient_api_key'], | ||
'#size' => '50' | ||
); | ||
|
||
$form['ambient_api_password'] = array( | ||
'#type' => 'textfield', | ||
'#title' => t('Ambient API password'), | ||
'#default_value' => $configuration['ambient_api_password'], | ||
'#size' => '50' | ||
); | ||
|
||
$form['ambient_api_sms_url'] = array( | ||
'#type' => 'textfield', | ||
'#title' => t('Ambient API SMS URL'), | ||
'#default_value' => empty($configuration['ambient_api_sms_url']) ? 'https://services.ambientmobile.co.za/sms' : $configuration['ambient_api_sms_url'], | ||
'#size' => '50' | ||
); | ||
|
||
$form['ambient_sms_batch_process'] = array( | ||
'#type' => 'checkbox', | ||
'#title' => t('Batch processing'), | ||
'#description' => t('If this is enabled, SMS messages will be queued and processed at cron time. If it is disabled, and sms is processed immediately.'), | ||
'#default_value' => intval($configuration['ambient_sms_batch_process']), | ||
); | ||
|
||
$form['ambient_inbox_process_per_cron'] = array( | ||
'#type' => 'textfield', | ||
'#title' => t('Process inbox rate'), | ||
'#description' => t('This is how many inbox messages will be processed per cron run. Default: 500'), | ||
'#default_value' => empty($configuration['ambient_inbox_process_per_cron']) ? 500 : empty($configuration['ambient_inbox_process_per_cron']), | ||
'#size' => '50' | ||
); | ||
|
||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* Send method for the ambient gateway | ||
*/ | ||
function ambient_sms_gateway_send($number, $message, $options) { | ||
|
||
$configuration = variable_get('sms_ambient_sms_gateway_settings', array()); | ||
if(! $configuration['ambient_api_key'] || ! $configuration['ambient_api_password'] || ! $configuration['ambient_api_sms_url']) { | ||
drupal_set_message("Please configure the Ambient SMS Gateway module before sending messages.","error"); | ||
return array( | ||
'status' => FALSE, | ||
'message' => 'Please configure the Ambient SMS Gateway module before sending messages.', | ||
'variables' => array(), | ||
); | ||
} | ||
|
||
$configuration['options'] = $options; | ||
$sms = new ambientOutboundSms($number, $message, $configuration); | ||
$sms->send(); | ||
|
||
return array( | ||
'status' => $sms->send_status, | ||
'message' => $sms->send_status_message, | ||
'variables' => array(), | ||
); | ||
} | ||
|
||
/** | ||
* There are no fields added to the send form. | ||
* Seems like the implimentation requires this though | ||
*/ | ||
function ambient_sms_gateway_send_form($required = FALSE) { | ||
$form = array(); | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* Deal with inbound SMS. Check out hook_menu above. | ||
* This function will write the message to an ambeint staging table that is processed | ||
* at cron run. By default 500 messages are processed per cron run | ||
*/ | ||
function ambient_sms_gateway_incoming_callback() { | ||
$XML_RESPONSE = "<xml>OK</xml>"; // We just return OK all the time. | ||
|
||
$sms = ambientInboundSms::parseInbound(); | ||
$sms->transaction->response = $XML_RESPONSE; | ||
|
||
$configuration = variable_get('sms_ambient_sms_gateway_settings', array()); | ||
if($configuration['ambient_sms_batch_process'] > 0) { | ||
$sms->logTransaction(); // This writes to the db for cron processing | ||
} else { | ||
$sms->processed_at = time(); | ||
$sms->logTransaction(); // This writes to the db for cron processing | ||
sms_incoming($sms->sender, $sms->message, array('inbox_data' => $sms)); | ||
} | ||
|
||
print $XML_RESPONSE; | ||
} | ||
|
||
function ambient_sms_gateway_incoming_access_callback() { | ||
|
||
if(!$_POST['msisdn'] || !$_POST['msg']) { return FALSE; } | ||
|
||
//TODO: Do we want to match Ambient IP's here? | ||
return TRUE; | ||
} | ||
|
||
/** | ||
* Implementation of hook_validate_number(). | ||
* For now we are simply allowing all numbers through. We must check | ||
* with Ambient what format the allow | ||
*/ | ||
function ambient_sms_gateway_validate_number($number, $options) { | ||
return TRUE; | ||
} | ||
|
||
/** | ||
* Implementation of hook_cron(). | ||
*/ | ||
function ambient_sms_gateway_cron() { | ||
$configuration = variable_get('sms_ambient_sms_gateway_settings', array()); | ||
|
||
if(intval($configuration['ambient_inbox_process_per_cron'])<=0) { $count = 500; } | ||
else { $count = intval($configuration['ambient_inbox_process_per_cron']); } | ||
|
||
ambient_sms_gateway_process_inbox($count); | ||
} | ||
|
||
/** | ||
* Process the inbox | ||
*/ | ||
function ambient_sms_gateway_process_inbox($max) { | ||
$res = db_query("select * from {ambient_sms_gateway_inbox} where processed_at is NULL or processed_at <= 0 limit %d",intval($max)); | ||
$count = 0; | ||
while($row = db_fetch_object($res)) { | ||
$count++; | ||
db_query("update ambient_sms_gateway_inbox set processed_at=%d where id=%d", time(), $row->id); | ||
sms_incoming($row->sender, $row->message, array('inbox_data' => $row)); | ||
} | ||
|
||
if($count > 0) { | ||
watchdog("Ambient SMS Gateway", "Processed %d inbox messages", array("%d" => $count)); | ||
} | ||
} | ||
|
Oops, something went wrong.