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

Telegram机器人键盘:实现点击按钮填文本到输入框而非直接发送

实现Telegram机器人按钮点击填充输入框而非直接发送

要实现点击按钮后将文本填入消息输入框、而非直接发送的需求,你需要借助Telegram Bot API的InlineKeyboardMarkup组件,并为按钮配置switch_inline_query_current_chat参数,具体实现方式如下:

核心逻辑

常规的ReplyKeyboardMarkup按钮点击后会直接把文本发送到聊天窗口,而InlineKeyboardMarkup的switch_inline_query_current_chat参数,会触发客户端将指定文本填充到当前聊天的消息输入框中,用户可以继续编辑内容后再手动发送。

代码示例(以Python的python-telegram-bot库为例)

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler

def start(update, context):
    # 构建带填充输入框逻辑的按钮组
    keyboard = [
        [InlineKeyboardButton("1", switch_inline_query_current_chat="1")],
        [InlineKeyboardButton("2", switch_inline_query_current_chat="2")],
        [InlineKeyboardButton("*", switch_inline_query_current_chat="*")]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text("点击按钮填充输入框:", reply_markup=reply_markup)

def main():
    # 替换为你的机器人Token
    updater = Updater("YOUR_BOT_TOKEN")
    dp = updater.dispatcher
    dp.add_handler(CommandHandler("start", start))
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

效果说明

  • 用户点击按钮“1”后,当前聊天的消息输入框会自动填入“1”
  • 用户可继续输入“”和“1”,将输入框内容编辑为“11”,手动发送后机器人就能收到这条完整消息

注:该功能依赖Telegram客户端支持,官方APP、网页版等主流客户端均已兼容此特性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 14:01:40