This package is a simple Laravel wrapper for the Official Telegram Bot API.
composer require aglipanci/telegrambot
In order for the Telegram Bot to work you need to add the following values to your .env file:
TELEGRAMBOT_API_KEY=api-key-here
TELEGRAMBOT_USERNAME=bot-username-here
TELEGRAMBOT_WEBHOOK_URL=webhook-url-here
To publish the package configuration file, execute the following artisan command:
php artisan vendor:publish --provider="AgliPanci\TelegramBot\TelegramBotServiceProvider"
The main Telegram class from Longman\TelegramBot\Telegram
will be available as a facade through AgliPanci\TelegramBot\Facades\Telegram
and all the original methods can be called statically from everywhere. For example:
use \AgliPanci\TelegramBot\Facades\Telegram;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Telegram as TelegramBot
Telegram::setUpdateFilter(function (Update $update, TelegramBot $telegram, &$reason = 'Update denied by update_filter') {
$user_id = $update->getMessage()->getFrom()->getId();
if ($user_id === 428) {
return true;
}
$reason = "Invalid user with ID {$user_id}";
return false;
});
On your web.php or api.php add the following line:
Route::telegrambot('telegram');
If you are using the package route and controller you don't need to populate TELEGRAMBOT_WEBHOOK_URL
.
After populating .env
file with the API credentials and webhook URL you need to execute the following command to setup the Webhook URL:
php artisan telegram:set:webhook
Now you are ready to process requests by the Telegram Bot.
The default path for the commands it's Bot/Commands insider your App directory. That can be changed in the telegrambot.php
by changing the commands_dir key.
'commands_dir' => app_path('Bot/Commands')
You can generate classes for your commands using the artisan command:
php artisan telegram:make:command SomethingCommand
This will generate the command structure on the specified command directory (the default is App/Bot/Commands
). After generating the command the system will be ready to process /something
from your users.
For more details on how the Official Telegram Bot API works please read their official documentation.