serogaq / laravel-tgbotapi
This package provides methods for working with the telegram bot api, and helpers for receiving updates via webhooks or the long polling method
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires
- php: >=8.0.2
- guzzlehttp/guzzle: ^7.2
- illuminate/support: ^8.0 || ^9.0
Requires (Dev)
- nunomaduro/collision: ^5.10 || ^6.1
- orchestra/testbench: ^6.0 || ^7.0
- php-parallel-lint/php-parallel-lint: ^1.3
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-04-11 09:31:38 UTC
README
This package provides methods for working with the telegram bot api, and helpers for receiving updates via webhooks or the polling method
Requires PHP >= 8.0.2
and Laravel >= 8
Installation
Require the serogaq/laravel-tgbotapi
package in your composer.json
and update your dependencies:
composer require serogaq/laravel-tgbotapi
Then run the command:
php artisan tgbotapi:install
Usage
Set up your bot in config/tgbotapi.php
Add a listener for the new update event:
// app/Providers/EventServiceProvider.php use App\Listeners\HandleNewUpdate; use Serogaq\TgBotApi\Events\NewUpdateEvent; class EventServiceProvider extends ServiceProvider { protected $listen = [ NewUpdateEvent::class => [ HandleNewUpdate::class, ], ]; }
To receive updates via webhooks, use the command:
php artisan tgbotapi:setwebhook
To receive updates via a long polling, create a background task:
// app/Console/Kernel.php class Kernel extends ConsoleKernel { protected function schedule(Schedule $schedule) { $schedule->command('tgbotapi:getupdates', ['bot_username', '--until-complete'])->everyMinute()->withoutOverlapping()->runInBackground(); } }
Events will be sent to app/Listeners/HandleNewUpdate.php
where you can process them.
Package provides the logic for handling updates in controllers:
// app/Listeners/HandleNewUpdate.php use Serogaq\TgBotApi\Facades\BotManager; use Serogaq\TgBotApi\Events\NewUpdateEvent; use Serogaq\TgBotApi\Traits\ProcessingInControllers; class HandleNewUpdate { use ProcessingInControllers; }
Next, create controller for the update:
$ php artisan make:tgbotapi:controller CommandUpdate
All updates with type CommandUpdate, i.e. commands, will be processed in the controller:
// app/TgBotApi/Updates/CommandUpdate.php use Serogaq\TgBotApi\Events\NewUpdateEvent; use Serogaq\TgBotApi\Facades\BotManager; use Serogaq\TgBotApi\Interfaces\UpdateController; class CommandUpdate implements UpdateController { public function __construct(protected NewUpdateEvent $event) {} public function handle(): void { BotManager::bot('username_bot')?->sendMessage([ 'text' => $this->event->update['message']['text'], 'chat_id' => $this->event->update['message']['chat']['id'] ])->send(); } }
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Troubleshooting
If you like living on the edge, please report any bugs you find on the issues page.
License
The BSD-3-Clause. Please see License File for more information.