Processing Incoming Messages
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
Gateways that support two-way messaging can pass incoming messages to the SMS Framework where other modules can process them using
hook_sms_incoming($op, $number, $message, $options)
.
- $op has three possible values: 'pre process', 'process', and 'post process'
- $number is the number from which the message originated.
- $message is the text of the message.
- $options is an optional array of extended attributes for the message.
Here is an example from the SMS User module which logs a user in based on their mobile number:
function sms_user_sms_incoming($op, $number, $message, $options) {
switch ($op) {
case 'pre process':
sms_user_auth($number);
break;
case 'post process':
sms_user_logout();
break;
}
}
Message parsing should occur during the 'process' $op in most cases. Here is an example of how regular expressions can be used to parse incoming messages. This code will create a new node from incoming message.
An example message would look like this:
tags:23,35 This is the text of the message
A node will be created with the text and tagged with taxonomy terms 23 and 35.
function mymodule_sms_incoming($op, $number, $message, $options) {
global $user;
if ($op == 'process') {
$message = strtolower($message);
$matches = array();
if (preg_match('/tags:([\d,]+?)+ (.+)/', $message, $matches)) {
$terms = explode(',', $matches[1]);
foreach ($terms as $tid) {
if ($term = taxonomy_get_term($tid)) {
$taxonomy[$tid] = $term;
}
}
$node = new StdClass();
$node->title = truncate_utf8($message, 32, TRUE) . '...';
$node->body = $matches[2];
$node->teaser = node_teaser($node->body, isset($node->format) ? $node->format : NULL);
$node->type = 'story';
$node->taxonomy = $taxonomy;
node_save($node);
}
}
}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion