Skip to content

Commit

Permalink
If errors in sending messages are exceeded, the chat will be deleted …
Browse files Browse the repository at this point in the history
…from the database.
  • Loading branch information
andrey-helldar committed Mar 21, 2024
1 parent 025605f commit 9d6e9b8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
23 changes: 21 additions & 2 deletions app/Services/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,30 @@

class Telegram
{
protected int $maxErrors = 10;

public function publish(ReleaseData $data): void
{
$this->chats()->each(
fn (TelegraphChat $chat) => $this->send($chat, $data)
fn (TelegraphChat $chat) => rescue(
fn () => $this->send($chat, $data),
fn () => $this->failed($chat)
)
);
}

protected function send(TelegraphChat $chat, ReleaseData $data): void
{
rescue(fn () => $chat->html($this->message($data))->send());
$chat->html($this->message($data))->send();

$this->resetErrors($chat);
}

protected function failed(TelegraphChat $chat): void
{
$chat->errors <= $this->maxErrors
? $chat->increment('errors')
: $chat->delete();
}

protected function message(ReleaseData $release): string
Expand All @@ -31,4 +45,9 @@ protected function chats(): Collection
{
return TelegraphChat::get();
}

protected function resetErrors(TelegraphChat $chat): void
{
$chat->updateQuietly(['errors' => 0]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::table('telegraph_chats', function (Blueprint $table) {
$table->unsignedInteger('errors')->default(0)->after('telegraph_bot_id');
});
}

public function down(): void
{
Schema::table('telegraph_chats', function (Blueprint $table) {
$table->dropColumn('errors');
});
}
};

0 comments on commit 9d6e9b8

Please sign in to comment.