You are here

function sms_twilio_command in Twilio SMS Integration 7

Same name and namespace in other branches
  1. 6 sms_twilio.module \sms_twilio_command()
  2. 7.2 sms_twilio.module \sms_twilio_command()

Executes a command using the Twilio API

1 call to sms_twilio_command()
sms_twilio_send in ./sms_twilio.module
Callback for sending messages.

File

./sms_twilio.module, line 110
Adds support for sending SMS messages using the Twilio gateway.

Code

function sms_twilio_command($command = 'auth', $data = array(), $config = NULL, $account = '') {
  if (!isset($config)) {
    $gateway = sms_gateways('gateway', 'twilio');
    $config = $gateway['configuration'];
  }

  // Twilio REST API version
  $ApiVersion = isset($config['sms_twilio_api']) && $config['sms_twilio_api'] ? $config['sms_twilio_api'] : DEFAULT_TWILIO_API_VERSION;

  // Set our AccountSid and AuthToken
  $AccountSid = $config['sms_twilio_api_sid'];
  $AuthToken = $config['sms_twilio_api_auth_token'];

  // Instantiate a new Twilio Rest Client
  switch ($ApiVersion) {
    case '2010-04-01':

      // Include the PHP TwilioRest library
      require_once DRUPAL_ROOT . '/' . $config['sms_twilio_path'] . '/Services/Twilio.php';
      $client = new Services_Twilio($AccountSid, $AuthToken);
      switch ($command) {
        case 'sendmsg':
          $response = $client->account->messages
            ->sendMessage($config['sms_twilio_number'], $data['number'], $data['message']);
          break;
      }
      break;
    case '2008-08-01':

      // Include the PHP TwilioRest library
      require_once DRUPAL_ROOT . '/' . $config['sms_twilio_path'] . '/twilio.php';
      $client = new TwilioRestClient($AccountSid, $AuthToken);
      switch ($command) {
        case 'sendmsg':
          $response = $client
            ->request("/{$ApiVersion}/Accounts/{$AccountSid}/SMS/Messages", "POST", array(
            "To" => $data['number'],
            "From" => $config['sms_twilio_number'],
            "Body" => $data['message'],
          ));
          break;
      }
      break;
    default:
      $result = array(
        'status' => FALSE,
        'message' => t('Invalid Twilio API: @api', array(
          '@api',
          $ApiVersion,
        )),
      );
      return $result;
  }
  watchdog('sms_twilio', print_r($response, TRUE));

  // Check for HTTP errors
  if ($response->IsError) {
    $result = array(
      'status' => FALSE,
      'message' => t('An error occured during the HTTP request: @error', array(
        '@error' => $response->ErrorMessage,
      )),
    );
  }
  else {
    $result = array(
      'status' => TRUE,
      'data' => t('Message sent to @number', array(
        '@number' => $data['number'],
      )),
    );
  }
  return $result;
}