Viewing file: Plivo.php (5.59 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\SmsProvider;
use App\Models\MessageLog;
use App\Models\Message;
use Plivo\Exceptions\PlivoRestException;
use Plivo\RestClient;
class Plivo
{
private $credentials;
private $to_numbers;
private $body;
private $from_number;
private $mms_files;
private $message;
private $send_fail = [];
public function __construct($credentials)
{
$this->credentials = $credentials;
}
public function setTo($to)
{
$this->to_numbers = $to;
return $this;
}
public function setBody($body)
{
$this->body = $body;
return $this;
}
public function setFrom($from)
{
$this->from_number = $from;
return $this;
}
public function setFiles($files)
{
$this->mms_files = $files;
return $this;
}
public function setQueue($queue)
{
$this->queue = $queue;
return $this;
}
public function setMessage($message)
{
$this->message = $message;
return $this;
}
public function setType($response)
{
$this->sms_type = $response;
return $this;
}
public function send()
{
if (!isset($this->credentials->pl_auth_id) || !isset($this->credentials->pl_auth_token)) {
throw new \Exception('This provider is not configured. Please contact with the administrator');
}
try {
$client = new RestClient($this->credentials->pl_auth_id, $this->credentials->pl_auth_token);
$messageLogIds = [];
$queueIds = [];
foreach ($this->to_numbers as $key => $to_number) {
if ($this->sms_type && $this->sms_type == 'inbox') {
$type = 'inbox';
} else {
$type = 'sent';
}
if ($this->queue) {
$this->message_log = MessageLog::updateOrCreate([
'to' => $to_number,
'from' => $this->from_number,
'body' => $this->body,
'message_id' => $this->message->id,
'customer_id' => $this->message->customer_id,
'type' => 'sent',
], ['status' => 'succeed', 'queue_id' => $this->queue->id]);
} else {
$this->message_log = MessageLog::create([
'to' => $to_number,
'from' => $this->from_number,
'body' => $this->body,
'message_id' => $this->message->id,
'customer_id' => $this->message->customer_id,
'type' => $type,
'status' => 'succeed'
]);
}
$messageLogIds[] = $this->message_log->id;
}
$response = '';
$toNumbers = implode('<', $this->to_numbers);
if ($this->mms_files) {
$newMessageFiles = $this->mms_files;
array_walk($newMessageFiles, function (&$value, $index) {
$value = asset('uploads/' . $value);
});
$client->client->setTimeout(40);
$response = $client->messages->create([
"src" => $this->from_number,
"dst" => $toNumbers,
"text" => $this->body,
"type" => "mms",
"media_urls" => $newMessageFiles,
"url" => route('plivo.webhook', [$this->message->id])
]);
} else {
$response = $client->messages->create([
"src" => $this->from_number,
"dst" => $toNumbers,
"text" => $this->body,
"url" => route('plivo.webhook', [$this->message->id])
]
);
}
if ($this->queue) {
$smsQueue = $this->queue;
$messageUid = $response->getmessageUuid()[0];
Message::where('id', $smsQueue->id)->update(['response_id' => $messageUid]);
}
foreach ($messageLogIds as $key => $id) {
$uid = $response->getmessageUuid()[$key];
MessageLog::where('id', $id)->update(['response_id' => $uid]);
}
} catch (PlivoRestException $ex) {
if ($this->message_log) {
$this->message_log->update(['status' => 'failed', 'response_code' => $ex->getCode()]);
}
if ($this->queue) {
$this->queue->update(['status' => 'failed', 'response_code' => $ex->getCode()]);
}
//TODO:: Need to change count in the future
if (isset($type) && $type == 'sent' && count($this->to_numbers) == 1) {
$this->setSendFail([
'message_id' => $this->message->id,
'from_number' => $this->from_number,
'to_number' => $this->to_numbers[0],
'created_at' => now(),
'updated_at' => now(),
'reason' => $ex->getMessage(),
]);
}
}
}
public function getSendFail()
{
return $this->send_fail;
}
private function setSendFail($sendFailArray)
{
$this->send_fail[] = $sendFailArray;
}
}
|