Submitted by webmaster on December 16, 2015

Why integrate your website with SMS?

There are a lot of reasons why more sites are moving to integrate with SMS.  The chief among them being added security and identification of its users.  Steam even recently started requesting customers to sign up and link their accounts to their mobile numbers in order to prevent a large amount of hacking that is currently happening.  Connecting a website user's account to a physical device like a cell phone is a great way to ensure that the person accessing the website is the same one using the cell phone and this also allows a website to be surer that the account is still secure even if it is being accessed from an unusual location.

For some smaller websites, security like this may not seem like an important thing. However, consider some of the recent attacks on websites where users personal information, credit information, and site activity was exposed and had lead to several suicides then security like this seems more in line with what we are talking about.  Many site owners think that this may be expensive or they don't even have the ability to grasp how this sort of integration would work, so that is the basis for today's blog post.

SMS Framework

Luckily if you have a Drupal-based website then a large part of the work is done already.  There is a great API module already available that can integrate your website with an SMS gateway provider.  Think of a gateway provider like your credit card processor.  It is a third-party service that you pay a subscription to in order to gain access to their resources that enable SMS messages transmitting to cell phones.  In the past, this was tricky because you had to create code that worked directly with each cell phone company you wished to send SMS messages to.  However today with these gateways it is a lot simpler to accomplish.

While this module provides a lot of functionality and has a long list of SMS gateways, it may not have the gateway you need or want to use.  You also may be in a situation like being in a country where there are not a lot of options to choose from, and none of the options are available out of the box from the SMS Framework module.  Luckily, we can create our own SMS gateway module that will work with the SMS Framework API in order to connect to these other gateways.  The process of writing such a module is pretty easy and I will go over it below.

Defining your SMS Gateway

You will want to create a new Drupal module for any SMS gateway's not listed.  To first tell the SMS Framework of your new gateway you will need to create a hook_gateway_info function in your new module.  This will return the details and callbacks used in order to configure and send SMS messages.  Additionally, if you want to configure your site to be able to receive SMS alerts when you could do that as well.  I will talk about that further down in more detail.

/** * Implement hook_gateway_info */ function sms_fowiz_gateway_info() { return array( 'sms_fowiz' => array( 'name' => 'FOWiz Gateway', 'send' => 'sms_fowiz_send', 'receive' => TRUE, 'configure form' => 'sms_fowiz_settings', ) ); }

In this case, I am defining a new SMS Gateway so that I can send SMS messages in the Philippines using http://www.fowiz.com/, their service is relatively simplistic in its implementation, but allows for the full standard functionality that we are looking for.  In addition, you can utilize your Cell phone's unlimited SMS plan instead of being charged.  So in this function we are providing the name, a send callback to use when the system wants to send an SMS message, we are letting the SMS Framework know that our gateway can send messages to the site, and we finally define a configuration form that will be used by the admin users in order to enter the account information for their account at FOWiz.

Sending a SMS Message with the SMS Framework

Now that we have let the SMS Framework know about the new gateway we need to create a function for sending the message via that API.  This will be different for each gateway, so you will need to reference the documentation for the provider you will be using.  For FOWiz it is a simple curl post to the url that will enable sending the SMS via their API.

/** * SMS Send Function */ function sms_fowiz_send($number, $message, $options) { $gateway = sms_gateways('gateway', 'sms_fowiz'); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://cloud.fowiz.com/api/message_http_api.php"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query( array( 'username' => $gateway['configuration']['sms_fowiz_user'], 'phonenumber' => $number, 'message' => $message, 'passcode' => $gateway['configuration']['sms_fowiz_pass'], ), '', '&' ) ); $result = curl_exec($curl); $error = curl_error($curl); curl_close($curl); return array( 'status' => (strpos($result, 'Bad Request!') t('Cound not send message to %number.'), 'variables' => array('%number', $number) ); }

For this gateway it only returns a Bad Request! plain text response when there is a issue sending the sms, and does not seem to provide better details on the error.  Additionally, the documentation for this gateway is lacking in how it responds to various errors, so it will be something that will need to evolve as we expereince the error.  For now you can see we are recieving the number, message, and an optional options array passed into our function. We load up the configuration for our gateway which includes the username and passcode required to send SMS through FOWiz.

Receiving SMS Messages using the SMS Framework

In addition to sending messages, we can also create a menu callback that will allow providers to post messages to our site.  There are a number of ways this can be handy for your site.  Again with this particular provider we get very simple feedback, we will get the data in the form of a $_POST and it will have the number, message, and time it was received. We then pass this through to the SMS Framework which handles alerting the rest of the site.

function sms_fowiz_recieve() { sms_incoming($_POST['phone_number'], $_POST['message'], array('sent' => $_POST['timestamp'])); }

Really there is a not to it, you then have the ability to plug in rules or other contrib modules to build more extensions onto the system.