You are here

function sms_clickatell_command in SMS Framework 6

Same name and namespace in other branches
  1. 5 modules/sms_clickatell/sms_clickatell.module \sms_clickatell_command()

Executes a command using the Clickatell API

data array fields: number - MSISDN of message recipient. Purely numeric and must begin with intl prefix, eg. 4477121231234. message - Message text. Max 459 chars (3x SMS). Use %n for newline. options - Array of additional options, as below.

data['options'] array fields: sender - Optional: Sender ID may be an MSISDN (max 16 chars) or an alphanumeric string (max 11 chars). See note about Approved Sender IDs in the header of this file. Clickatell param: 'from' reference - Optional: Reference tag to apply to message. Will appear on any receipt. No spaces. Clickatell param: 'cliMsgId' delaymins - Optional: Delay message sending by N minutes. Clickatell param: 'deliv_time' expiremins - Optional: The message send will abort if not sent within N minutes. Clickatell param: 'validity' priority - Optional: Queue priority to apply to the message. Can be 1, 2 or 3, where 1 is high priority. Clickatell param: 'queue' expectreply - Optional: Route the message properly so that the user can reply. Clickatell param: 'mo'

Parameters

$command: One of 'auth', 'sendmsg' or 'getbalance'.

$data: All data required to perform the command.

$config: Gateway configuration parameters.

Return value

Response from command.

4 calls to sms_clickatell_command()
sms_clickatell_admin_form_validate in modules/sms_clickatell/sms_clickatell.module
sms_clickatell_balance in modules/sms_clickatell/sms_clickatell.module
Get account balance
sms_clickatell_get_session_id in modules/sms_clickatell/sms_clickatell.module
Get a new or existing Clickatell session ID
sms_clickatell_send in modules/sms_clickatell/sms_clickatell.module
Callback for sending messages.

File

modules/sms_clickatell/sms_clickatell.module, line 234
Clickatell gateway module for Drupal SMS Framework. Outbound+Inbound+Receipts

Code

function sms_clickatell_command($command = 'auth', $data = array(), $config = NULL) {
  $gateway = sms_gateways('gateway', 'clickatell');
  if ($config == NULL) {
    $config = $gateway['configuration'];
  }
  if ($config['sms_clickatell_ssl']) {
    $scheme = 'https';
  }
  else {
    $scheme = 'http';
  }
  switch ($command) {
    case 'auth':
      $query = 'api_id=' . $config['sms_clickatell_api_id'] . '&user=' . $config['sms_clickatell_user'] . '&password=' . $config['sms_clickatell_password'];
      break;
    case 'sendmsg':

      // Check if the message requires unicode handling
      if ($unicode_message = sms_clickatell_unicode($data['message'])) {
        $message = $unicode_message;
      }
      else {
        $message = drupal_urlencode($data['message']);
      }
      $query = 'session_id=' . sms_clickatell_get_session_id() . '&to=' . $data['number'] . '&text=' . $message;

      // Check if the message requires concatenation (long messages)
      // Note: concatenation over multiple messages reduces each SMS message length by 7 chars.
      $concat = 1;
      if (strlen($message) > 160) {
        $concat = 2;
        if (strlen($message) > 306) {
          $concat = 3;
        }
      }
      $query .= '&concat=' . $concat;

      // Add any optional arguments
      if (isset($data) && array_key_exists('options', $data)) {

        // sender (Clickatell: from)
        if (array_key_exists('sender', $data['options'])) {
          $query .= '&from=' . $data['options']['sender'];
          $sender_set = TRUE;
        }

        // delaymins (Clickatell: deliv_time)
        if (array_key_exists('delaymins', $data['options']) && $data['options']['delaymins'] >= 10 && $data['options']['delaymins'] <= 10080) {
          $query .= '&deliv_time=' . $data['options']['delaymins'];
        }

        // priority (Clickatell: queue)
        if (array_key_exists('priority', $data['options']) && $data['options']['priority'] >= 1 && $data['options']['priority'] <= 3) {
          $query .= '&queue=' . $data['options']['priority'];
        }

        // expiremins (Clickatell: validity)
        if (array_key_exists('expiremins', $data['options']) && $data['options']['expiremins'] >= 1 && $data['options']['expiremins'] <= 1440) {
          $query .= '&validity=' . $data['options']['expiremins'];
        }

        // reference (Clickatell: cliMsgId)
        if (array_key_exists('reference', $data['options']) && strlen($data['options']['reference']) <= 32) {
          $query .= '&cliMsgId=' . $data['options']['reference'];
        }

        // expectreply (Clickatell: mo)
        if (array_key_exists('expectreply', $data['options'])) {
          $query .= '&mo=' . $data['options']['expectreply'];
        }
      }

      // If sender is not set and default sender exists, then apply default sender
      if (!isset($sender_set) && $config['sms_clickatell_from']) {
        $query .= '&from=' . $config['sms_clickatell_from'];
      }

      // Apply callback parameter if set
      if ($config['sms_clickatell_callback']) {
        $query .= '&callback=' . $config['sms_clickatell_callback'];
      }
      break;
    case 'getbalance':
      $query = 'session_id=' . sms_clickatell_get_session_id();
      break;
  }

  // Run the command
  $http_result = drupal_http_request($scheme . '://api.clickatell.com/http/' . $command . '?' . $query);

  // Check for HTTP errors
  if ($http_result->error) {
    return array(
      'status' => FALSE,
      'message' => t('An error occured during the HTTP request: @error', array(
        '@error' => $http_result->error,
      )),
    );
  }
  if ($http_result->data) {

    // Check for Clickatell errors
    if (preg_match('/^ERR: ([\\d]+), (([\\w]|[\\s])+)$/', $http_result->data, $matches)) {
      $errorcode = $matches[1];
      $errortext = $matches[2];
      $result = array(
        'status' => FALSE,
        'status_code' => sms_clickatell_map_response_code($errorcode),
        'gateway_status_code' => $errorcode,
        'gateway_status_text' => $errortext,
      );
    }
    elseif ($command == 'auth') {

      // Add Clickatell session ID to result array.
      list($status, $sid) = explode(': ', $http_result->data);
      $result = array(
        'status' => TRUE,
        'status_code' => SMS_GW_OK,
        'sid' => $sid,
      );
    }
    elseif ($command == 'getbalance') {

      // Add Clickatell credit balance to result array.
      preg_match('/Credit:[\\s]+([\\d]+\\.*[\\d]+)$/', $http_result->data, $matches);
      $result = array(
        'status' => TRUE,
        'status_code' => SMS_GW_OK,
        'gateway_status_text' => $matches[1],
      );
    }
    else {

      // Return a good response array
      $result = array(
        'status' => TRUE,
        'status_code' => SMS_GW_OK,
      );
    }
  }
  return $result;
}