grozzzny/telegram

Telegram module for Yii2

Installs: 20

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 0

Open Issues: 0

Type:yii2-extension

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

This package is auto-updated.

Last update: 2024-04-23 22:19:41 UTC


README

Telegram module for yii2

Set Webhook

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

Install

Use module with aki/yii2-bot-telegram

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

Set log

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

Add module

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

Add component

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

Add route:

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

Use action:

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])
        ]);
    }
}