Skip to content

Commit

Permalink
Fixed telegram bot handle message
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Mar 21, 2024
1 parent c88fa63 commit 891804c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
6 changes: 2 additions & 4 deletions app/Http/Controllers/WebHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Controllers;

use App\Jobs\TelegramMessage;
use App\Services\TelegramBot;
use Illuminate\Http\Request;

class WebHookController extends Controller
Expand All @@ -16,11 +15,10 @@ class WebHookController extends Controller
*
* @return void
*/
public function telegram(Request $request, TelegramBot $telegramBot): void
public function telegram(Request $request): void
{
TelegramMessage::dispatch(
$request->collect('message'),
$telegramBot,
$request->collect('message')
);
}
}
24 changes: 8 additions & 16 deletions app/Jobs/TelegramMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TelegramMessage implements ShouldQueue
/**
* Create a new job instance.
*/
public function __construct(public Collection $message, public TelegramBot $telegramBot)
public function __construct(public Collection $message)
{
$this->text = $this->message->only(['text', 'caption'])->first();
$this->messageId = $this->message->get('message_id');
Expand All @@ -33,28 +33,20 @@ public function __construct(public Collection $message, public TelegramBot $tele
/**
* Execute the job.
*/
public function handle(): void
public function handle(TelegramBot $telegramBot): void
{
// If the message is a reply to another message, it's likely not spam.
// Let's not disrupt the conversation and ignore it.
if ($this->message->has('reply_to_message')) {
return;
}

if ($this->telegramBot->isSpam($this->text)) {
$this->blocked();
}
}
if ($telegramBot->isSpam($this->text)) {
// Delete the spam message from the group chat.
$telegramBot->deleteMessage($this->chatId, $this->messageId);

/**
* Block the message and mute the sender in the group.
*/
public function blocked(): void
{
// Delete the spam message from the group chat.
$this->telegramBot->deleteMessage($this->chatId, $this->messageId);

// Mute the sender of the spam message in the group chat.
$this->telegramBot->muteUserInGroup($this->chatId, $this->from);
// Mute the sender of the spam message in the group chat.
$telegramBot->muteUserInGroup($this->chatId, $this->from);
}
}
}

0 comments on commit 891804c

Please sign in to comment.