开发车辆油耗记录Telegram Bot:如何实现非逐位/非Web App的数字键盘?
Telegram Bot实现数字键盘的替代方案(非逐位输入/非Web App)
问题背景
我正在开发一款记录车辆油耗的Telegram Bot,用户通过指令启动新记录后,Bot会依次请求花费金额、燃油量、上次加油后行驶里程等数值,用户回复文本后,Bot解析数字并存入数据库。为加快输入流程,需要实现数字键盘,但现有的逐位发送消息方案不符合需求,且不想开发Web App,求其他可行方法。
可行方案:用自定义回复键盘(ReplyKeyboardMarkup)模拟数字键盘
Telegram Bot没有原生的数字键盘API,但可以通过自定义回复键盘模拟手机端的数字输入界面,用户点击按钮即可输入数字/小数点,还能添加删除键优化输入体验,且不会逐位发送独立消息。
实现思路
- 构建类似手机数字键盘的按钮布局,包含0-9数字、小数点和删除键
- 维护用户的当前输入状态(通过Bot的用户数据存储)
- 处理用户的按钮点击:点击数字/小数点则追加到当前输入,点击删除键则移除最后一位
- 可选添加确认按钮,用户输入完成后提交数值
Python-Telegram-Bot代码示例
from telegram import ReplyKeyboardMarkup, KeyboardButton from telegram.ext import Updater, CommandHandler, MessageHandler, Filters # 生成数字键盘布局 def build_numeric_keyboard(): # 可添加确认按钮优化流程 keyboard = [ [KeyboardButton("1"), KeyboardButton("2"), KeyboardButton("3")], [KeyboardButton("4"), KeyboardButton("5"), KeyboardButton("6")], [KeyboardButton("7"), KeyboardButton("8"), KeyboardButton("9")], [KeyboardButton("."), KeyboardButton("0"), KeyboardButton("←")], [KeyboardButton("确认输入")] ] # resize_keyboard让键盘适配屏幕,one_time_keyboard点击后自动收起 return ReplyKeyboardMarkup(keyboard, resize_keyboard=True, one_time_keyboard=False) # 启动新记录指令 def start_new_record(update, context): # 初始化用户输入状态 context.user_data["current_input"] = "" update.message.reply_text("请输入本次加油花费金额:", reply_markup=build_numeric_keyboard()) # 处理用户输入 def handle_numeric_input(update, context): user_msg = update.message.text current_input = context.user_data.get("current_input", "") # 处理删除键 if user_msg == "←": new_input = current_input[:-1] if len(current_input) > 0 else "" context.user_data["current_input"] = new_input update.message.reply_text(f"当前输入:{new_input or '空'}", reply_markup=build_numeric_keyboard()) return # 处理确认输入 if user_msg == "确认输入": if not current_input: update.message.reply_text("请先输入有效数值!", reply_markup=build_numeric_keyboard()) return # 尝试解析为数字 try: numeric_value = float(current_input) if "." in current_input else int(current_input) # 这里可以添加存入数据库的逻辑,或者进入下一个字段的请求 update.message.reply_text(f"已确认金额:{numeric_value},接下来请输入燃油量:", reply_markup=build_numeric_keyboard()) # 重置当前输入状态,准备下一个字段 context.user_data["current_input"] = "" except ValueError: update.message.reply_text("输入无效,请重新输入!", reply_markup=build_numeric_keyboard()) return # 处理数字和小数点输入 if user_msg in "0123456789.": # 限制小数点只能出现一次 if user_msg == "." and "." in current_input: update.message.reply_text("数值只能包含一个小数点!", reply_markup=build_numeric_keyboard()) return new_input = current_input + user_msg context.user_data["current_input"] = new_input update.message.reply_text(f"当前输入:{new_input}", reply_markup=build_numeric_keyboard()) else: update.message.reply_text("请使用数字键盘输入有效内容!", reply_markup=build_numeric_keyboard()) def main(): # 替换为你的Bot Token updater = Updater("YOUR_TELEGRAM_BOT_TOKEN") dp = updater.dispatcher dp.add_handler(CommandHandler("newfuel", start_new_record)) dp.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_numeric_input)) updater.start_polling() updater.idle() if __name__ == "__main__": main()
方案优势
- 完全在Telegram聊天界面内完成输入,无需跳转外部Web App
- 用户点击按钮即可构建完整数值,不会产生逐位的独立消息
- 可自定义键盘布局,添加删除、确认等功能优化输入体验
- 适配Python-Telegram-Bot等主流Bot框架,实现成本低
内容的提问来源于stack exchange,提问作者Aleben
相关产品推荐
相关产品推荐

