> For the complete documentation index, see [llms.txt](https://vkfast.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vkfast.gitbook.io/docs/bot/rule.md).

# Правила (Rule)

## Использование правил

Класс, который валидирует событие, должен наследовать от базового класса `BaseRule`. Валидация события происходит в методе `passes`, который принимает объект события, а после возвращает `true` или `false`. Если хоть одно правило вернуло `false` - обработка события текущим хендлером прекращается.

```php
use Fastik1\VkBot\Rules\isChatMessageRule;
use Fastik1\VkBot\Rules\isPrivateMessageRule;
use Fastik1\Vkfast\Bot\Events\MessageNew;

$bot->message(function (MessageNew $event) { // обработка всех сообщений из чатов (использование правила)
    // обработка события, которое прошло валидацию правилами
})->rule(new isChatMessageRule);

$bot->message(function (MessageNew $event) { // обработка всех личных сообщений (использование правила)
    // обработка события, которое прошло валидацию правилами
})->rule([new isPrivateMessageRule, /* другие правила в виде массива */]);
```

## Реализация правила `isChatMessageRule`

```php
namespace Fastik1\Vkfast\Bot\Rules; // используйте свой namespace при создании собственных правил

use Fastik1\Vkfast\Bot\Events\Event;
use Fastik1\Vkfast\Bot\Events\MessageNew;

class IsChatMessageBaseRule extends BaseRule
{
    public function passes(MessageNew|Event $event): bool
    {
        return $event->message->peer_id != $event->message->from_id; // если peer_id и from_id равны - значит это сообщение из чата
    }
}
```

{% hint style="info" %}
isChatMessageRule и isPrivateMessageRule - базовые правила, проверяющие сообщение на источник: чат или личное сообщение.
{% endhint %}

## Использование динамический правил

Вы можете сделать свое правило динамичным. Пример такого правила:

```php
use Fastik1\Vkfast\Bot\Events\Event;
use Fastik1\Vkfast\Bot\Events\MessageEvent;
use Fastik1\Vkfast\Bot\Rules\BaseRule;

class PeerIdRule extends BaseRule
{
    private int $peer_id;

    public function __construct(int $peer_id)
    {
        $this->peer_id= $peer_id;
    }

    public function passes(MessageEvent|Event $event): bool
    {
        return $event->message->peer_id == $this->peer_id;
    }
}
```

Использование такого правила:

```php
$bot->message(function (MessageNew $event) { // обработка всех сообщений из чатов (использование правила) 
    // обработка события, которое прошло валидацию правилами
})->rule(new PeerIdRule(2000000001));
```

Таким образом, мы можем удобно проверять событие на определенный `peer_id`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vkfast.gitbook.io/docs/bot/rule.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
