1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
问题出在代码的这一部分

case("add"):
editMessageText($chatId, "should I add?");
您没有正确传递参数。editMessageText 方法需要一个参数 ,该参数应为 'Integer.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;
}


?>

3

官方 Telegram 文档说:

请注意,目前只能在没有 reply_markup 或使用内嵌键盘的情况下编辑消息。

您可以使用 editMessageReplyMarkup API。

共享

改进此答案跟随

编辑于 2019 年 5 月 28 日 2:33

Cody Gray 的用户头像

科迪·格雷

244 千米5151 枚金质徽章501501 枚银质徽章581581 枚铜牌回答 2019年5月28日 0:24

6111 枚金徽章22 枚银质徽章1313 枚铜牌