maxodrom/yii2-telegram

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

安装: 1

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 23

类型:yii2-extension

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

This package is auto-updated.

Last update: 2024-09-29 05:02:32 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

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

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

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