电报webhook不使用数据库数据

时间:2018-02-14 03:50:17

标签: bots telegram telegram-bot

我创建了一个电报机器人。但不是硬编码回复,我决定从数据库中拉出它们..下面..问题是机器人不会返回数据库中的数据。它在电报中返回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]]);
    }

2 个答案:

答案 0 :(得分:0)

您不需要在函数中使用&lt; php echo 作为参数。

看看你的其他变量,比如$ chat_id,$ me2 ......你应该使用&#34; text&#34; =&GT; $ mes1 [&#34; message&#34;] &#34; text&#34; =&GT; $ mes2 [&#34;消息&#34;]

它已经被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"]));
    }

答案 1 :(得分:0)

我发现了问题。 数据库对象$text在持有if语句的函数中不可用。 我不得不在函数中声明它是全局的。 例如

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

现在工作正常