Viewing file: SendSms.php (14.95 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Jobs;
use App\Models\Campaign; use App\Models\Contact; use App\Models\DynamicGateway; use App\Models\MessageLog; use App\Models\SmsQueue; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; use Illuminate\Support\Facades\Log;
class SendSms implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $customer_id; public $from; public $to; public $body; public $message_id; public $dynamic_gateway_id; public $sms_type; public $from_type; public $message_files; public $message; public $message_log;
public function __construct($message, $sms_type) { $this->customer_id = $message->customer_id; $this->from = $message->from; $this->to = $message->to; $this->body = $message->body; $this->message_id = $message->message_id; $this->dynamic_gateway_id = $message->dynamic_gateway_id; $this->sms_type = $sms_type; $this->from_type = $message->from_type; $this->message_files = $message->message_files ? json_decode($message->message_files) : []; $this->message = $message; }
/** * Execute the job. * * @return void */
public function handle() { try { $val = [ 'language' => 'en', 'voice_type' => 'men' ]; $dlt_template_id = ''; $dlt_entity_id = ''; if($this->message && isset($this->message->sms_gateway_value) && $this->message->sms_gateway_value){ $decodedValue = json_decode($this->message->sms_gateway_value); $dlt_entity_id = $decodedValue->entity_id ?? ''; $dlt_template_id = $decodedValue->dlt_template_id ?? ''; }
$to = $this->to; $from = $this->from; $body = $this->body;
$dynamicGateway = DynamicGateway::where('id', $this->dynamic_gateway_id)->first();
if (!$dynamicGateway) { throw new \Exception('Invalid Gateway'); }
//Create / Update MessageLog if ($this->sms_type && $this->sms_type == 'inbox') { $type = 'inbox'; } else { $type = 'sent'; }
$this->message_log = MessageLog::updateOrCreate([ 'to' => $to, 'from' => $from, 'body' => $body, 'message_id' => $this->message->message_id, 'customer_id' => $this->customer_id, 'type' => $type, 'status' => 'pending', ], ['status' => 'succeed', 'queue_id' => $this->message->id]);
try {
$sendingUrl = json_decode($dynamicGateway->weblink)->url; $sendingUrlMethod = json_decode($dynamicGateway->weblink)->method;
$parameters = []; if ($dynamicGateway->others) { foreach (json_decode($dynamicGateway->others) as $key => $gt) { $parameters[$key] = $gt; } } $headers = []; if ($dynamicGateway->headers) { foreach (json_decode($dynamicGateway->headers) as $key => $hd) { $headers[$key] = $hd; } }
$templateBody = $body; //SMS Body $parameters[$dynamicGateway->message_key] = $templateBody; //From Number $parameters[$dynamicGateway->from_mobile_key] = $from; // To Number if ($dynamicGateway->name == 'Bulk360') { $parameters[$dynamicGateway->to_mobile_key] = str_replace('+', '', $to); } else { $parameters[$dynamicGateway->to_mobile_key] = $to; }
if ($dynamicGateway->prefill && $dynamicGateway->prefill->name == 'InfoBip') { $parameters['indiaDlt'] = [ 'contentTemplateId' => $dlt_template_id, 'principalEntityId' => $dlt_entity_id, ]; }
if ($this->from_type && $this->from_type == 'voicecall') { $queue = $this->message; $msg = $queue->message; $voice_data = isset($msg->voice_obj) ? json_decode($msg->voice_obj) : ''; if (isset($dynamicGateway->voice_sms_lang_key) && isset($voice_data->language) && $voice_data->language && $dynamicGateway->voice_sms_lang_key) { $parameters[$dynamicGateway->voice_sms_lang_key] = $voice_data->language; } if (isset($dynamicGateway->voice_sms_voice_key) && isset($voice_data->voice_type) && $voice_data->voice_type && $dynamicGateway->voice_sms_voice_key) { $parameters[$dynamicGateway->voice_sms_voice_key] = $voice_data->voice_type; } }
if ($this->from_type == 'mms' || $this->from_type == 'whatsapp') { if (isset($dynamicGateway->mms_mobile_key) && isset($this->message->message_files) && $this->message->message_files) { $newMessageFiles = json_decode($this->message->message_files); array_walk($newMessageFiles, function (&$value, $index) { $value = asset('uploads/' . $value); }); // MMS File $parameters[$dynamicGateway->mms_mobile_key] = $newMessageFiles; } } // $parameters['type']='';
$param_type = 'form_params'; if ($dynamicGateway->prefill && $dynamicGateway->prefill->name == 'Plivo') { $param_type = 'json'; } else { $param_type = 'form_params'; }
if ($dynamicGateway->prefill && $dynamicGateway->prefill->name == 'SMS Noc') { $parameters[$dynamicGateway->to_mobile_key] = str_replace('+', '', $to); $param_type = 'json';
unset($parameters['noc_api_key']); }
if ($dynamicGateway->prefill && $dynamicGateway->prefill->name == 'Call DXING TTS') { $param_type = 'json'; //From Number $parameters[$dynamicGateway->from_mobile_key] = str_replace('+', '', $from); }
if ($dynamicGateway->prefill && $dynamicGateway->prefill->name == 'Ejointech') { $param_type = 'json';
$parameters['type'] = 'send-sms'; $gen_task_parameters = []; unset($parameters[$dynamicGateway->message_key]); unset($parameters[$dynamicGateway->from_mobile_key]); unset($parameters[$dynamicGateway->to_mobile_key]); $gen_new_para = []; if ($dynamicGateway->others) { foreach (json_decode($dynamicGateway->others) as $key => $gt) { unset($parameters[$key]); $gen_new_para[$key] = $gt; } } $sendingUrl = $sendingUrl . '?' . http_build_query($gen_new_para);
$gen_task_parameters[$dynamicGateway->message_key] = $templateBody; $gen_task_parameters[$dynamicGateway->from_mobile_key] = str_replace('+', '', $from); $gen_task_parameters[$dynamicGateway->to_mobile_key] = $to; $gen_task_parameters['tid'] = $this->message->id;
$parameters['type'] = 'send-sms'; $parameters['tasks'] = [$gen_task_parameters]; $headers = []; $headers['Content-Type'] = "application/json;charset=utf-8."; }
if ($this->message->from_type == 'whatsapp' && $dynamicGateway->prefill && $dynamicGateway->prefill->name == 'Twilio') { $parameters[$dynamicGateway->to_mobile_key] = 'whatsapp:' . $to; $parameters[$dynamicGateway->from_mobile_key] = 'whatsapp:' . $from; }
if ($dynamicGateway->prefill && $dynamicGateway->prefill->name == 'Konnect OBD') { $parameters['id'] = Str::random(7); $param_type = 'json'; $parameters[$dynamicGateway->to_mobile_key] = str_replace('+', '', $to); }
if ($dynamicGateway->prefill && $dynamicGateway->prefill->name == 'InfoBip Custom') { $parameters[$dynamicGateway->to_mobile_key] = [$to]; unset($parameters['text']); $parameters['content'] = [ "body" => [ $dynamicGateway->message_key => $templateBody, "type" => "TEXT" ] ]; // $parameters['channel'] = 'SMS'; $parameters = [ [ 'messages' => $parameters ] ]; $param_type = 'json';
$headers['Content-Type']='application/json'; $headers['Accept']='application/json'; }
if ($dynamicGateway->prefill && $dynamicGateway->prefill->name == 'Bulk SMS') { $prm=[ [ 'to'=>$to, 'body'=>$body ] ]; $username=isset(json_decode($dynamicGateway->inputs)->username)?json_decode($dynamicGateway->inputs)->username:''; $password=isset(json_decode($dynamicGateway->inputs)->password)?json_decode($dynamicGateway->inputs)->password:''; if(!$username || !$password){ throw new \Exception('Gateway Not Configured'); }
$sending_url = $sendingUrl.'?auto-unicode=true&longMessageMaxParts=30'; $client = new \GuzzleHttp\Client(['verify' => false]); $response = $client->post($sending_url, [ 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Basic ' . base64_encode("$username:$password"), ], 'body' => json_encode($prm), 'timeout' => 20, 'connect_timeout' => 10, ]); $responseString = $response->getBody()->getContents();
}elseif ($sendingUrlMethod == 'post') { $sending_url = $sendingUrl; $client = new \GuzzleHttp\Client(['verify' => false]); $response = $client->post($sending_url, [ $param_type => $parameters, 'headers' => $headers, ]); $responseString = $response->getBody()->getContents();
Log::info($responseString);
$responseString = ''; } else { $sending_url = $sendingUrl . '?' . http_build_query($parameters); $client = new \GuzzleHttp\Client(['verify' => false]); $response = $client->get($sending_url, [ 'headers' => $headers, ]); $responseString = $response->getBody()->getContents();
$responseString = json_decode($responseString);
// if(!$responseString || !isset($responseString->code) || $responseString->code !='200'){ // throw new \Exception('Failed Message'); // }
// Log::info($responseString); } if ($this->message) { $sms_queue = $this->message; $sms_queue->delivered_at = now(); $sms_queue->save(); } if ($this->message_log && $responseString) { $sms_log = $this->message_log; $sms_log->updated_at = now()->addSeconds(3); if ($dynamicGateway->prefill->name == 'clicksend') { $xml_file = simplexml_load_string($responseString); $json = json_encode($xml_file); $array = json_decode($json, TRUE); if (isset($array['messages']) && isset($array['messages']['message']) && isset($array['messages']['message']['messageid'])) { $sms_log->response_code = $array['messages']['message']['messageid']; } } $sms_log->save(); } } catch (\Exception $ex) { throw new \Exception($ex->getMessage()); } } catch (\Exception $ex) { \Log::info($ex->getMessage());
dd($ex->getMessage());
$sms_queue = $this->message; $sms_queue->status = 'failed'; $sms_queue->save();
$failed_message_log = $this->message_log; $failed_message_log->status = 'failed'; $failed_message_log->save();
$totalCount = 0; $characters = mb_strlen($sms_queue->body, "UTF-8"); if (strlen($sms_queue->body) != strlen(utf8_decode($sms_queue->body))) { if ($characters && $characters > 70) { $grandTotal = ceil($characters / 70); if ($grandTotal > 1) $totalCount = $grandTotal; } } else { if ($characters && $characters > 155) { $grandTotal = ceil($characters / 155); if ($grandTotal > 1) $totalCount = $grandTotal; } }
$reverseCreditUserWallet = $sms_queue->user->wallet;
if ($reverseCreditUserWallet) { $reverseCreditUserWallet->credit = $reverseCreditUserWallet->credit + $totalCount; $reverseCreditUserWallet->save(); } } }
public function failed(\Throwable $exception) { MessageLog::where('id', $this->message_log->id)->update(['status' => 'failed']); SmsQueue::where('id', $this->message->id)->update(['status' => 'failed', 'delivered_at' => null]); } }
|