CuriousPerson
CuriousPerson

Reputation: 47

Telegram bot does nothing after one reply

I am new to python and trying to create a conversation bot on telegram that asks the user for their gender and age. However, my bot does not do anything after getting the age of the user. My functions for the gender and the age:

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

The command handler and message handler in the main function:

 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)

The log of the bot(Renamed the token in the log):

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

Link to py file for the whole code: https://github.com/TryingOutSomething/testing/blob/master/testing.py

Upvotes: 1

Views: 806

Answers (1)

LuRsT
LuRsT

Reputation: 4103

You're missing adding your functions to the handlers.

At line 109, you have this:

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

That's to add the function help as a handler, to handle commands. What you need is to add your functions as handlers too, you can just copy paste that those lines and replace 'help' with the thing you write to your bot, and help with the name of the function you want it to be.

Example:

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

Upvotes: 1

Related Questions