【问题标题】:Unable to call function using python-telegram-bot in ConversationHandler无法在 ConversationHandler 中使用 python-telegram-bot 调用函数
【发布时间】:2021-08-11 19:33:03
【问题描述】:

在我的代码中,当机器人被触发时,函数user_exist() 被触发。如果返回False,则函数new_account()被触发并正常工作,相反,当返回True时,它应该触发函数wallet_handler(),但它不能。代码在对话处理程序中运行。我以为我做的一切都很好,但我不明白为什么函数wallet_handler() 没有被触发。任何人都可以帮助我吗?这是我的代码:

import logging
import data
from web3 import Web3
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup
from telegram.ext import (
    Updater,
    CommandHandler,
    MessageHandler,
    Filters,
    ConversationHandler,
    CallbackContext,
    CallbackQueryHandler,
)

NEW_ACCOUNT, WALLET_HANDLER= range(2)

def start(update: Update, context: CallbackContext) -> int:
    chatid = update.message.chat_id

    if data.user_exist(chatid) == True:
        print('s')
        return WALLET_HANDLER
    if data.user_exist(chatid) == False:
        update.message.reply_text('This is your first time with us! Please insert a wallet')  
        return NEW_ACCOUNT


def new_account(update: Update, context: CallbackContext) -> int:
    chatid = update.message.chat_id
    wallet = update.message.text

    if Web3.isAddress(wallet) == True:
        data.new_account(chatid, wallet)
        update.message.reply_text('New account succesfully created.\nWelcome to Tarsier. Enjoy it!')
        return WALLET_HANDLER
    else:
        update.message.reply_text('Wallet Address invalid.\nPlease insert a valid Wallet Address.')
        return NEW_ACCOUNT
    

def wallet_handler(update: Update, context: CallbackContext) -> int:
    update.message.reply_text('aaa')
    return ConversationHandler.END

def cancel(update: Update, context: CallbackContext) -> int:
    """Cancels and ends the conversation."""
    user = update.message.from_user
    update.message.reply_text(
        'Bye! I hope we can talk again some day.', reply_markup=ReplyKeyboardRemove()
    )

    return ConversationHandler.END


def main() -> None:
    """Run the bot."""
    # Create the Updater and pass it your bot's token.
    updater = Updater("TOKEN")

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # Add conversation handler with the states GENDER, PHOTO, LOCATION and BIO
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            NEW_ACCOUNT: [MessageHandler(Filters.text & ~Filters.command, new_account)],
            WALLET_HANDLER: [MessageHandler(Filters.text & ~Filters.command, wallet_handler)],
        },
        fallbacks=[CommandHandler('cancel', cancel)],
    )

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


if __name__ == '__main__':
    main()

【问题讨论】:

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


    【解决方案1】:

    您应该使用 check_update 函数来确定会话处于哪个状态。

    新用户状态 0 现有用户状态 1

    https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.conversationhandler.html#telegram.ext.ConversationHandler.check_update

    【讨论】:

    • 您能否展示如何编辑我的代码以使用 check_update ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2018-03-12
    • 2019-08-07
    • 2019-03-04
    • 1970-01-01
    相关资源
    最近更新 更多