-
Maybe do you know how better i can realize bun, unban system in my bot? It will be fine if here was middleware. |
Beta Was this translation helpful? Give feedback.
Answered by
punyflash
Jul 25, 2023
Replies: 1 comment 1 reply
-
By architecture, bot handlers are middlewares (responsibility chain) by themselves, you can just create handler that will check if user is banned and then add it on top of your handlers list: <?php
namespace Somewhere\InYour\App;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use WeStacks\TeleBot\Handlers\UpdateHandler;
class AuthHandler extends UpdateHandler
{
public function __invoke($next)
{
if ($tgUser = $this->update->user()) {
$user = User::where('telegram_id', $thUser->id)->first();
// This will check if user is banned (you need to write ban functionality by yourself or use some package)
if (!$user || $user->isBanned()) {
return $this->sendMessage([
'text' => "You can't access this bot!"
]);
}
// Store user in auth object for next update handlers to avoid extra database queries
// Access it by Auth::user()
Auth::setUser($user);
}
// In case you are using Laravel Octane you need to logout user manually after handling update
return tap($next(), fn () => Auth::forgetUser());
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
punyflash
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By architecture, bot handlers are middlewares (responsibility chain) by themselves, you can just create handler that will check if user is banned and then add it on top of your handlers list: