【问题标题】:Return multiple states in a function in Python-Telegram-Bot在 Python-Telegram-Bot 中的函数中返回多个状态
【发布时间】:2020-04-21 12:34:25
【问题描述】:

我有一个有趣的问题

这是我的代码https://pastebin.com/0LEKUhEQ,我在这里解释所有内容并尝试不同的方式。我用 Python-Telegram-Bot 创建了一个电报机器人。

我需要在一个函数中返回两个不同的状态。我尝试“return CHOOSING, TYPING_REPLY7”,但它不起作用(请参阅代码)。

我也尝试 if 和 else,但在 else 中我没有 update.message.text 因为用户没有发送任何东西,所以它不起作用。

简而言之:有没有办法在一个函数中返回多个状态?

from telegram import ReplyKeyboardMarkup, InlineKeyboardButton, InlineKeyboardMarkup

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler
updater = Updater(token='MYTOKEN', use_context=True)

dispatcher = updater.dispatcher

import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)


CHOOSING, TYPING_REPLY3, TYPING_REPLY7 = range(3)

#Callback data
ELEVEN, TWELVE = range(2)


keyboard4 = [
        [InlineKeyboardButton("Keep This Title", callback_data=str(TWELVE)),
         InlineKeyboardButton("Back", callback_data=str(ELEVEN))]
    ]
markup4 = InlineKeyboardMarkup(keyboard4)


def start(update, context):
    start.title = "Default Title"
    context.bot.send_message(chat_id=update.effective_chat.id, text="Title is: " + start.title)
    update.message.reply_text('Write and send a New Title or Click a Button', reply_markup = markup4)

#In this point, i want to return CHOOSING and also TYPING_REPLY7

#i try "return CHOOSING, TYPING_REPLY7" but it does not work
#i try if and else like this, but in else i don't have update.message.text because the user does not send anything, so it doesn't work
    """
   if update.message.text  != None:
       return TYPING_REPLY7
   else:
       return CHOOSING
   """

    #if user click on a button (The user does not send anything)
    return CHOOSING

    #if user write and send new title
    return TYPING_REPLY7


#from TYPING_REPLY7
def newtitle(update, context):
    start.title = update.message.text
    context.bot.send_message(chat_id=update.effective_chat.id, text="New Title is: " +  start.title)

    return TYPING_REPLY3

#from TYPING_REPLY3 and TWELVE
def description(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id, text="Description: ok")

#from ELEVEN
def back(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id, text="END")

def main():
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            CHOOSING: [CallbackQueryHandler(back, pattern='^' + str(ELEVEN) + '$'),
                    CallbackQueryHandler(description, pattern='^' + str(TWELVE) + '$')],

            TYPING_REPLY7: [MessageHandler(Filters.text, newtitle)],
            TYPING_REPLY3: [MessageHandler(Filters.text, description)]
        },

        fallbacks=[CommandHandler('start', start)]
    )

    dispatcher.add_handler(conv_handler)
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python telegram-bot python-telegram-bot


    【解决方案1】:

    您可以为同一状态混合不同类型的处理程序:

    states={
        CHOOSING: [
            CallbackQueryHandler(back, pattern='^' + str(ELEVEN) + '$'),
            CallbackQueryHandler(description, pattern='^' + str(TWELVE) + '$'),
            MessageHandler(Filters.text, newtitle)
        ],
        # TYPING_REPLY7: [],  # NOTE: not needed anymore (can be removed)
    
        TYPING_REPLY3: [MessageHandler(Filters.text, description)]
    },
    

    因此,您仍然有一个当前状态(从处理程序返回),但是有 2+ 种方式来处理该状态下的新响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 2021-06-21
      相关资源
      最近更新 更多