简体   繁体   中英

How to implement a Telegram bot program that could handle multiple steps of input?

I'm programming a telegram bot in Python3 using the Telegram bot API. I'm facing the problem of handling requests that need multiple steps to couplet. For example, for an airline search bot:

  1. the bot ask for departure city,
  2. the user input a name,
  3. the bot ask for destination,
  4. the user input another name,
  5. after a bunch of questions, the bot returns the result to the user.

What can I do?

You need to have a question tree that users can traverse it (you can use a linked list for it and save this tree to the database).

For each question, there is a method that takes some actions (like storing some data in the database) and sends a question/result to the user. Each user has a CurrentState that contain state of the user in the question tree. The user sent an answer to and the bot will run corresponding method and responds to the user.

method = //Fetch user CurrentState from db e.g. airlineBot.doSomething
method(TelegramMessage)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM