You are here

function twilio_command in Twilio 7

Executes a command using the Twilio REST API.

Parameters

string $command: The Twilio API command to execute.

array $data: The array of data and configuration used by some command being executed.

Return value

bool TRUE if the command executed correctly, FALSE otherwise.

3 calls to twilio_command()
twilio_receive_message in ./twilio.module
Callback for incoming messages.
twilio_receive_voice in ./twilio.module
Callback for incoming voice calls.
twilio_send in ./twilio.module
Sends a message via Twilio.

File

./twilio.module, line 189
Twilio module

Code

function twilio_command($command = '', $data = array()) {

  // Try to load the library and check if that worked.
  if (($library = libraries_load(TWILIO_LIBRARY)) && !empty($library['loaded'])) {

    // Set our account_sid, auth_token, and number.
    $account_sid = !empty($data['options']['twilio_account']) ? $data['options']['twilio_account'] : variable_get('twilio_account', FALSE);
    $auth_token = !empty($data['options']['twilio_token']) ? $data['options']['twilio_token'] : variable_get('twilio_token', FALSE);
    $number = !empty($data['options']['twilio_number']) ? $data['options']['twilio_number'] : variable_get('twilio_number', FALSE);

    // If we don't have one of our twilio variables don't bother doing anything.
    if (!$account_sid || !$auth_token || !$number) {
      return FALSE;
    }

    // Twilio REST API version.
    $api_version = !empty($data['options']['api_version']) ? $data['options']['api_version'] : TWILIO_API_VERSION;
    switch ($api_version) {
      case '2010-04-01':
        switch ($command) {
          case 'sendmsg':

            // Instantiate a new Twilio Rest Client.
            $client = new Services_Twilio($account_sid, $auth_token);
            $sms = array(
              'To' => '+' . $data['country'] . $data['number'],
              'From' => $number,
              'Body' => $data['message'],
            );
            if (!empty($data['media'])) {
              $sms['MediaUrl'] = $data['media'];
            }
            try {
              $response = $client->account->messages
                ->create($sms);
              return TRUE;
            } catch (Exception $e) {
              watchdog('Twilio', $e
                ->getMessage(), array(), WATCHDOG_ERROR);
              $link = l($e
                ->getInfo(), $e
                ->getInfo());
              $message = t('Twilio has returned the error: "@error". For more information visit the following link. !link', array(
                '@error' => $e
                  ->getMessage(),
                '!link' => $link,
              ));
              drupal_set_message($message, 'error');
            }
            if (!empty($response->status) && $response->status == 'failed') {
              watchdog('Twilio', 'An unkown error occured during the HTTP request');
            }
            break;
          case 'validate':
            $validator = new Services_Twilio_RequestValidator($auth_token);
            $type = !empty($data['type']) ? $data['type'] : 'sms';
            $url = $GLOBALS['base_url'] . '/twilio/' . $type;
            $signature = $_SERVER["HTTP_X_TWILIO_SIGNATURE"];
            $post_vars = $_POST;
            if ($validator
              ->validate($signature, $url, $post_vars)) {
              watchdog('Twilio', 'Incoming SMS message validated');
              return TRUE;
            }
            else {
              watchdog('Twilio', 'Incoming SMS could not be validated');
            }
            break;
        }
        break;
      case '2008-08-01':
        switch ($command) {
          case 'sendmsg':

            // Instantiate a new Twilio Rest Client.
            $client = new TwilioRestClient($account_sid, $auth_token);
            try {
              $response = $client
                ->request("/{$api_version}/Accounts/{$account_sid}/SMS/Messages", "POST", array(
                "To" => '+' . $data['country'] . $data['number'],
                "From" => $number,
                "Body" => $data['message'],
              ));
              return TRUE;
            } catch (Exception $e) {
              watchdog('Twilio', $e, array(), WATCHDOG_ERROR);
            }
            if ($response->IsError) {
              watchdog('Twilio', 'An error occured during the HTTP request: @error', array(
                '@error' => $response->ErrorMessage,
              ));
            }
            break;
        }
        break;
    }
  }
  else {
    watchdog('Twilio', 'The twilio library was not loaded properly');
  }
  return FALSE;
}