一封回复后电报机器人什么也不做

时间:2018-09-30 14:42:49

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

我是python的新手,它试图在电报上创建一个对话机器人,询问用户性别和年龄。但是,我的机器人在达到用户年龄后什么也不做。 我的性别和年龄功能:

def gender(bot, update):
# Get gender from user
user = update.message.from_user
logger.info("Gender of %s: %s", user.first_name, update.message.text)
update.message.reply_text(
    "<TEXT>",
reply_markup=ReplyKeyboardRemove())

def age(bot, update):
# Get age from user
user = update.message.from_user
logger.info("Age of %s: %s", user.first_name, update.message.text)
update.message.reply_text(
        "<TEXT>"
)
return ConversationHandler.END

def skip_age(bot, update):
# /skip command
user = update.message.from_user
logger.info("User %s did not specify age", user.first_name)
update.message.reply_text(
    "<TEXT>"
)
return ConversationHandler.END

主要功能中的命令处理程序和消息处理程序:

 conv_handler = ConversationHandler(
    entry_points=[CommandHandler('chat', chat)],

    states={
        GENDER: [RegexHandler('^(Boy|Girl|Other)$', gender)],

        AGE: [MessageHandler(Filters.text, age),
            CommandHandler('skip', skip_age)]
    },

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

dispatcher.add_handler(conv_handler)

机器人的日志(在日志中重命名了令牌):

2018-09-30 21:20:33,977 - __main__ = INFO - Gender of Jan: Boy
2018-09-30 21:29:00,826 - telegram.vendor.ptb_urllib3.urllib3.connectionpool = WARNING - Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<telegram.vendor.ptb_urllib3.urllib3.connectionpool.HTTPSConnectionPool object at 0x7fafec6b5ef0>, 'Connect timed out. (connect timeout=5.0)')': /bot<TOKEN>/getUpdates

链接到py文件以获取整个代码:https://github.com/TryingOutSomething/testing/blob/master/testing.py

1 个答案:

答案 0 :(得分:1)

您没有将功能添加到处理程序中。

在109行,您有以下内容:

# Help command
help_command = CommandHandler('help', help)
dispatcher.add_handler(help_command)

那就是添加功能help作为处理程序,以处理命令。您需要将功能也添加为处理程序,只需复制粘贴这些行,然后将'help'替换为您写给bot的内容,并将help替换为所需函数的名称就是这样。

示例:

# Ask my age command
the_age_command = CommandHandler('age', age)
dispatcher.add_handler(the_age_command)