-
Hi I have a scenario of performing actions on users :
<?php
use SergiX44\Nutgram\Nutgram;
use App\Commands\DeleteUserCommand;
use App\Commands\AddToFreindsCommand;
use App\Commands\BlockUserCommand;
$bot = new Nutgram('TOKEN');
$bot->onCallbackQueryData('delete_user:{id}', DeleteUserCommand::class);
$bot->onCallbackQueryData('add_to_friends:{id}', AddToFreindsCommand::class);
$bot->onCallbackQueryData('bloc_kuser:{id}', BlockUserCommand::class);
// ...
$bot->run();
<?php
namespace App\Commands;
use App\Models\User;
use SergiX44\Nutgram\Nutgram;
class DeleteUserCommand
{
public function __invoke(Nutgram $bot, $id)
{
$user = User::find($id);
if (! $user) {
throw new Exception("User with id {$id} not found! ...");
}
// do delete
$bot->sendMessage("User with id {$id} deleted.");
}
}
<?php
namespace App\Commands;
use App\Models\User;
use SergiX44\Nutgram\Nutgram;
class AddToFreindsCommand
{
public function __invoke(Nutgram $bot, $id)
{
$user = User::find($id);
if (! $user) {
throw new Exception("User with id {$id} not found! ...");
}
// do add
$bot->sendMessage("User with id {$id} added to freinds list.");
}
}
<?php
namespace App\Commands;
use App\Models\User;
use SergiX44\Nutgram\Nutgram;
class BlockUserCommand
{
public function __invoke(Nutgram $bot, $id)
{
$user = User::find($id);
if (! $user) {
throw new Exception("User with id {$id} not found! ...");
}
// do block
$bot->sendMessage("User with id {$id} blocked.");
}
} If you pay attention, this code is always repeated in the above classes: $user = User::find($id);
if (! $user) {
throw new Exception("User with id {$id} not found! ...");
} Okay, I'm thinking of defining a middleware as follows to check the presence of a user.
<?php
use SergiX44\Nutgram\Nutgram;
use App\Commands\DeleteUserCommand;
use App\Commands\AddToFreindsCommand;
use App\Commands\BlockUserCommand;
$bot = new Nutgram('TOKEN');
$bot->onCallbackQueryData('delete_user:{id}', DeleteUserCommand::class)->middleware(CheckUserExistenceMiddleware::class);
$bot->onCallbackQueryData('add_to_friends:{id}', AddToFreindsCommand::class)->middleware(CheckUserExistenceMiddleware::class);
$bot->onCallbackQueryData('bloc_kuser:{id}', BlockUserCommand::class)->middleware(CheckUserExistenceMiddleware::class);
// ...
$bot->run();
<?php
namespace App\Middlewares;
use App\Models\User;
use SergiX44\Nutgram\Nutgram;
class CheckUserExistenceMiddleware
{
// In Nutgram, it is not possible to send the parameters that we have defined in the handlers... Here, the $id parameter is defined only for example.
public function __invoke(Nutgram $bot, $next, $id)
{
$user = User::find($id);
if (! $user) {
throw new Exception("User with id {$id} not found! ...");
}
// do stuff
$next($bot);
}
} But there is a problem. how can i pass Please help me. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Just do this: class CheckUserExistenceMiddleware
{
public function __invoke(Nutgram $bot, $next)
{
[$action, $userID] = explode(':', $bot->callbackQuery()->data);
$user = User::find($userID);
if (! $user) {
throw new Exception("User with id {$id} not found! ...");
}
// do stuff
$next($bot);
}
} |
Beta Was this translation helpful? Give feedback.
Just do this: