onmotion/yii2-telegram

支持基于Telegram机器人的网站聊天

安装数量: 5,541

依赖项: 0

建议者: 0

安全性: 0

星标: 51

关注者: 11

分支: 23

开放问题: 1

类型:yii2-extension

v0.8.6 2018-10-29 09:35 UTC

This package is auto-updated.

Last update: 2024-09-10 00:59:50 UTC


README

Latest Stable Version Total Downloads License Daily Downloads Monthly Downloads

支持基于Telegram机器人的网站聊天

该机器人的逻辑基于 akalongman/php-telegram-bot,因此您可以阅读longman的说明,了解如何注册Telegram机器人等。

目前仅支持Telegram webhook api。您需要SSL证书!不支持http!

安装

安装此扩展的首选方式是通过 composer

运行

composer require onmotion/yii2-telegram

添加到您的web配置中

 'modules' => [
     //...
    'telegram' => [
        'class' => 'onmotion\telegram\Module',
        'API_KEY' => 'forexample241875489:AdfgdfFuVJdsKa1cycuxra36g4dfgt66',
        'BOT_NAME' => 'YourBotName_bot',
        'hook_url' => 'https://yourhost.com/telegram/default/hook', // must be https! (if not prettyUrl https://yourhost.com/index.php?r=telegram/default/hook)
        'PASSPHRASE' => 'passphrase for login',
        // 'db' => 'db2', //db file name from config dir
        // 'userCommandsPath' => '@app/modules/telegram/UserCommands',
        // 'timeBeforeResetChatHandler' => 60
    ]
    //more...
 ]

并添加到控制台配置中

 'bootstrap' => [   
 //other bootstrap components...
                'telegram'],
 'modules' => [
         //...
     'telegram' => [
         'class' => 'onmotion\telegram\Module',
         'API_KEY' => 'forexample241875489:AdfgdfFuVJdsKa1cycuxra36g4dfgt66',
         'BOT_NAME' => 'YourBotName_bot',
     'hook_url' => 'https://yourhost.com/telegram/default/hook', // must be https! (if not prettyUrl https://yourhost.com/index.php?r=telegram/default/hook)
         'PASSPHRASE' => 'passphrase for login',
     ]
 ],       

运行迁移

php yii migrate --migrationPath=@vendor/onmotion/yii2-telegram/migrations #that add 4 tables in your DB

或添加到您的配置文件中

'controllerMap' => [
    ...
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationNamespaces' => [
            'onmotion\telegram\migrations',
        ],
    ],
    ...
],

并运行

php yii migrate/up

转到 https://yourhost.com/telegram/default/set-webhook(如果不是prettyUrl https://yourhost.com/index.php?r=telegram/default/set-webhook

现在您可以将它放置在您想要的位置

echo \onmotion\telegram\Telegram::widget(); //that add chat button in the page

在右下角您可以看到

chat button

如果您点击它

client chat

并服务器端

client chat

如果您想限制消息历史记录的存储期限,请添加到您的crontab

#leave 5 days (if empty - default = 7)
php yii telegram/messages/clean 5

您还可以使用自定义命令。为此,您可以从 /vendor/onmotion/yii2-telegram/Commands 复制UserCommands目录,并在配置中添加此路径,例如

'userCommandsPath' => '@app/modules/telegram/UserCommands'

timeBeforeResetChatHandler - 在聊天处理器被杀死之前(如果它忘记了执行/leavedialog)之前的分钟数。如果为0或未设置,则永不杀死。