I'm trying to edit messages in a telegram bot. I'm using the following code. I can send messages with no problem, but the edit message part doesn't work. It just doesn't do anything!!
<?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;
}
?>
Any idea about what can be wrong with it? Thanks
The problem is in this part of your code
case("add"):
editMessageText($chatId, "should I add?");
You are not passing the parameters correctly. editMessageText method requires a parameter message_id
, which should be an `Integer.
This should be the working code
<?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;
}
?>
The official Telegram docs say:
Please note, that it is currently only possible to edit messages without reply_markup or with inline keyboards.
You may be able to use theeditMessageReplyMarkup
API.
I understand that this question is quite old and this is my first answer, If someone is having the issue with editMessageText
You need to understand few points very clearly:
1 - ONLY MESSAGES SENT BY A BOT Could be edited by bot. Bot can't edit messages sent by a human.
2- editMessageText requires 3 parameters:
editMessageText($chatId, $messageId, "MY TEXT THAT WILL REPLACE");
3 - The code by ManzoorWani would not work because the $messageId
is still giving human message id, so you would be getting an 400 permission error.
You would need to put: $messageId = $messageId -1
to get the id of text sent by bot earlier, First try the action
message then try to edit message, bot shall reply you something by action
message and by add
you would be replacing bot text.
EXAMPLE from the code above:
function editMessageText($chatId, $messageId, $message) {
$messageId=$messageId-1;
$url="https://api.telegram.org/bot[BOTAPI]/editMessageText?chat_id=".$chatId."&message_id=".$messageId."&text=".$message;
file_get_contents($url);
}
4 - $messageId
always reflects a sequence, like 1,2,3,4 so the id of the next message is obvious, you can perform actions accordingly.
ANOTHER tip is to first try browser to edit the text sent by bot using url if you are doing this for the first time just to be sure its working.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.