You are here

function sms_twilio_command in Twilio SMS Integration 6

Same name and namespace in other branches
  1. 7.2 sms_twilio.module \sms_twilio_command()
  2. 7 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 100
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'];
  }

  // Include the PHP TwilioRest library
  require_once $config['sms_twilio_path'] . '/twilio.php';

  // Twilio REST API version
  $ApiVersion = "2008-08-01";

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

  // Instantiate a new Twilio Rest Client
  $client = new TwilioRestClient($AccountSid, $AuthToken);
  switch ($command) {
    case 'sendmsg':

      // Check if the message requires unicode handling
      $response = $client
        ->request("/{$ApiVersion}/Accounts/{$AccountSid}/SMS/Messages", "POST", array(
        "To" => $data['number'],
        "From" => $config['sms_twilio_number'],
        "Body" => $data['message'],
      ));
      break;
  }
  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;
}