Viewing file: Soniyal.php (5.28 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\WhatsAppProvider;
use App\Models\MessageLog;
use Illuminate\Support\Facades\Log;
class Soniyal implements SendMessageInterface
{
private $credentials;
private $to_numbers;
private $body;
private $from_number;
private $mms_files;
private $message;
private $send_fail=[];
private $message_log;
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 setMessage($message){
$this->message=$message;
return $this;
}
public function process()
{
$apiKey = $this->credentials->soniyal_api_key;
$soniyalUrl = $this->credentials->soniyal_url;
$mmsFile = '';
if (isset($this->mms_files)){
$mmsFile = $this->mms_files;
}
if (!$apiKey){
throw new \Exception('Sorry! Soniyal API kry is missing. Please contact with the administrator');
}
if (!$soniyalUrl) {
throw new \Exception('Sorry! Soniyal URL is missing. Please contact with the administrator');
}
foreach ($this->to_numbers as $to) {
try {
$this->message_log = MessageLog::create([
'to' => $to,
'from' => $this->from_number,
'body' => $this->body,
'message_id' => $this->message->id,
'customer_id' => $this->message->customer_id,
'type' => 'sent',
'message_files'=> $mmsFile,
]);
$url = "$soniyalUrl?apikey=$apiKey&mobile=$to&msg=$this->body";
if ($mmsFile){
$file_imgs = [];
$file_pdf = [];
$file_video = [];
$i = 0;
foreach (json_decode($mmsFile) as $key => $mms_file) {
$mms_type = explode('.', $mms_file);
if ($mms_type[1] == "jpg" || $mms_type[1] == "jpeg" || $mms_type[1] == "png" || $mms_type[1] == "svg" || $mms_type[1] == "svg" || $mms_type[1] == "webp") {
$file_imgs['img' . ++$i] = $mms_file;
}
elseif ($mms_type[1] == "pdf") {
$file_pdf[] = $mms_file;
} elseif ($mms_type[1] == "mp4" || $mms_type[1] == "mp3" || $mms_type[1] == "webm" || $mms_type[1] == "ogg") {
$file_video[] = $mms_file;
}
}
$img = '';
$count = 0;
foreach ($file_imgs as $key => $file_img){
$img .= '&'.$key.'='.$file_img.",";
$count++;
if ($count == 4) {
break;
}
}
}
if (isset($file_pdf[0]) && isset($file_video[0])){
$url = "$soniyalUrl?apikey=$apiKey&mobile=$to&msg=$this->body$img&pdf=$file_pdf[0]&video=$file_video[0]";
}elseif (isset($file_video[0])){
$url = "$soniyalUrl?apikey=$apiKey&mobile=$to&msg=$this->body$img&video=$file_video[0]";
}elseif (isset($file_pdf[0])){
$url = "$soniyalUrl?apikey=$apiKey&mobile=$to&msg=$this->body$img&pdf=$file_pdf[0]";
}
else{
$url = "$soniyalUrl?apikey=$apiKey&mobile=$to&msg=$this->body$img";
}
Log::info($url);
$client = new \GuzzleHttp\Client(['verify' => false]);
$response = $client->request('POST', $url);
$responseString = json_decode($response->getBody()->getContents());
if (isset($responseString->status) && $responseString->status == 'ERROR' && isset($responseString->errormsg)){
throw new \Exception($responseString->errormsg);
}
Log::info($responseString);
} catch (\Exception $ex) {
if ($this->message_log) {
$this->message_log->update(['status' => 'failed']);
}
Log::error($ex);
$this->setSendFail([
'message_id' => $this->message->id,
'from_number' => $this->from_number,
'to_number' => $to,
'reason' => $ex->getMessage(),
]);
}
}
}
public function getSendFail(){
return $this->send_fail;
}
private function setSendFail($sendFailArray){
$this->send_fail[]=$sendFailArray;
}
}
|