black-river / telegram-bot
用 Laravel 在 5 分钟内创建您的 Telegram Bot
Requires
- guzzlehttp/guzzle: ~6.0
- illuminate/contracts: 5.2.*|5.3.*
- illuminate/support: 5.2.*|5.3.*
This package is not auto-updated.
Last update: 2024-09-18 19:09:09 UTC
README
安装
使用 Composer 安装此包
composer require black-river/telegram-bot
将服务提供者添加到您的 config/app.php
文件的 providers
数组中
BlackRiver\TelegramBot\ServiceProvider::class,
配置
发布配置文件
php artisan vendor:publish --provider="BlackRiver\TelegramBot\ServiceProvider"
在您的 .env
文件中设置环境变量
APP_URL="http://your-bot.com"
...
TELEGRAM_TOKEN="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
要使用 自签名证书,您还应指定证书路径
TELEGRAM_CERTIFICATE="/etc/nginx/ssl/your-bot.com.crt"
快速入门
在您的路由文件中定义默认 webhook 路由
Route::post(config('telegram.token'), function (BlackRiver\TelegramBot\Bot $bot) { $bot->listen(); });
- 使用 Bot 的
listen
方法处理命令。
设置 webhook url
php artisan webhook:set
为了确保 bot 已准备好,发送 /ping
消息
为了确保没有中间件或前缀可能“阻止”默认 webhook 路由,请检查您的
app/Providers/RouteServiceProvider.php
。
Webhook URL
您可以将默认 webhook 路由更改为您自己的
Route::post('your-secret-path', function (BlackRiver\TelegramBot\Bot $bot) { $bot->listen(); });
php artisan webhook:set your-secret-path
要删除 webhook 集成,运行 php artisan webhook:remove
。
Bot 命令
在 app/Http/BotCommands
目录中创建一个新的 Bot 命令
php artisan make:bot-command NameCommand
编辑 app/Http/BotCommands/NameCommand.php
的 handle
方法
$this->client->send('sendMessage', [ 'chat_id' => $this->request->json('message.chat.id'), 'text' => 'Hello, '.trim($message), ]);
-
使用客户端的
send
方法调用任何可用的方法。 -
使用客户端的
save
方法保存 Telegram 文件。 -
客户端和请求在命令中分别通过
$this->client
和$this->request
可用。
将新命令添加到您的 config/telegram.php
文件的 commands
数组中
'/name' => App\Http\BotCommands\NameCommand::class,
发送 /name Johnny
消息
原始 Webhook
Route::post('your-secret-path', function (Illuminate\Http\Request $request, BlackRiver\TelegramBot\Client $client) { // $bot->listen(); $update = $request->json()->all(); $client->send('sendMessage', [ 'chat_id' => $request->json('message.chat.id'), 'text' => 'I\'ve got it!', ]); });
门面
将门面添加到您的 config/app.php
文件的 aliases
数组中
'TelegramBot' => BlackRiver\TelegramBot\Facades\TelegramBot::class, 'TelegramApi' => BlackRiver\TelegramBot\Facades\TelegramApi::class,
用法
Route::post('your-secret-path', function () { // TelegramBot::listen(); TelegramApi::send('sendMessage', [ 'chat_id' => Request::json('message.chat.id'), 'text' => 'Hey!', ]); });
示例
向您的聊天发送照片
Route::post('photo', function (BlackRiver\TelegramBot\Client $client) { $client->send('sendPhoto', [ 'chat_id' => 'your-chat-id', 'photo' => fopen(storage_path('photo.png'), 'r'), ]); });
保存传入的文件
Route::post('your-secret-path', function (Illuminate\Http\Request $request, BlackRiver\TelegramBot\Client $client) { $doc = $request->json('message.document'); $filename = $doc['file_name']; $file = $client->send('getFile', ['file_id' => $doc['file_id']]); $client->save($file['result']['file_path'], storage_path($filename)); });
扩展
要扩展客户端,请将新的宏添加到您的 app/Providers/AppServiceProvider.php
文件的 boot
方法中
app('BlackRiver\TelegramBot\Client')->macro('sendUploadedPhoto', function ($chatId, \Illuminate\Http\UploadedFile $photo) { $saved = $photo->move(storage_path(), $photo->getClientOriginalName()); $this->send('sendPhoto', [ 'chat_id' => $chatId, 'photo' => fopen($saved->getRealPath(), 'r'), ]); } );
向您的聊天发送上传的照片
Route::post('upload', function (Illuminate\Http\Request $request, BlackRiver\TelegramBot\Client $client) { $client->sendUploadedPhoto('your-chat-id', $request->file('photo')); });
处理错误
客户端使用 Guzzle Http Client 与 Telegram API 交互,因此您可以处理 Guzzle 异常
try { $client->send('methodName', []); } catch (GuzzleHttp\Exception\ClientException $e) { // 400 level errors... } catch (GuzzleHttp\Exception\ServerException $e) { // 500 level errors... } catch (GuzzleHttp\Exception\RequestException $e) { // Connection timeout, DNS errors, etc. }
Lumen
使用 Composer 安装此包
composer require black-river/telegram-bot
将服务提供者添加到您的 bootstrap/app.php
文件的 Register Service Providers
部分
$app->register(BlackRiver\TelegramBot\ServiceProvider::class);
在您的 .env
文件中设置 APP_URL
、TELEGRAM_TOKEN
和 TELEGRAM_CERTIFICATE
变量。
将供应商的 telegram.php
配置文件复制到您的 config
目录
mkdir config/ && cp vendor/black-river/telegram-bot/config/telegram.php config/
在您的路由文件中定义默认 webhook 路由
$app->post(config('telegram.token'), function (BlackRiver\TelegramBot\Bot $bot) { $bot->listen(); });
许可
Laravel Telegram Bot 在 MIT 许可证 (MIT) 下授权。