vanloctech/telehook

Telegram Webhook - Laravel 包用于处理 Telegram 上的 Webhook 命令

v2.1.0 2023-03-14 15:35 UTC

This package is auto-updated.

Last update: 2024-09-16 06:12:50 UTC


README

类似 Telegram 机器人的 Laravel 命令对话

DEMO

demo.mp4

要求

  • Laravel 框架 >= 5.8 及以上
  • PHP >= 7.3

报告问题

如果您发现任何问题,请随时通过 GitHub 的错误跟踪器 报告此项目的问题。

或者,分支该项目并提交 pull request :)

设置

安装包

composer require vanloctech/telehook

发布配置文件 telehook.php,迁移文件和项目翻译文件

php artisan vendor:publish --provider="Vanloctech\Telehook\TelehookServiceProvider"

执行每分钟运行一次的计划任务以停止超过时间限制的对话

// app/Console/Kernel.php

// ...

protected function schedule(Schedule $schedule)
{
    // ...
    $schedule->command('telehook:stop-conversation')->everyMinute();
}

// ...

我们还提供了 Telehook 的外观(已使用我们的设置连接),如果您需要,请将以下内容添加到您的 config/app.php 文件中。

// config/app.php

'aliases' => [
    ...
    'Telehook' => \Vanloctech\Telehook\Facades\TelehookFacade::class,
],

使用方法

创建 Telegram 命令 - telehook 命令

php artisan make:telehook-command <Command Name>
# Ex: php artisan make:telehook-command HelloWorld

重写 finish 函数中的代码

<?php
// TelehookCommand/HelloWorldTelehookCommand.php

namespace App\TelehookCommand;

use Vanloctech\Telehook\Commands\TelehookCommand;

class HelloWorldTelehookCommand extends TelehookCommand
{
    ...

    /**
     * Execute when prepare finish conversation
     *
     * @return void
     */
    public function finish()
    {
        // TODO: Implement finish() method.
    }
}

将命令添加到 telehook 配置文件 config/telehook.php

// config/telehook.php

    'commands' => [
        HelpTelehookCommand::class,
        ...
        
        // add more your command
        \App\TelehookCommand\HelloWorldTelehookCommand::class
    ],

使用 Telehook 向多个 chatId 发送消息

Telehook::init()->setChatId('<chatId>')->sendMessage('your text');
Telehook::init()->setChatId(['<array chatId>'])->sendMessages('your text');

使用 telegramApi 属性的更多功能

// any files

Telehook::init('<chat_id>')->telegramApi->sendPhoto(...);
Telehook::init('<chat_id>')->telegramApi->sendDocument(...);
# and more function support call api, referer: https://github.com/irazasyed/telegram-bot-sdk
# <chat_id> you can get in `telehook_users` table through `TelehookUser` model

如何在项目中设置

.env 文件中声明 https URLAPP_URL),(因为 Telegram Webhook 需要 https

**您可以使用 ngrok(https://ngrok.com)来创建 https URL

# .env

APP_URL=https://<your app url>

接下来,您可以使用在 config/telehook.php 中设置的信息或 .env 文件中设置的信息设置 webhook

// config/telehook.php

    /*
    |--------------------------------------------------------------------------
    | Unique authentication token of telegram bot
    |--------------------------------------------------------------------------
    */
    'token' => env('TELEHOOK_TOKEN', ''),

    /*
    |--------------------------------------------------------------------------
    | Set webhook parameters
    |--------------------------------------------------------------------------
    */
    'set_webhook' => [
        'url' => env('APP_URL') . '/' . env('TELEHOOK_TOKEN', '')
            . '/' . env('TELEHOOK_PATH', 'webhook'),
        // 'certificate' => env('TELEHOOK_CERTIFICATE', ''),
        // 'ip_address' => '',
        // 'max_connections' => '',
        // 'allowed_updates' => '',
        'drop_pending_updates' => true,
    ],

并运行命令以设置 webhook

php artisan telehook:set-webhook

# output example
Your URI webhook: https://<your url webhook>
Set webhook successfully.

设置菜单命令(bot 命令列表)

php artisan telehook:set-command

# output example
+---------+-----------------------------------+
| command | description                       |
+---------+-----------------------------------+
| help    | Display list of the bot's command |
| example | Example command                   |
| stop    | Stop conversation                 |
+---------+-----------------------------------+

您可以为清除完成的对话设置计划

// app/Console/Kernel.php

// ...

protected function schedule(Schedule $schedule)
{
    // ...
    $schedule->command('telehook:clear --chunk=1000')->dailyAt('01:00');
}

// ...