首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python Telegram bot API中解决updater.dispatcher.add_hanlder问题

如何在python Telegram bot API中解决updater.dispatcher.add_hanlder问题
EN

Stack Overflow用户
提问于 2017-08-07 19:51:40
回答 1查看 2.2K关注 0票数 0

我正在编写一个python电报机器人,我想与嵌入式键盘底部工作。我写了下面的代码:

代码语言:javascript
复制
from telegram import *
from telegram.ext import *
mbti_message = 'This is a test massage that should sent in MBTI part'

def startx(bot, update):
    keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'),
                 InlineKeyboardButton("Option 2", callback_data='2')],

                [InlineKeyboardButton("Option 3", callback_data='3')]]

    reply_markup = InlineKeyboardMarkup(keyboard)
    chat_id = update.message.chat_id
    bot.sendMessage(chat_id, "Message Sent From Function STARTX", reply_markup=reply_markup)

def buttomx(bot, update):

    query = update.callback_query

    bot.edit_message_text(text="Selected option: %s" % query.data,
                          chat_id=query.message.chat_id,
                          message_id=query.message.message_id)

def start (bot,update):

    keyboard_list = [[InlineKeyboardButton("ABOUT US", callback_data='1')],
                     [InlineKeyboardButton("MBTI Test Start", callback_data='2')]]
    reply_markup = InlineKeyboardMarkup(keyboard_list)
    chat_id = update.message.chat_id
    start_message_sent = "Welcome To My Bot Please Chose Your Options"
    bot.sendMessage(chat_id,start_message_sent, reply_markup=reply_markup)
    bot.sendMessage(chat_id,start_message_sent_global,reply_markup=reply_markup)

def bottom(bot, update):

    counter = 0
    query = update.callback_query
    chat_id = query.message.chat_id
    selected_option = int(query.data)

    if selected_option==1 :
        bot.sendMessage(chat_id,"You Chose About Us Section Thanks For Choosing Us")
    elif selected_option==2:
        bot.sendMessage(chat_id,"You Are Ready To Start Test ...")
        command = 'mbti'
        updater.dispatcher.add_handler(CommandHandler(command=command, callback=startx))
        updater.dispatcher.add_handler(CallbackQueryHandler(buttomx))
        updater.start_polling()

当用户按下底部的MBTI set command to mbti并将其传递给命令处理程序时,当命令处理程序获得mbti命令时,启动运行starx函数。

但是当我在if条件中使用下面的代码时,它会在检查if条件之前发送它

代码语言:javascript
复制
updater.dispatcher.add_handler (CommandHandler (command = command , startx)

在这种情况下我应该怎么做?

EN

回答 1

Stack Overflow用户

发布于 2017-09-18 02:14:01

使用ConversationHandler显示深层交互式菜单。另一种方式可以存储状态,并使用userState调用函数。

有关更多信息,请参阅docs example,它还支持user_data和RegExp,以便强大地处理来自用户的消息。

不要忘记:数据存储在内存中,在某些情况下,您可能丢失或不正确。很好,在入口点清理了user_data

每个函数可以有多个returns指向其他条目和另一边-每个条目在不同的匹配大小写下有许多不同的函数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45546250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档