Our Blog

Simple REST API SMS App Integration with Telecom Provider

I have been building apps recently that integrate a REST API which subscribes users to a Drupal web app via SMS, they are simultaneously subscribed in the telecom operators database. Conditions must be checked to keep the users status in sync with Drupal and the operator. Other conditions that I won’t cover here include recurring billing or the free trial period.

This is a practical example of how using the REST protocol allows technologies that are completely different to communicate with each other. The technologies in our stack include SMS, mobile billing and a Drupal web app.

First create the URL endpoint with a callback function by using hook_menu

function my_module_menu() {
$items['my_url/send'] = array(
'title' => 'send',
'page callback' => 'my_module_send_page',
'access callback' => TRUE
);
return $items;
}

When a mobile originated text message “MO” is sent from the customer handset to the shortcode created for our app, a relay message is sent from the telecom integrator’s API and hits the endpoint created with hook_menu on our web app. We then use a GET request to get the values from the values posted to our endpoint url. Parameters include product identification information, partner ID, customer MSISDN and subscription method, as well as other info. We also run an additional API call to check if the MSISDN/phone number is valid and if there is enough money on the customer account.

Here we have the beginning of the callback function that runs when our endpoint is hit.

function my_module_send_page() {

if (isset($_GET[‘MyVar4’])) {
$mobile = $_GET[‘MyVar3’];
$PricePointId = $_GET[‘MyVar2’];
$prodId = $_GET[‘MyVar’];
$mtmo = time() . ‘MO’;

There are many conditions in the business requirements, which determine different messages in the $text variable to the customer. Success/failure, weekly/monthly, arabic/english, below is the case if success and weekly PricePointId is selected. There is also a check for what language is selected, here we use the language value in the user object, we get this from what language the user selects on the registration form. We then pass back the correct language in the $text variable in a post request using cURL with everything else. Here is some more of the callback function.

if ($PricePointId == XXXXXX) {
if($user->language == 'en') {
$text = 'You have successfully subscribed
}
else {
$text = 'تمّ إشتراكك بملهمتي بنجاح بسعر ‘
}
}

$data = array(
‘Password’ => ‘test’,
‘ProductId’ => $prodId,
‘PricePointId’ => $PricePointId,
‘Destination’ => $mobile,
“Text”=> $text
);

$qry_str = drupal_http_build_query($data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, ‘http://integrator-api-endpoint/mystring?’ . (string) $qry_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

Here we get the result from the cURL post to the telecom API.
If the result is greater than one that is success, so we create the user in Drupal.

if($result > 0) {
$obj->field_product = $prodId;
$obj->save(); */
$smart_db->changeUserRoles($user->uid , 'subscribe');
$smart_db->changeplan($user->uid, $prodId, $free = TRUE);
$to=$user->mail;
drupal_mail('user', 'register_no_approval_required', $to, user_preferred_language($user, $default = NULL), array('account' => $user), variable_get('site_mail', ''));
}
}

Comments ( 3 )

The comments are now closed.