pdanchenko
pdanchenko

Reputation: 214

Telegram bot: how to wait for user replies and process them one by one?

I want to create a telegram bot to predict football scores with my friends. So I have a list of fixtures (e.g. ["Man Utd vs Spurs", "Liverpool vs Man City", "Arsenal vs Chelsea"]).

I want to send these fixtures to a user one by one. So it should look like this:

user_message: /prediction

bot_message: fixture1
user_message: score1
bot_message: fixture2
user_message: score2
...
bot_message: fixture10
user_message: score10

bot_message: Predictions saved!

But I can't understand how to make a bot wait for a user's reply. I tried to use register_next_step_handler but it doesn't work as I expected.

for fixture in prediction.schedule:
        bot.send_message(message.chat.id, fixture["fixture_name"])
        bot.register_next_step_handler(message, make_prediction)
        ...

It firstly sends all the messages (10 in my case) and then executes make_prediction function 10 times after just one user message.

This is how it works now:

https://i.sstatic.net/AbkYQ.jpg

Upvotes: 0

Views: 555

Answers (1)

Rigorich
Rigorich

Reputation: 74

One of solutions:
I suppose you will store user's predictions somewhere in array
Initially all of user's predictions are empty — filled with null value

When user call /prediction command, you start end:

  1. if there is no null elements in array, goto 7
  2. find null element in array
  3. ask user to make prediction to this element
  4. handle user's answer
  5. put answer instead this null
  6. goto 1
  7. send message that there is no more available predictions

So, to user this dialog will look like this:

> /prediction
# Foo vs Bar ?
> Foo
# A vs B ?
> I bet my house to A!
# Thanks for your predictions!

Upvotes: 0

Related Questions