Q&A

How to store user's second message after receiving user's first message under certain condition?

Pre condition, Condition to reproduce

I want to store user's second or third messages by the first text that the user send. For exmaple, if the user type "food", the bot would ask "what kind of food?", and the user could reply as like "noodle, rice, or cake...etc". And the bot would response the price of the food who type. However, I want the user get the price through typing "food" then typing "noodle". I dont want the price show up, if the user directly typing "noodle". Like level by level.

Issue/ Error Message details

I try to call the second @handler.add under the main @handler.add function, however, I found that the program would stay in the second @handler.function, and I could not jump back to the first level of @handler.add. Codes Like below.

Accompanying respective source code

@handler.add(MessageEvent, message=TextMessge): def handle_message(event):

umsg = event.message.text
if umsg == "food":
	get_price(umsg)
if umsg == "drink":
	get_priced(umsg)

def get_price(umsg):

@handler.add(MessageEvent, message=TextMessge):
	def pricein(event):
	
					item = event.message.text
					if item == "xxx":
						...
						...
						...

Resolution Tried

After the code running to the second @handler.add which decorates (def pricein), the code would not jump up to the first level of @handler.add where I want the user could start over from typing "food" or "drink".

Reference information (Version / Platform/ environment)

Python 3.9

  • 0
  • 1
  • 299
  • twitter facebook

How about using database like mysql or postgresql to store previous message? Of course any database like redis is enough.

  1. you call db.put(userid, message) to store message related to a user when the user sent message.
  2. you can get previous message by calling db.get(userid) when user sends second message. If the previous message is food, you can continue processing you want.
  • 2

Similar posts

    No similar posts

Are you sure? question.vm