Skip to content

Instantly share code, notes, and snippets.

@hyzhak
Last active September 3, 2016 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyzhak/b9adcc938abe9bfb4335cf31ef0abbee to your computer and use it in GitHub Desktop.
Save hyzhak/b9adcc938abe9bfb4335cf31ef0abbee to your computer and use it in GitHub Desktop.
architecture of stories - stateless and async
"""
v0.1.0
Bot asks user about destionation of space travelling.
- stateful story, it stores context of story (current question) in python interpreter
"""
@story.once('lets go!')
async def async_story(message):
dest = await ask_location(message['user'], text='Where do you go?')
store_destination(dest['location'])
origin = await ask_location(message['user'], text='Where do you now?')
store_origin(origin['location'])
await tell(message['user'], 'Thanks! Give me a minute I will find you right spaceship!')
"""
v0.1.1
Bot asks user about destionation of space travelling.
- stateful story, it stores context of story (current question) in python interpreter
"""
@story.once('lets go!')
async def async_story(message):
dest = await ask_location(message['user'], text='Where do you go?')
store_destination(dest['location'])
origin = await ask_location(message['user'], text='Where do you now?')
while origin['location'] in ['starts', 'planets]:
location = origin['location']
if location == 'stars':
#cycle back
origin = await ask_location(message['user'], text='Which star do you prefer?', then=receive_destination)
elif location == 'planets':
#cycle back
origin = await ask_location(message['user'], text='Which planet do you prefer?', then=receive_destination)
store_origin(origin['location'])
await tell(message['user'], 'Thanks! Give me a minute I will find you right spaceship!')
"""
v0.2.0
Bot asks user about destionation of space travelling.
- stateless story. it stores context of story (current question and results) somewhere (maybe DB)
"""
@story.once('lets go!')
def stateless_story(message):
return ask_location(message['user'], text='Where do you go?')
@story.than()
def than(message):
store_destination(message['location'])
return ask_location(message['user'], text='Where do you now?')
@story.than()
def than(message):
store_origin(message['location'])
return tell(message['user'], 'Thanks! Give me a minute I will find you right spaceship!')
"""
v0.2.1
Bot asks user about destionation of space travelling.
- stateless story. it stores context of story (current question and results) somewhere (maybe DB)
"""
@story.on('lets go!')
def stateless_story_with_bifurcation():
@story.then()
def request_destination(message):
return ask_location(message['user'], text='Where do you go?')
@story.then()
def receive_destination(message):
location = message['location]']
if location == 'stars':
#cycle back
return ask_location(message['user'], text='Which star do you prefer?', then=receive_destination)
elif location == 'planets':
#cycle back
return ask_location(message['user'], text='Which planet do you prefer?', then=receive_destination)
elif:
return choose_option(top10_planets,
text='Here is the most popular places. Maybe you would like to choose one?',
then=receive_destination_options)
else:
store_destination(message['location'])
return request_origin(message)
@story.then()
def receive_destination_options(message):
store_destination(message['location'])
return request_origin(message)
@story.then()
def request_origin(message):
return ask_location(message['user'], text='Where do you now?', topic='get-origin')
@story.then()
def receive_origin(message):
store_origin(message['location'])
return tell(message['user'], 'Thanks! Give me a minute I will find you right spaceship!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment