romanlazko/laravel-telegram
最新稳定版本:1.0.2
Composer 安装命令:
composer require romanlazko/laravel-telegram
包简介
Telegram Api for Laravel
README 文档
README
The laravel-telegram package provides a seamless integration between Laravel applications and the Telegram Bot API. It enables developers to easily build Telegram bots with clean, expressive Laravel syntax, and supports features such as:
- Bot creation and registration via Artisan
- Webhook management
- Command-based architecture
- Automatic injection of Telegram update data (Chat, Message, CallbackQuery, etc.)
- Full support for Laravel queues
🚀 Installation
Install the package using Composer:
composer require romanlazko/laravel-telegram
Optional: Publish Migrations
php artisan vendor:publish --tag=laravel-telegram-migrations
🤖 Create a Bot
Use the following command to register a bot with your Laravel application:
php artisan telegram:bot YOUR_BOT_TOKEN
This will generate a new service provider at:
app/Providers/Telegram/BotNameProvider.php
Registering the Provider
Ensure the generated service provider is registered:
- Laravel 11+: in
bootstrap/providers.php - Laravel 10 and below: in
config/app.phpunder theprovidersarray
Add it manually if it was not auto-registered.
🌐 Set the Webhook
To set the Telegram webhook for your bot:
php artisan telegram:set-webhook telegram_bot_id
The webhook URL defaults to:
https://your-app-url.com/api/telegram-webhook/{telegram_bot_id}
Local Development
Use ngrok to expose your local server:
ngrok http 8000
Then update .env:
APP_URL=https://your-ngrok-url.ngrok.io
Re-run the webhook command after updating the URL.
📦 Creating Commands
Commands are the main way to handle user interactions with the bot. Each command is a Laravel Job that responds to Telegram updates.
Generate a new command with:
php artisan telegram:command DefaultCommand
This creates a file at:
app/Telegram/BotName/Commands/DefaultCommand.php
✨ Example Command
namespace App\Telegram\MyBot\Commands; use Romanlazko\LaravelTelegram\Command; use Romanlazko\LaravelTelegram\Models\Types\Chat; class DefaultCommand extends Command { public function execute(Chat $chat) { $this->apiMethod('sendMessage'); $this->text('My first command!'); $this->chatId($chat->id); $this->parseMode('Markdown'); return $this->send(); } public function resolveDefaultClosureDependencyForEvaluationByName(string $parameterName): array { return match ($parameterName) { 'update' => [$this->getObject()], 'message' => [$this->evaluate(fn ($update) => $update->message ?? $update->callback_query->message)], 'chat' => [$this->evaluate(fn ($message) => $message->chat)], default => parent::resolveDefaultClosureDependencyForEvaluationByName($parameterName), }; } }
🧠 Dependency Injection
Your command’s execute() method can declare parameters like:
UpdateMessageChatCallbackQuery
These will be resolved and injected automatically from the incoming Telegram update.
You can override the method resolveDefaultClosureDependencyForEvaluationByName() to customize how dependencies are resolved.
✅ Features at a Glance
- Support for multiple bots
- Laravel-native syntax
- Webhook auto-registration
- Commands as Laravel Jobs
- Dependency injection
- Clean architecture
📚 Resources
📝 License
This package is open-sourced software licensed under the MIT license.
💬 Support
Feel free to open issues or contribute with PRs. Happy bot building 🤖!
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-05