Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class State(Enum):
- WAIT_NAME = auto()
- WAIT_AGE = auto()
- conversation_state = {}
- @bot.on(events.NewMessage(pattern='/start'))
- async def start(event):
- await event.respond("Hi there! Please click the button below to provide your information.",
- buttons=[(Button.inline("Provide Information", b"provide_info"))])
- @bot.on(events.CallbackQuery(data=b'provide_info'))
- async def handler(event):
- who = event.sender_id
- # Starting a conversation
- await event.respond('Hi! What is your name?')
- conversation_state[who] = State.WAIT_NAME
- @bot.on(events.NewMessage)
- async def handle_answers(event):
- who = event.sender_id
- state = conversation_state.get(who)
- if state == State.WAIT_NAME:
- name = event.text # Save the name wherever you want
- await event.respond('Nice! What is your age?')
- conversation_state[who] = State.WAIT_AGE
- elif state == State.WAIT_AGE:
- age = event.text # Save the age wherever you want
- await event.respond(f'Thank you!')
- # Conversation is done so we can forget the state of this user
- del conversation_state[who]
- bot.start()
- bot.run_until_disconnected()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement