Telegram Webhook不使用数据库数据



我创建了一个电报机器人。但是,我决定将它们从数据库中提取。它在电报中返回null ..但是,如果我回应变量 $mes['message']在我的浏览器中,它返回有效的数据。有什么问题?

 $message1 = $db->prepare("SELECT message FROM about where id=1 ");
    $message1->execute();
    $mes = $message1->fetch();
    if (isset($message['text'])) {
        // incoming text message
        $text = $message['text'];
        if ((strpos($text, "/start") === 0) || (strpos($text, "/start") === 0) ) {
            apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => 'Hello', 'reply_markup' => array(
                'keyboard' => array(array('Hello', 'Hi')),
                'one_time_keyboard' => true,
                'resize_keyboard' => true)));
        } else if ($text === "Hello" || $text === "Hi") {
            apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Nice to meet you'));
        } else if (strpos($text, "/stop") === 0) {
            apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => 'Did i do anything wrong?', 'reply_markup' => array(
                'keyboard' => array(array('Yes', 'No')),
                'one_time_keyboard' => true,
                'resize_keyboard' => true)));
        }else if ($text === "No") {
            apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Nice to meet you'));
        }else if ($text === "Tell me more") {
            apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => '<?php echo $mes2["message"];?>'));
        } else {
            apiRequestWebhook("sendMessage", array('chat_id' => $chat_id, "reply_to_message_id" => $message_id, "text" => '<?php echo $mes1["message"]; ?>'));
        }

    } else {
        apiRequestJson("sendMessage", ['chat_id' => $chat_id, "text" => $me2 , 'reply_markup' => [
            'keyboard' => [['Tell me more', 'Got it']],
            'one_time_keyboard' => true,
            'resize_keyboard' => true]]);
    }

您不需要使用&lt; php echo 用于函数中的参数。

查看其他变量,例如$ chat_id,$ me2 ...您应该使用" text" => $ mes1 [" message"] " text" => $ mes2["消息"]

它已经由PHP启用。您不需要再嵌入一次

     }else if ($text === "Tell me more") {
        apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => $mes2["message"]));
    } else {
        apiRequestWebhook("sendMessage", array('chat_id' => $chat_id, "reply_to_message_id" => $message_id, "text" => $mes1["message"]));
    }

我发现了问题。保存IF语句的功能中,数据库对象$text不可用。我必须将其声明为全球功能。例如

    function sendMessage () {
    global $text;
    //if statements here
    }

现在正常工作

最新更新