CrestRoad/laravel/app/Console/Commands/Sendwhatsapp.php

141 lines
5.3 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Http\Requests\LambdaRequest;
use App\Models\Session;
use App\Services\SessionCommunicationService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
class Sendwhatsapp extends Command
{
protected $signature = 'whatsapp:send';
protected $description = 'Send queued WhatsApp messages and expand active message_send campaigns';
protected $comm;
public function __construct(SessionCommunicationService $comm)
{
parent::__construct();
$this->comm = $comm;
}
public function handle()
{
$auth = env('INTERAKT_API_AUTH', 'Basic S1BnVWoxRGZUcWpTYVl2OG9ZN3lEbkk2YjlXTzUtZXhvOWpMQ3RIN3ZJVTo=');
$sends = DB::table('raphiwa_send')->where('is_sent', 0)->limit(30)->get();
if ($sends->count() > 0) {
foreach ($sends as $send) {
if ($send->session_id > 0) {
$mode = $this->comm->resolveMode((int) $send->session_id);
if (!$this->comm->shouldSendWhatsApp($mode)) {
DB::table('raphiwa_send')->where('id', $send->id)->update([
'is_sent' => 1,
'response' => 'skipped_email_only_session',
'updated_at' => now(),
]);
continue;
}
}
$resp = Http::withHeaders(['Authorization' => $auth])
->post('https://api.interakt.ai/v1/public/message/', (array) json_decode($send->text));
$respj = json_decode($resp->getBody());
print_r($respj);
if ($respj->result) {
DB::table('raphiwa_send')->where('id', $send->id)->update([
'is_sent' => 1,
'response' => $resp->getBody(),
'updated_at' => now(),
]);
} else {
DB::table('raphiwa_send')->where('id', $send->id)->update([
'is_sent' => 1,
'response' => $resp->getBody(),
'updated_at' => now(),
'status' => 0,
]);
}
}
} else {
$sends = DB::table('message_send as ms')
->leftJoin('sessions as s', 'ms.session_id', '=', 's.id')
->leftJoin('message_template as t', 'ms.template', '=', 't.id')
->where('ms.status', 'active')
->select(
'ms.id',
'ms.session_id',
's.code',
's.type',
's.number',
's.start_date',
's.venue',
's.communication_mode',
't.template_code',
't.template_body',
't.title as template_title',
't.button_url',
't.button_label',
'ms.header_params',
'ms.body_params',
'ms.button_params',
'ms.audience'
)
->limit(5)
->get();
if ($sends->count() > 0) {
foreach ($sends as $send) {
print_r($send);
$searchparam = ['session_code' => $send->code];
if ($send->audience == 'attended') {
$searchparam['present'] = 'yes';
}
$response = LambdaRequest::getrequest(
'contact',
'contact/get_all',
[
'source' => 'dynamodb',
'search' => json_encode($searchparam),
'start' => 0,
'length' => 9999,
],
env('LAMBDA_ACCESS_TOKEN', 'default_value')
);
$sessionModel = Session::find($send->session_id);
$contacts = SessionCommunicationService::extractContactItems($response);
if ($sessionModel && count($contacts) > 0) {
$config = [
'id' => $send->id,
'template_code' => $send->template_code,
'template_body' => $send->template_body,
'template_title' => $send->template_title,
'button_url' => $send->button_url,
'button_label' => $send->button_label,
'header_params' => $send->header_params,
'body_params' => $send->body_params,
'button_params' => $send->button_params,
];
foreach ($contacts as $item) {
$this->comm->dispatchForContact($sessionModel, $item, $config);
}
}
DB::table('message_send')->where('id', $send->id)->update(['status' => 'send']);
}
}
}
return 0;
}
}