You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telegram机器人开发:能否同时使用inline keyboard与评论按钮?

解决方案

Telegram 频道的默认评论按钮有平台自动生成规则:仅当消息没有自定义 ReplyMarkup 时才会显示,一旦你添加了自定义 inline 键盘,平台就会自动隐藏默认评论按钮。要同时保留自定义按钮和评论功能,只需要手动把评论按钮添加到你的自定义 inline 键盘中即可,实现效果和官方默认评论按钮完全一致。

前提条件

  • 你的频道已经绑定了对应的讨论组,已正常开启评论功能
  • 可获取到频道的数字ID(公开频道也可直接使用频道用户名)

实现步骤

  1. 发送目标消息,从返回的响应结果中获取该消息在频道内的 MessageID
  2. 拼接对应消息的评论跳转链接,两种可选格式:
    • 通用格式(支持公开/私有频道):https://t.me/c/[频道数字ID去掉前缀-100]/[消息ID]?comment=1
    • 公开频道专属格式:https://t.me/[频道用户名]/[消息ID]?comment=1
  3. 将评论按钮作为 URL 类型的 inline 按钮,和你需要的其他自定义按钮一起配置到键盘中
  4. 调用编辑消息接口,给已发送的消息添加上构造好的混合键盘

完整Golang代码示例

import "strings"
import "fmt"

// 1. 先发送不带键盘的目标消息
message := tgbotapi.NewPhotoUpload(channel.ID, tgbotapi.FileBytes{
    Name:  "image-name",
    Bytes: image,
})
message.Caption = "This is the caption"
message.ParseMode = "HTML"
response, err := bot.Send(message)
if err != nil {
    fmt.Println(err)
    return
}

// 2. 拼接评论链接,这里使用通用数字ID方案
channelRawID := fmt.Sprintf("%d", channel.ID)
channelPureID := strings.TrimPrefix(channelRawID, "-100")
commentUrl := fmt.Sprintf("https://t.me/c/%s/%d?comment=1", channelPureID, response.MessageID)

// 3. 构造同时包含自定义按钮和评论按钮的inline键盘
var keyboard = tgbotapi.NewInlineKeyboardMarkup(
    tgbotapi.NewInlineKeyboardRow(
        tgbotapi.NewInlineKeyboardButtonData("Test", "test"),
    ),
    tgbotapi.NewInlineKeyboardRow(
        tgbotapi.NewInlineKeyboardButtonURL("评论", commentUrl),
    ),
)

// 4. 为已发送的消息更新键盘配置
editMarkup := tgbotapi.NewEditMessageReplyMarkup(channel.ID, response.MessageID, keyboard)
_, err = bot.Send(editMarkup)
if err != nil {
    fmt.Println(err)
}

内容的提问来源于stack exchange,提问作者quattro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 06:48:00