Возврат нескольких состояний в функцию в Python -Telegram-Bot - PullRequest
1 голос
/ 21 апреля 2020

У меня интересная проблема .

Это мой код https://pastebin.com/0LEKUhEQ, где я все объясняю и пробую разными способами. Я создал бота для телеграммы с помощью Python -Telegram-Bot.

Мне нужно вернуть два разных состояния в функции. Я пытаюсь "вернуть CHOOSING, TYPING_REPLY7", но это не работает (пожалуйста, смотрите код).

Также я пытаюсь, если и еще, но в другом у меня нет 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()
...