Webforms and SMS Integration

The SMS module from D6 hasn't been ported yet and according to the things I've rea recently, it might be sometime before it is. Apparently there are many changes that people are asking for and so it might be sometime before the port arrives, if ever, see: http://drupal.org/node/795202

In the meantime, I *used* to have a D6 website with a feature whereby people could text me from the site. That was D6. As we are all starting to lear, D7 is significantly different and so I wanted to recreate the functionality with as little coding as possible, just enough to be able to get the feature working. Isn't that what we mostly want anyway ?

Another change that I've discovered is that the webform module no longer allows you to add PHP as explained here: http://drupal.org/project/webform_php

Minimal Module!

So, given that I want to use a webform as that's quick to setup, and given that I have to write a module, what's the minimal code required to make it happen ? The first thing you need for any D7 module is of course the module info file, which has also changed structure (is there anything that hasn't it seems!) and so here's mine:

name = OBJITSU
description = Custom code for my site
package = "OBJITSU"
core = 7.x

files[] = objitsu.module

This tells Drupal that we have a single file and no configuration callback. So this module is going to be pretty simple, which after all is the main requirement for me to ever write any code in PHP these days. Blame learning Haskell!

The Module

Having told Drupal we have a file we want sucked in, let's take a look at what was required to intercept the webform module and then to add submit handler to it and finally, to process the message from the input box (form id is 'message' by the way, from my webform configuration) and to send it to my SMS provider, BulkSMS.co.uk. I've used them for a long time and they are very very good with excellent support and no I have no connection with them other than being a very happy customer.

function objitsu_form_alter(&$form, &$form_state, $form_id)
{
    switch ($form_id)
    {
        case 'webform_client_form_3':
            $form['#submit'][] = '_objitsu_sms_intercept_submit';
            break;
    }
}

This adds another submission handler to the form. The name was worked out by simply putting a drupal_set_message() call into the hook and then cutting and pasting the name into emacs from the browser page. How hard can that be! Having now learned the name of the beast we can control it. Line 6 is where we append the name of the function we want added to the submit handlers called. I have seen a lot of people hang themselves here by forgetting the '[]' and thus killing anything that might already have been put in place. Always append, and remember one of lifes great programming rules: He who writes last, writes best.

Processing the Message

Now lets look at the implementation of that function, _objitsu_sms_intercept_submit, as this is where we perform a quick check to make sure that the message is not blank, no point in wasting credits on empty strings now is there, and if that is ok we can send the message.

function _objitsu_sms_intercept_submit($f, $fs)
{
    $sms = trim($fs['input']['submitted']['message']);
    if (strlen($sms))
        {
        $url  = 'http://www.bulksms.co.uk/eapi/submission/send_sms/2/2.0';
    	$data =
    	    'username=USERNAME&password=PASSWORD&message='
    	    .urlencode($sms)
    	    .'&msisdn=447943826075';

	    $response = _objitsu_do_post_request
	        ($url,
	         $data,
	         "Content-type: application/x-www-form-urlencoded");

        drupal_set_message($response);
        }
}

The function that is called to send, _objitsu_sms_intercept_submitis an adaptation of the sample PHP code provided by BulkSMS with one addition, I added the Content-type header to the call as PHP now generates a warning about assuming the type to be "www/url-encoded" which totaly naffs up my nice web site! LOL. So I just fixed that, the rest of the code ...

function _objitsu_do_post_request($url, $data, $optional_headers = null)
{
    $params = array(
        'http'=> array(
	        'method'  => 'POST',
	        'content' => $data));

    if ($optional_headers !== null) {
    	$params['http']['header'] = $optional_headers;
    }

    $ctx = stream_context_create($params);
    $fp  = fope n($url, 'rb', false, $ctx);
    fpassthr u($fp);

    if (!$fp) {
    	drupal_set_message("Problem with $url, Cannot connect");
    }
    $response = @stream_get_contents($fp);
    if ($response === false) {
	    drupal_set_message("Problem reading data from $url, No status returned");
    }

    return $response;
}

... is down to BulkSMS and works just fine. Note that values USERNAME and PASSWORD are obviously not my real details as I've no wish to get ripped off by script-kiddies the world over.

I also have made the webform send me the IP address of the sender in the email that backs it up, this means that if I should run out of credits, the message gets through to my email account which... I get on my phone! Ha HA! Inscrutable isn't it!

I hope this is useful to somebody looking for a simple way to send SMS from Drupal 7.

:)
Emacs The Viking

Content Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <pre> <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or class="OPTIONS" [title="the title"].
  • Lines and paragraphs break automatically.

Full HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or class="OPTIONS" [title="the title"].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.