Telegram bot – How to handle conversations?
Question:
I am trying to learn to make a telegram bot but I am not sure how to achieve continuous conversations. All I know is how to respond to the individual messages, for example like this –
If a user enters wrong command, for example /jnaddaad
def unknown_response(update: Update, context: CallbackContext):
update.message.reply_text(
"Sorry I can't recognize you , you said '%s'" % update.message.text)
My use-case is simple –
- User enters his country using /addcountry command.
- After the country, I will as ask what city is he from, and he should be able to answer using /addcity command.
- After city, he should be able to enter the addresses(multiple) using /addaddresses
- I save everything in a database – username, country, city and addresses.
- User can update/delete one or more addresses.
Note:- User should not be directly able to enter city without country, and addresses with city. So the flow should be addcounty -> addcity -> addaddresses. And without the previous steps, user should not be able to access current steps.
I can probably be able to do 1 and 4. I just want a direction on how can I achieve the asked. Do I need to maintain a database with user and current user’s username and steps they have performed till now, or is it possible with python-telegram-bot?
Answers:
That should be achievable with just python-telegram-bot and the ConversationHandler.
The official docs for the ConversationHandler have a good explanation and also (more importantly) four fully working example bots.
I am trying to learn to make a telegram bot but I am not sure how to achieve continuous conversations. All I know is how to respond to the individual messages, for example like this –
If a user enters wrong command, for example /jnaddaad
def unknown_response(update: Update, context: CallbackContext):
update.message.reply_text(
"Sorry I can't recognize you , you said '%s'" % update.message.text)
My use-case is simple –
- User enters his country using /addcountry command.
- After the country, I will as ask what city is he from, and he should be able to answer using /addcity command.
- After city, he should be able to enter the addresses(multiple) using /addaddresses
- I save everything in a database – username, country, city and addresses.
- User can update/delete one or more addresses.
Note:- User should not be directly able to enter city without country, and addresses with city. So the flow should be addcounty -> addcity -> addaddresses. And without the previous steps, user should not be able to access current steps.
I can probably be able to do 1 and 4. I just want a direction on how can I achieve the asked. Do I need to maintain a database with user and current user’s username and steps they have performed till now, or is it possible with python-telegram-bot?
That should be achievable with just python-telegram-bot and the ConversationHandler.
The official docs for the ConversationHandler have a good explanation and also (more importantly) four fully working example bots.