Advertisement
disk6969

conv example

Jul 19th, 2023 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. class State(Enum):
  2.     WAIT_NAME = auto()
  3.     WAIT_AGE = auto()
  4.  
  5. conversation_state = {}
  6.  
  7. @bot.on(events.NewMessage(pattern='/start'))
  8. async def start(event):
  9.     await event.respond("Hi there! Please click the button below to provide your information.",
  10.                         buttons=[(Button.inline("Provide Information", b"provide_info"))])
  11.  
  12. @bot.on(events.CallbackQuery(data=b'provide_info'))
  13. async def handler(event):
  14.     who = event.sender_id
  15.  
  16.     # Starting a conversation
  17.     await event.respond('Hi! What is your name?')
  18.     conversation_state[who] = State.WAIT_NAME
  19.  
  20. @bot.on(events.NewMessage)
  21. async def handle_answers(event):
  22.     who = event.sender_id
  23.     state = conversation_state.get(who)
  24.  
  25.     if state == State.WAIT_NAME:
  26.         name = event.text  # Save the name wherever you want
  27.         await event.respond('Nice! What is your age?')
  28.         conversation_state[who] = State.WAIT_AGE
  29.  
  30.     elif state == State.WAIT_AGE:
  31.         age = event.text  # Save the age wherever you want
  32.         await event.respond(f'Thank you!')
  33.         # Conversation is done so we can forget the state of this user
  34.         del conversation_state[who]
  35.  
  36. bot.start()
  37. bot.run_until_disconnected()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement