Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

RING Central SMS Integration #12504

Open
wants to merge 3 commits into
base: developer
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions modules/SMSNotifier/providers/RingCentral.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?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') {
z0lo13 marked this conversation as resolved.
Show resolved Hide resolved
$field['text'] = [''];
$field['label'] = 'CLIENT_ID';
$fields[] = $field;
}
if ($name === 'RINGUSER') {
$field['text'] = [''];
$field['label'] = 'RINGUSER';
$fields[] = $field;
}
if ($name === 'RINGPASS') {
$field['text'] = [''];
$field['label'] = 'RINGPASS';
$fields[] = $field;
}
if ($name === 'RINGPHONE') {
$field['text'] = [''];
$field['label'] = 'RINGPHONE';
$fields[] = $field;
}
if ($name === 'RINGEXT') {
$field['text'] = [''];
$field['label'] = 'RINGEXT';
$fields[] = $field;
}
z0lo13 marked this conversation as resolved.
Show resolved Hide resolved
}
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What class is this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

@z0lo13 z0lo13 May 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mariuszkrzaczkowski

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.
P.S
Letting you know that Ringcentral one of the largest VoIP providers in North America, so potentially including this integration will bring you more clients.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mariuszkrzaczkowski

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);

Choose a reason for hiding this comment

The 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

$response = [];
try {
$response = \App\RequestHttp::getClient([
'headers' => [
'Authorization' => 'Bearer ' . $this->apiKey
]
])->get($this->url . $companyNumber);
if (200 === $response->getStatusCode()) {
$this->data = $this->parseData(\App\Json::decode($response->getBody()->getContents()));
}
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
\App\Log::warning($e->getMessage(), 'RecordCollectors');
$this->response['error'] = $e->getResponse()->getReasonPhrase();
}

$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;
}
}