function engagements_send_sms_to_number

6.x engagements.module engagements_send_sms_to_number($to_number, $body, $to_cwid = "", $from_number = "default", $bool_send_opt_out_message = TRUE)

Actually send a text message. Will drupal_set_message() as an error if there is a problem, and return FALSE. Returns TRUE on success, no drupal_set_message is printed.

5 calls to engagements_send_sms_to_number()
engagements_handle_sms_stop in modules/engagements/engagements.module
This function is called by engagements_handle_incoming_sms, when we receive 'STOP' from the user. We must add them to our "sms_do_not_txt" table, and send a reply letting them know how to re-subscribe in the future.
engagements_handle_sms_unstop in modules/engagements/engagements.module
User opted-IN to receiving txt messages.
engagements_mass_sms_perform_batch_operation in modules/engagements/engagements.module
engagements_send_email_or_txt_form_submit in modules/engagements/engagements.module
notify_by_sms in modules/notify/notify.module

File

modules/engagements/engagements.module, line 3237
This is the primary module file for the engagements module.

Code

function engagements_send_sms_to_number($to_number, $body, $to_cwid = "", $from_number = "default", $bool_send_opt_out_message = TRUE) 
 {

  $to_number = engagements_convert_to_valid_phone_number($to_number);

  require_once (fp_get_module_path('engagements', TRUE, FALSE) . '/lib/signalwire/vendor/autoload.php');

  //use SignalWire\Rest\Client;

  // project id, auth tolen, space url
  $project_id = variable_get('sms_project_id', '');
  $auth_token = variable_get('sms_auth_token', '');
  $space_url = variable_get('sms_space_url', '');

  $from_phone = $from_number;
  $phones = engagements_get_from_phones();
  if ($from_phone == "default") {
    $from_phone = engagements_convert_to_valid_phone_number($phones ['default']['num']);
  }

  if ($bool_send_opt_out_message) {
    // We must include a "text STOP to opt-out" type message to the bottom of every notification.
    // TODO:  May be a setting instead, on the signalwire settings page
    $body .= "\n\n" . "Text STOP to opt-out of txt messages.";
  }




  // project id, auth tolen, space url
  $client = new SignalWire\Rest\Client($project_id, $auth_token, array("signalwireSpaceUrl" => $space_url));
  $external_msg_id = FALSE;

  try {

    $message = $client->messages
      ->create(
    "+1" . $to_number, // to
    array(
      "from" => "+1" . $from_phone, // my signalwire account phone number 
      "body" => $body
    )
    );

    $external_msg_id = $message->sid;

    // save message information to our sms_history table.

    $record = $message;

    $message_sid = $record->sid;

    $from_mobile = engagements_convert_to_valid_phone_number($record->from);
    $body = $record->body;
    $num_segments = intval($record->numSegments);

    $date_sent = $record->dateCreated; // Since we just created it, just dateCreated instead of dateSent.    
    $throw_away = print_r($date_sent, TRUE); // Not sure why, but I have to do this in order for the data field to populate.

    //$date_sent_ts = strtotime($date_sent->date);  // Was causing a mysql error all of a sudden.  Just use current time.
    $date_sent_ts = time();

    // The price is not available right now, so we will set it...
    $price = 0.0013 * $num_segments;
    //$fp_price = engagements_get_fp_price($price);
    $fp_price = 0.0033 * $num_segments;


    $direction = $record->direction;

    // Write our data to our sms_history table.
    db_query(
    "INSERT INTO sms_history (`message_sid`, `sw_type`, `body`, `from_number`, `to_number`, `sw_price`, `fp_price`, `to_cwid`, `updated`, `direction`, `media_filenames`, `date_sent`, price_processed, num_segments)
              VALUES (?, 'sms', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ", 
    array($message_sid, $body, $from_mobile, $to_number, $price, $fp_price, $to_cwid, time(), $direction, '', $date_sent_ts, 0, $num_segments)
    );
  }
  catch (Exception $e) {
    fp_add_message("An error has occured while trying to send a text message to <em>$to_number</em>.  The
            error has been logged.  Please have the technical administrator investigate.  Error message:" . $e->getMessage(), "error");
    watchdog("engagements_sms", "An error has occured while trying to send a text message to <em>$to_number</em>.  Error: " . $e->getMessage() . ".  The complete
            error is: <pre>" . print_r($e, true) . "</pre>", array(), WATCHDOG_ERROR);
    return FALSE;
  }

  watchdog("engagements_sms", "SMS message sent successfully to $to_number. External_msg_id = " . $external_msg_id);

  return $external_msg_id;
}