如何解决电报自定义KeyboardButton不起作用执行后,电报中没有按钮按键,不会发生错误
python版本3.8.3
import telegram #imorted methodts
from telegram.ext import Updater,CommandHandler
import requests
from telegram import ReplyKeyboardMarkup,KeyboardButton
from telegram.ext.messagehandler import MessageHandler
import json
# below is function defined the buttons to be return
def start(bot,update):
button1 = KeyboardButton("hello")
button2 = KeyboardButton("by")
keyboard = [button1,button2]
reply_markup = telegram.ReplyKeyboardMarkup(keyboard)
chat_id = update.message.chat_id
bot.send_message(chat_id=chat_id,text='please choose USD or EUR',reply_markup = reply_markup) # it works and returns text if reply_markup parameter disabled.
def main():
updater = Updater('my token')
dp = updater.dispatcher
dp.add_handler(CommandHandler('start',start))
updater.start_polling()
updater.idle()
main()
按钮不起作用。请帮助检查可能是此问题的原因。
解决方法
尝试使用字符串而不是键盘按钮:
def start(bot,update):
keyboard = [["USD","EUR"]]
reply_markup = telegram.ReplyKeyboardMarkup(keyboard)
chat_id = update.message.chat_id
bot.send_message(chat_id=chat_id,text='please choose USD or EUR',reply_markup = reply_markup)
def main():
updater = Updater('my token')
dp = updater.dispatcher
dp.add_handler(CommandHandler('start',start))
updater.start_polling()
updater.idle()
main()
,
添加logging,这样您就可以查看是否有任何错误或有错误的响应。
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。