K
K
kkolorid2019-04-26 22:16:51
PHP
kkolorid, 2019-04-26 22:16:51

Telegram bot API PHP - why does the response come three times?

I am attaching https://github.com/TelegramBot/Api to the yii2 framework. For some reason, after I added the 2nd on() function to process the text of the incoming message - the answers from the first on() (which processes the buttons) come three (3) times ... If you remove the second on() - it works, as it should - one answer. Previously, this code worked in native php. There is some kind of magic on the framework ...
The action code where the bot lies:

function start($bot, $message) {

            //global $db;

            // $db->query('DELETE FROM `actions` WHERE `login` = {?}', array($message->getChat()->getUserName()));

            $mess = 'Приветствую тебя!';

            $keyboard = new \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup(
                [
                    [
                        ['callback_data' => 'start', 'text' => ' Начать']
                    ],
                    [
                        ['callback_data' => 'start2', 'text' => ' Магазин']
                    ],
                    [
                        ['url' => 'http://google.com/', 'text' => ' Инструкция'],
                    ],
                ]
            );

            $bot->sendMessage($message->getChat()->getId(), $mess, false, null, null, $keyboard);

        }

        if(true){
            error_reporting(E_ALL & ~(E_NOTICE | E_USER_NOTICE | E_DEPRECATED));
            ini_set('display_errors', 1);
        }

        $token = Yii::$app->params['botToken'];

        try {

            $bot = new \TelegramBot\Api\Client($token, null);

            $bot->command('start', function ($message) use ($bot) {

                if (!BotUsers::findOne(['user_id' => $message->getChat()->getId()])) {

                    $user = new BotUsers();
                    $user->user_id = $message->getChat()->getId();
                    $user->date = Yii::$app->formatter->asDate('now', 'php:Y-m-d');
                    $user->time = Yii::$app->formatter->asTime('now', 'php:H:i:s');
                    $user->last_login = $message->getChat()->getUserName();
                    $user->save(false);

                } else {

                    $user = BotUsers::findOne(['user_id' => $message->getChat()->getId()]);

                    if ($user->last_login != $message->getChat()->getUserName()) {

                        $user->last_login = $message->getChat()->getUserName();
                        $user->save(false);

                    }

                }

                start($bot, $message);

            });

            $bot->on(function($update) use ($bot){

                $callback = $update->getCallbackQuery();
                $message = $callback->getMessage();
                $data = $callback->getData();

                $bot->answerCallbackQuery($callback->getId());

                $user = BotUsers::find()->where(['user_id' => $message->getChat()->getId()])->one();

                if($data == 'start'){

                    start($bot, $message);

                }

                if($data == 'start2'){

                    $bot->sendMessage($message->getChat()->getId(), $data);

                }

            }, function($update) {
                $callback = $update->getCallbackQuery();
                if (is_null($callback) || !strlen($callback->getData()))
                    return false;
                return true;
            });

            // Отлов любых сообщений + обрабтка reply-кнопок
            $bot->on(function($update2) use ($bot){

                $message2 = $update2->getMessage();
                $mtext = $message2->getText();
                $cid = $message2->getChat()->getId();

                if(mb_stripos($mtext,"власть советам") !== false){
                    $bot->sendMessage($message2->getChat()->getId(), "Смерть богатым!");
                }

            }, function($message2) use ($bot){
                return true; // когда тут true - команда проходит
            });

            $bot->run();

        } catch (\TelegramBot\Api\Exception $e) {
            $e->getMessage();
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question