grozzzny/telegram

Yii2 的 Telegram 模块

安装: 20

依赖项: 0

建议者: 0

安全性: 0

星标: 1

关注者: 2

分支: 0

公开问题: 0

类型:yii2-extension

dev-main 2021-08-23 16:16 UTC

This package is auto-updated.

Last update: 2024-09-23 23:06:54 UTC


README

Telegram 模块 for yii2

设置 Webhook

https://api.telegram.org/bot<token>/setWebhook?url=https://site.ru/telegram/<token>

安装

使用 aki/yii2-bot-telegram 模块

$ php composer.phar require grozzzny/telegram "dev-main"

设置日志

'log' => [
  'targets' => [
      [
           'class' => 'yii\log\FileTarget',
           'levels' => ['info'],
           'categories' => ['telegram'],
           'logFile' => '@app/runtime/logs/telegram.log'
      ],
  ],
],

添加模块

'telegram' => [
     'class' => 'grozzzny\telegram\TelegramModule',
     'controllerMap' => [
         'default' => [
             'class'  => 'grozzzny\telegram\controllers\DefaultController',
             'classAction' => 'grozzzny\telegram\components\ExampleAction'
         ]
     ]
],

添加组件

 'telegram' => [
     'class' => 'grozzzny\telegram\components\Telegram',
     'botToken' => '',
     'trace' => false
 ],

添加路由

'telegram/<token:[^\/]+>' => 'telegram/default/index',

使用动作

alt text

class ExampleAction extends \grozzzny\telegram\components\TelegramAction
{
    public function bind()
    {
        parent::bind();

        # text interception
        if($this->update->message->text == 'message'){
            Yii::$app->telegram->sendMessage([
                'chat_id' => $this->update->message->chat->id,
                'text' => 'You wrote the word "message"'
            ]);
        };
    }

    # /test
    public function commandTest($param = null)
    {
        $chat_id = $this->update->message->chat->id;

        Yii::$app->telegram->sendMessage([
            'chat_id' => $chat_id,
            'text' => 'Test success!'
        ]);
    }

    # /run
    public function commandRun($param = null)
    {
        $chat_id = $this->update->message->chat->id;

        Yii::$app->telegram->sendMessage([
            'chat_id' => $chat_id,
            'text' => 'Hi! How can I help you?',
            'reply_markup' => self::inlineKeyBoard([[
                [
                    'text' => 'I\'m a button, click on me',
                    'callback_data' => self::encodeCallbackData('answerClick', ['123'])
                ],
            ]])
        ]);
    }

    protected function answerClick($numbers)
    {
        Yii::$app->telegram->answerCallbackQuery(['callback_query_id' => $this->update->callback_query->id]);

        $chat_id = $this->update->callback_query->message->chat->id;

        Yii::$app->telegram->sendMessage([
            'chat_id' => $chat_id,
            'text' => 'The button passed the parameters "'.$numbers.'". Subscribe to the event "your response"',
            'reply_markup' => self::forceReply(),
        ]);

        $this->callbackEvent($chat_id, 'response', [$numbers, '456']);
    }

    protected function response($numbers_1, $numbers_2)
    {
        $chat_id = $this->update->message->chat->id;

        $text = $this->update->message->text;

        Yii::$app->telegram->sendMessage([
            'chat_id' => $chat_id,
            'text' => implode(' ', ['Response:', $numbers_1, $numbers_2, $text])
        ]);
    }
}