在电报机器人中使用编辑消息

时间:2017-02-20 21:12:29

标签: php telegram telegram-bot

我试图在电报机器人中编辑消息。 我使用以下代码。我可以毫无问题地发送消息,但编辑消息部分不起作用。它只是没有做任何事情!!

   <?php

if ($_SERVER['HTTPS'] != "on") {
$url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
header("Location: $url");
exit;
}

$botToken="XXXX...";
$website="https://api.telegram.org/bot".$botToken;

$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];

switch($message) {
    case("action"):
                sendMessage($chatId, "What should I do?");
    break;

    case("add"):
        editMessageText($chatId, "should I add?");

    break;

default:
    sendMessage($chatId, "default");
}

function sendMessage($chatId, $message) {

$url = $GLOBALS[website]."/sendMessage?    chat_id=".$chatId."&text=".urlencode($message)."&reply_markup".$reply1;
file_get_contents($url);
}

function editMessageText($chatId, $messageId, $message) {

$url = $GLOBALS[website]."/editMessageText?chat_id=".$chatId."&message_id=".$messageId."&text=".urlencode($message);
file_get_contents($url);

}

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       

$data = curl_exec($ch);
curl_close($ch);

return $data;
}


?>

有关它可能出现什么问题的任何想法? 感谢

2 个答案:

答案 0 :(得分:1)

问题在于你的代码的这部分

case("add"):
    editMessageText($chatId, "should I add?");

您没有正确传递参数。 editMessageText方法需要一个参数message_id,它应该是一个整数。

这应该是工作代码

    <?php

    if ($_SERVER['HTTPS'] != "on") {
    $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    header("Location: $url");
    exit;
    }

    $botToken="XXXX...";
    $website="https://api.telegram.org/bot".$botToken;

    $content = file_get_contents("php://input");
    $update = json_decode($content, true);
    $chatId = $update["message"]["chat"]["id"];
    $message = $update["message"]["text"];
    // get message_id
    $messageId = $update["message"]["message_id"];
    switch($message) {
        case("action"):
                    sendMessage($chatId, "What should I do?");
        break;

        case("add"):
        // add the 2nd parameter
            editMessageText($chatId, $messageId, "should I add?");

        break;

    default:
        sendMessage($chatId, "default");
    }

    function sendMessage($chatId, $message) {

    $url = $GLOBALS[website]."/sendMessage?    chat_id=".$chatId."&text=".urlencode($message)."&reply_markup".$reply1;
    file_get_contents($url);
    }

    function editMessageText($chatId, $messageId, $message) {

    $url = $GLOBALS[website]."/editMessageText?chat_id=".$chatId."&message_id=".$messageId."&text=".urlencode($message);
    file_get_contents($url);

    }

    function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
    }


    ?>

答案 1 :(得分:1)

official Telegram docs说:

  

请注意,当前只能在不使用Reply_markup或嵌入式键盘的情况下编辑邮件。

您也许可以使用editMessageReplyMarkup API。