diff --git a/docs/README.md b/docs/README.md index b282e01..b0156c8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -47,6 +47,10 @@ - [Подписка на Webhook](#Подписка-на-Webhook) - [Удаление подписки](#Удаление-подписки) - [Список подписок](#Список-подписок) + - [Обработка хуков](#Обработка-хуков) + - [Long Polling](#Long-Polling) + - [Handler Classes](#Handler-Classes) + - [Тестирование](#Тестирование) ## Быстрый старт @@ -494,3 +498,119 @@ php artisan maxbot:webhook:unsubscribe https://yourapp.com/bot/webhook ```bash php artisan maxbot:webhook:list ``` + +### Обработка хуков + +Создайте контроллер для обработчика: + +```php +use Illuminate\Http\Request; +use BushlanovDev\MaxMessengerBot\Laravel\MaxBotManager; + +class WebhookController extends Controller +{ + public function handle(Request $request, MaxBotManager $botManager) + { + // Обработчик сообщений + $botManager->onMessageCreated(function (MessageCreatedUpdate $update) { + $message = $update->message; + // ... + }); + + // Обработчик команды /start + $botManager->onCommand('start', function (MessageCreatedUpdate $update) { + // ... + }); + + // Using Laravel container bindings + $botManager->onMessageCreated(MessageHandler::class); + $botManager->onCommand('help', HelpCommandHandler::class); + + return $botManager->handleWebhook($request); + } +} +``` + +Добавьте маршрут в `routes/web.php`: + +```php +Route::post('/bot/webhook', [WebhookController::class, 'handle']); +``` + +### Long Polling + +Создайте artisan команду для получения long polling обновлений: + +```php +use Illuminate\Console\Command; +use BushlanovDev\MaxMessengerBot\Laravel\MaxBotManager; + +class BotPollingCommand extends Command +{ + protected $signature = 'bot:polling'; + + protected $description = 'Start bot long polling'; + + public function handle(MaxBotManager $botManager) + { + $this->info('Starting bot polling...'); + $botManager->startLongPolling(); + } +} +``` + +### Handler Classes + +Примеры классов обработчиков обновлений: + +```php +class MessageHandler +{ + public function handle(MessageCreatedUpdate $update) + { + $message = $update->message; + $text = $message->body?->text; + + if ($text) { + app(Api::class)->sendMessage( + userId: $message->sender->userId, + text: "You said: $text", + ); + } + } +} + +class HelpCommandHandler +{ + public function handle(MessageCreatedUpdate $update) + { + app(Api::class)->sendMessage( + userId: $update->message->sender->userId, + text: "This is help message", + ); + } +} +``` + +### Тестирование + +Вы можете использовать мок API для тестирования: + +```php +use BushlanovDev\MaxMessengerBot\Api; + +class BotTest extends TestCase +{ + public function test_bot_sends_message() + { + $apiMock = $this->createMock(Api::class); + $apiMock->expects($this->once()) + ->method('sendUserMessage') + ->with(123456, 'Hello!'); + + $this->app->instance(Api::class, $apiMock); + + // Your test code here + } +} +```