在纯 php 上的电报机器人中使用回调查询处理行中的用户消息输入

问题描述

我在处理电报机器人中的用户消息输入时遇到了问题。

我在没有任何库的情况下在 PHP 上编写电报机器人。 Bot 通过 webhook 从电报中接收信息,两个 switch-case 正在处理消息和 callback_query。

短版本看起来像这样,它也没有保存到数据库

$output = json_decode(file_get_contents('PHP://input'),TRUE);
$chat_id = $output['message']['chat']['id'];
$message = $output['message']['text'];
$callback_query = $output['callback_query'];
$data = $callback_query['data'];
$callback_query_id = $callback_query['id'];
$message_id = $output['callback_query']['message']['message_id'];
$message_id_message = $output['message']['message_id'];
$chat_id_in = $callback_query['message']['chat']['id'];]

//Handler of messages
switch($message) {
    case '/start':
        $button1 = ['text'=>'Almaty','callback_data'=>"almaty"];
        $button2 = ['text'=>'Astana','callback_data'=>"astana"];
        $inline_keyboard = [[$button1],[$button2]];
        $keyboard=["inline_keyboard"=>$inline_keyboard];
        $replyMarkup = json_encode($keyboard);
        sendMessage($chat_id,"Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",$replyMarkup);

        // Save User to bd if it new,or catch to file.txt
        try
        {
            $first_name = isset($output["message"]['from']['first_name']) ? $output["message"]['from']['first_name'] : null;
            $username = isset($output["message"]['from']['username']) ? $output["message"]['from']['username'] : null;
            $pdo = getPDO();
            $stmt = $pdo->prepare("
                    INSERT INTO 
                      users (chat_id,first_name,username)
                    VALUES (:chat_id,:first_name,:username)
                ");
        } catch (Exception $e) {
            file_put_contents('file.txt',$e->getMessage()."\n",FILE_APPEND);
        }
        break;
}
    
//handler of callback_query
switch($data){
    case 'almaty':
        deleteMessage($chat_id_in,$message_id);
        deleteMessage($chat_id_in,$message_id-1);

        $inline_keyboard = [
            [
                ['text'=>'aujezovskij','callback_data'=>"aujezovskij"]
            ],[
                ['text'=>'alatauskij','callback_data'=>"alatauskij"]
            ],[
                ['text'=>'almalinskij','callback_data'=>"almalinskij"]
            ],[
                ['text'=>'bostandykskij','callback_data'=>"bostandykskij"]
            ],[
                ['text'=>'zhetysuskij','callback_data'=>"zhetysuskij"]
            ],[
                ['text'=>'medeuskij','callback_data'=>"medeuskij"]
            ],[
                ['text'=>'nauryzbajskiy','callback_data'=>"nauryzbajskiy"]
            ],[
                ['text'=>'turksibskij','callback_data'=>"turksibskij"]
            ],[
                ['text'=>'Go back','callback_data'=>"goback"]
            ]
        ];
        $keyboard=["inline_keyboard"=>$inline_keyboard];
        $replyMarkup = json_encode($keyboard);
        sendMessage($chat_id_in,$replyMarkup);
        break;

    case 'astana':
        deleteMessage($chat_id_in,$message_id-1);

        $inline_keyboard = [
            [
                ['text'=>'esilskij','callback_data'=>"esilskij"]
            ],[
                ['text'=>'almatinskij','callback_data'=>"almatinskij"]
            ],[
                ['text'=>'saryarkinskij','callback_data'=>"saryarkinskij"]
            ],[
                ['text'=>'r-n-bajkonur','callback_data'=>"r-n-bajkonur"]
            ],$replyMarkup);
        break;

    case "goback":
        deleteMessage($chat_id_in,$message_id);
        $button1 = ['text'=>'Almaty',[$button2]];
        $keyboard=["inline_keyboard"=>$inline_keyboard];
        $replyMarkup = json_encode($keyboard);
        sendMessage($chat_id_in,$replyMarkup);
        break;

    case "aujezovskij":
    case "alatauskij":
    case "almalinskij":
    case "bostandykskij":
    case "zhetysuskij":
    case "medeuskij":
    case "nauryzbajskiy":
    case "turksibskij":
    case "esilskij":
    case "almatinskij":
    case "saryarkinskij":
    case "r-n-bajkonur":
        deleteMessage($chat_id_in,$message_id);

        $inline_keyboard = [
            [
                ['text'=>'flats','callback_data'=>"kvartiry"]
            ],[
                    ['text'=>'rooms','callback_data'=>"komnaty"]
                ],[
                    ['text'=>'all','callback_data'=>"all"]
                ],[
                    ['text'=>'Go back','callback_data'=>"$city"]
                ]
            ];
            $keyboard=["inline_keyboard"=>$inline_keyboard];
            $replyMarkup = json_encode($keyboard);
            sendMessage($chat_id_in,$replyMarkup);
            break;
}

接下来的所有步骤我只使用带有内联按钮的 callback_query。但是在接下来的几个步骤中,我需要发送带有问题“写价格范围”和两个按钮返回“没关系”的消息: picture of message to user "Write price range"

主要问题是用户应该写整数范围时会有相同的问题,我需要知道我应该将此答案保存到哪个数据库列。

最近的尝试是制作一个包含队列的表格,该表格将在某个步骤创建并指示在哪里保存答案。为此,我在从中选择字段的位置添加认开关,并通过 IF 语句检查用户来自哪个步骤。 除了那些按钮返回“无关紧要”之外,一切正常,因为在队列表中存在行时,脚本被卡在开关认值中处理消息,并且无法转到正在处理回调查询的 switch-case 代码的简短示例:

// Message handler
switch($message) {
    case '/start':
        $button1 = ['text'=>'Алматы','callback_data'=>"almaty"];
        $button2 = ['text'=>'Астана',FILE_APPEND);
        }
        break;

    default:
        $pdo = getPDO();
        $stmt = $pdo->prepare("
                        SELECT suspense,suspense_from from user_suspense
                        WHERE chat_id = 201502307
                    ");

        $stmt->execute();
        $status = $stmt->fetch();
        if ($status['suspense_from'] == "price" AND $status['suspense'] == 'wait'){
            try
            {
                $pdo = getPDO();
                $stmt = $pdo->prepare("
                        UPDATE users
                        SET price=:price
                        WHERE chat_id = $chat_id
                    ");

                $stmt->execute([
                    ':price' => $message
                ]);

                $stmt = $pdo->prepare("
                        UPDATE user_suspense
                        SET suspense = :suspense
                        WHERE chat_id = $chat_id
                    ");

                $stmt->execute([
                    ':suspense' => 'proceed'
                ]);
            } catch (Exception $e) {
                file_put_contents('file.txt',$e->getMessage() . "\n",FILE_APPEND);
            }
                $pdo = getPDO();
                $stmt = $pdo->prepare("
                            SELECT suspense from user_suspense
                            WHERE chat_id = $chat_id
                        ");

                $stmt->execute();
                $status = $stmt->fetch();
            deleteMessage($chat_id,$status['chat_id_in']);
            deleteMessage($chat_id,$message_id_message);
            sendMessageNomarkup($chat_id,'fffff');
        }
        break;
}

//Callback Query handler
switch($data){
case "price":
            try
            {
                $pdo = getPDO();
                $stmt = $pdo->prepare("
                    INSERT INTO 
                      user_suspense (chat_id)
                    VALUES (:chat_id)
                ");

                $stmt->execute([
                    ':chat_id'=> $chat_id_in
                ]);
            } catch (Exception $e) {
                file_put_contents('file.txt',FILE_APPEND);
            }

            try
            {
                $pdo = getPDO();
                $stmt = $pdo->prepare("
                        UPDATE user_suspense
                        SET suspense=:suspense,suspense_from=:suspense_from,chat_id_in=:chat_id_in
                        WHERE chat_id = $chat_id_in
                    ");

                $stmt->execute([
                    ':suspense' => 'wait',':suspense_from' => $data,':chat_id_in' => $message_id+1
                ]);
            } catch (Exception $e) {
                file_put_contents('file.txt',FILE_APPEND);
            }
            deleteMessage($chat_id_in,$message_id);
            $inline_keyboard = [
                [
                    ['text'=>"Doesn't matter",'callback_data'=>"commission"]
                ],'callback_data'=>"remont"]
                ]
            ];
            $keyboard=["inline_keyboard"=>$inline_keyboard];
            $replyMarkup = json_encode($keyboard);
            sendMessage($chat_id_in,"Write price range \n Lorem ipsum dolor sit amet,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n Example: 100-200",$replyMarkup);
        break;
}

现在我不知道如何解决这个问题,没有代码的全局变化。我想添加完整的代码,但仅限于 30000 个字符,所以我希望我能清楚地展示和描述问题。 预先感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...