function send_message($from, $to, $message) { $from = PhoneNumber::normalizePhoneNumberToE164($from); $to = PhoneNumber::normalizePhoneNumberToE164($to); try { $account = OpenVBX::getAccount(); $response = $account->messages->sendMessage($from, $to, $message); } catch (Exception $e) { throw new VBX_Sms_messageException($e->getMessage()); } if (!in_array($response->status, array('sent', 'queued'))) { throw new VBX_Sms_messageException('SMS delivery failed. An unknown error occurred' . ' during delivery.'); } }
function send_message($from, $to, $message) { $from = PhoneNumber::normalizePhoneNumberToE164($from); $to = PhoneNumber::normalizePhoneNumberToE164($to); $twilio = new TwilioRestClient($this->twilio_sid, $this->twilio_token, $this->twilio_endpoint); error_log("Sending sms from {$from} to {$to} with content: {$message}"); $response = $twilio->request("Accounts/{$this->twilio_sid}/SMS/Messages", 'POST', array("From" => $from, "To" => $to, "Body" => $message)); $status = isset($response->ResponseXml) ? $response->ResponseXml->SMSMessage->Status : 'failed'; if ($response->IsError || $status != 'sent' && $status != 'queued') { error_log("SMS not sent - Error Occurred"); error_log($response->ErrorMessage); throw new VBX_Sms_messageException($response->ErrorMessage); } }
function make_call_path($to, $callerid, $path, $rest_access) { $twilio = new TwilioRestClient($this->twilio_sid, $this->twilio_token); $recording_url = site_url("twiml/redirect/{$path}/{$rest_access}"); $response = $twilio->request("Accounts/{$this->twilio_sid}/Calls", 'POST', array("Caller" => PhoneNumber::normalizePhoneNumberToE164($callerid), "Called" => PhoneNumber::normalizePhoneNumberToE164($to), "Url" => $recording_url)); if ($response->IsError) { error_log($from); error_log(var_export($response, true)); throw new VBX_CallException($response->ErrorMessage); } }
/** * Start an outbound call * * @param string $from - the user making the call, this is the device that'll be called first * @param string $to - the call destination * @param string $callerid - the number to use as the caller id * @param string $rest_access - token to authenticate the twiml request * @return void */ public function make_call($from, $to, $callerid, $rest_access) { try { PhoneNumber::validatePhoneNumber($from); // handle being passed an email address for calls to browser clients if (!filter_var($to, FILTER_VALIDATE_EMAIL)) { PhoneNumber::validatePhoneNumber($to); } } catch (PhoneNumberException $e) { throw new VBX_CallException($e->getMessage()); } // don't normalize email addresses that are used to identify browser clients if (!filter_var($to, FILTER_VALIDATE_EMAIL)) { $to = PhoneNumber::normalizePhoneNumberToE164($to); } $callerid = PhoneNumber::normalizePhoneNumberToE164($callerid); $from = PhoneNumber::normalizePhoneNumberToE164($from); $twiml_url = site_url("twiml/dial") . '?' . http_build_query(compact('callerid', 'to', 'rest_access')); try { $account = OpenVBX::getAccount(); $account->calls->create($callerid, $from, $twiml_url); } catch (Exception $e) { throw new VBX_CallException($e->getMessage()); } }