-
Notifications
You must be signed in to change notification settings - Fork 773
RING Central SMS Integration #12504
base: developer
Are you sure you want to change the base?
RING Central SMS Integration #12504
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,157 @@ | ||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * RINGCENTRAL - sms provider. | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| class SMSNotifier_RingCentral_Provider extends SMSNotifier_Basic_Provider | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Provider name. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @var string | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| protected $name = 'RingCentral'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Address URL. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @var string | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| protected $url = 'https://platform.ringcentral.com'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Encoding. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @var string | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| public $encoding = 'utf-8'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Format. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @var string | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| public $format = 'json'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Required fields. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @return string[] | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| public function getRequiredParams() | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| return ['CLIENT_ID','RINGUSER','RINGPASS','RINGPHONE','RINGEXT']; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public function get($key) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| return $this->$key; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Response. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @param Requests_Response $request | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @return bool | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| public function getResponse(Requests_Response $request) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| $response = \App\Json::decode($request->body); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return isset($response['error']) && !empty($response['error']) ? false : true; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Fields to edit in settings. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @return \Settings_Vtiger_Field_Model[] | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| public function getSettingsEditFieldsModel() | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| $fields = []; | ||||||||||||||||||||||||||||||
| $moduleName = 'Settings:SMSNotifier'; | ||||||||||||||||||||||||||||||
| foreach ($this->getRequiredParams() as $name) { | ||||||||||||||||||||||||||||||
| $field = ['uitype' => 1, 'column' => $name, 'name' => $name, 'displaytype' => 1, 'typeofdata' => 'V~M', 'presence' => 0, 'isEditableReadOnly' => false]; | ||||||||||||||||||||||||||||||
| if ($name === 'CLIENT_ID') { | ||||||||||||||||||||||||||||||
| $field['text'] = ['']; | ||||||||||||||||||||||||||||||
| $field['label'] = 'CLIENT_ID'; | ||||||||||||||||||||||||||||||
| $fields[] = $field; | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if ($name === 'RINGUSER') { | ||||||||||||||||||||||||||||||
| $field['text'] = ['']; | ||||||||||||||||||||||||||||||
| $field['label'] = 'RINGUSER'; | ||||||||||||||||||||||||||||||
| $fields[] = $field; | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if ($name === 'RINGPASS') { | ||||||||||||||||||||||||||||||
| $field['text'] = ['']; | ||||||||||||||||||||||||||||||
| $field['label'] = 'RINGPASS'; | ||||||||||||||||||||||||||||||
| $fields[] = $field; | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if ($name === 'RINGPHONE') { | ||||||||||||||||||||||||||||||
| $field['text'] = ['']; | ||||||||||||||||||||||||||||||
| $field['label'] = 'RINGPHONE'; | ||||||||||||||||||||||||||||||
| $fields[] = $field; | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if ($name === 'RINGEXT') { | ||||||||||||||||||||||||||||||
| $field['text'] = ['']; | ||||||||||||||||||||||||||||||
| $field['label'] = 'RINGEXT'; | ||||||||||||||||||||||||||||||
| $fields[] = $field; | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| foreach ($fields as &$field) { | ||||||||||||||||||||||||||||||
| $field = Settings_Vtiger_Field_Model::init($moduleName, $field); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return $fields; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public function getPatch() | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| $keys = $this->getRequiredParams(); | ||||||||||||||||||||||||||||||
| $keys[] = $this->toName; | ||||||||||||||||||||||||||||||
| $keys[] = $this->messageName; | ||||||||||||||||||||||||||||||
| $params = []; | ||||||||||||||||||||||||||||||
| foreach ($keys as $key) { | ||||||||||||||||||||||||||||||
| $params[$key] = $this->get($key); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return $params; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public function send() { | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| $url = $this->getUrl(); | ||||||||||||||||||||||||||||||
| $cliend_secret = $this->getAuthorization(); | ||||||||||||||||||||||||||||||
| $patch = $this->getPatch(); | ||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||
| $login = new RingCentral\SDK\SDK($patch['CLIENT_ID'], $cliend_secret, $url); | ||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What class is this?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't merge your commits because an additional library in the system is required, which in most cases won't be used. If you rewrite the communication code to "Guzzle, PHP HTTP client" (here is an example https://github.com/YetiForceCompany/YetiForceCRM/search?q=GuzzleHttp) we will merge your commits.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I won't be doing it, as it works just fine in the environment. What is the problem with Ringcentral SDK? It is an official well-maintained library. https://github.com/ringcentral/ringcentral-php you can close the request if you don't see potential in this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not too sure about guzzle but I do have sms, call logs, and call recordings working in production. Full pbx and mms integration is feasible and I would be willing to create a pull request for complete RingCentral / Telus Business Connect support; though I am not sure what your thoughts are on using curl as its not used in the code base, please let me know. // Call Log Auth Example
<?php
$clientId = '';
$clientSecret = '';
$jwt = '';
$url = 'https://platform.ringcentral.com/restapi/oauth/token';
$authorization = base64_encode($clientId . ':' . $clientSecret);
$appurl = 'https://platform.ringcentral.com/restapi/v1.0/account/~/call-log/?view=Detailed';
// Curl OAuth Token
$data = array(
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwt
);
$headers = array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic ' . $authorization
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
$resp = json_decode($response);
$token = $resp->access_token;
// Curl Call Logs
$curl = curl_init($appurl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Accept: application/json',
'Authorization: Bearer ' . $token
]);
$response = curl_exec($curl);
curl_close($curl);
// Check If Call Log Already Exists In DB
$data = json_decode($response, true);
if (isset($data['records'])) {
foreach ($data['records'] as $record) {
// Query ID in database
$id = $record['sessionId'];
...// Rough MMS / SMS Example
<?php
$from = '';
$to = '';
$text = '';
$attachment = 'data:text/plain;name=test.txt;base64,dGVzdAo=';
$appurl = 'https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/mms';
// OAuth Token
...
// Send SMS / MMS
$data = array(
'from' => array(
'phoneNumber' => $from
),
'text' => $text,
'attachments' => array(
$attachment
),
'to' => array(
array(
'phoneNumber' => $to
)
)
);
$curl = curl_init($appurl);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Accept: application/json',
'Content-Type: multipart/mixed',
'Authorization: Bearer ' . $token
]);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. guzzle would be better, it includes default proxy configuration and is simple to use, see YetiForceCRM/app/RecordCollectors/UaYouControl.php Lines 134 to 147 in f1904a6
|
||||||||||||||||||||||||||||||
| $platform = $login->platform(); | ||||||||||||||||||||||||||||||
| $RINGCENTRAL_USERNAME = $patch['RINGUSER']; | ||||||||||||||||||||||||||||||
| $RINGCENTRAL_NUMBER = $patch['RINGPHONE']; | ||||||||||||||||||||||||||||||
| $RINGCENTRAL_PASSWORD = $patch['RINGPASS']; | ||||||||||||||||||||||||||||||
| $RINGCENTRAL_EXTENSION = $patch['RINGEXT']; | ||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||
| $platform->login($RINGCENTRAL_USERNAME, | ||||||||||||||||||||||||||||||
| $RINGCENTRAL_EXTENSION, | ||||||||||||||||||||||||||||||
| $RINGCENTRAL_PASSWORD); | ||||||||||||||||||||||||||||||
| $params = array( | ||||||||||||||||||||||||||||||
| 'from' => array('phoneNumber' => $RINGCENTRAL_NUMBER), | ||||||||||||||||||||||||||||||
| 'to' => array( | ||||||||||||||||||||||||||||||
| array('phoneNumber' => $patch['to']), | ||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||
| 'text' => $patch['message'], | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
| $r = $platform->post('/account/~/extension/~/sms', $params); | ||||||||||||||||||||||||||||||
| } catch (\RingCentral\SDK\Http\ApiException $e) { | ||||||||||||||||||||||||||||||
| print 'Expected HTTP Error: ' . $e->getMessage() . PHP_EOL; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.