cawa0505/telegram-bot

用Laravel 5分钟创建你的Telegram机器人

0.8.1 2019-04-06 22:51 UTC

This package is auto-updated.

Last update: 2024-09-07 10:49:53 UTC


README

cover

安装

使用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消息

ping-command

为了确保没有中间件或前缀可以“阻止”默认的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),
]);
  • 使用Client的send方法调用任何可用的方法。

  • 使用Client的save方法保存Telegram文件。

  • Client和Request分别在Command中通过$this->client$this->request可用。

将新命令添加到你的config/telegram.php中的commands数组

'/name' => App\Http\BotCommands\NameCommand::class,

发送/name Johnny消息

name-command

原始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));
});

扩展

要扩展Client,向你的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'));
});

处理错误

Client使用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_URLTELEGRAM_TOKENTELEGRAM_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)