File: /var/www/exnova-telegram-bot-v2/app/Jobs/ProcessWebhookJob.php
<?php
namespace App\Jobs;
use App\Models\User;
use App\Services\Requests\TelegramRequest;
use Illuminate\Support\Carbon;
use Spatie\WebhookClient\Jobs\ProcessWebhookJob as SpatieProcessWebhookJob;
use Spatie\WebhookClient\Models\WebhookCall;
class ProcessWebhookJob extends SpatieProcessWebhookJob
{
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
$payload = $this->webhookCall->payload;
$ruleProcessed = null;
$this->webhookCall->url = '';
if (!empty($payload['update_id'])) {
$this->webhookCall->request_id = $payload['update_id'];
$this->webhookCall->save();
if (!empty(
array_intersect(
array_keys($payload),
['message', 'callback_query', 'poll_answer', 'channel_post', 'document', 'video', 'media', 'image']
)
)) {
$repeatedCall = false;
if (!empty($payload['update_id'])) {
$repeatedCall = WebhookCall::where('request_id', $payload['update_id'])
->where('id', '<>', $this->webhookCall->id)
->where('created_at', '>=', Carbon::now()->subMinutes(3)->format('Y-m-d H:i:s'));
$repeatedCall = $repeatedCall->first();
}
if (is_object($repeatedCall)) {
$this->webhookCall->processed = 2;
} else {
$request = new TelegramRequest($payload);
$ruleProcessed = $request->handle($this->webhookCall);
if ($ruleProcessed !== null) {
$this->webhookCall->processed = 1;
}
}
}
}
$this->webhookCall->updated_at = date('Y-m-d H:i:s');
$this->webhookCall->save();
} catch (\Exception $e) {
$this->failed($e);
}
}
public function failed(\Throwable $throwable)
{
$user = User::query()->first();
$user->sendMessageToChannel('Error in ProcessWebhookJob: ' . $throwable->getMessage() . "\n " . substr($throwable->getTraceAsString(), 0,400));
}
}