Skip to main content

Async framework for bots

Project description

# State [![support python versions](]( [![PyPI version](]( [![Build Status](]( [![Coverage Status](]( [![](](

Under active development. API could change so please follow for changes.

# Get Started

import botstory
from botstory.integrations import aiohttp, fb, mongodb
import os

story = botstory.Story()

# setup modules

# facebook integration
webhook_url='/webhook{}'.format(os.environ.get('FB_WEBHOOK_URL_SECRET_PART', '')),
webhook_token=os.environ.get('FB_WEBHOOK_TOKEN', None),

# http interface
port=int(os.environ.get('PORT', 8080)),

# db interface
uri=os.environ.get('MONGODB_URI', 'mongo'),
db_name=os.environ.get('MONGODB_DB_NAME', 'todobot'),

# match user queries
def hello_story():
async def say_hi(ctx):
await story.say('Hello {}!'.format(ctx['user'].name))

# start service



# Features

- **Use frontier features of Python**
*Botstory is heavily based on brand new Python features, like async/await.
We always focus on its evolution and don't support legacy versions (<3.5).*

- **All messengers in one place**
*Right now botstory supports only fb Messengers, but we are going to
support many others. So you can use common code base for different
platform and seamlessly organize communication between them. As well
we always open for contribution. And if you sure that one platform
should be here, just drop PR with unit tests.*

- **More than 90% test coverage**
*Code is covered by tests except few trivial places.
That way we are almost sure that any changes won't break other
functionality. And any breaking features won't come hidden.*

- **Declarative user dialogs**
*Match user queries, lead, fork and loop dialogs.*

- **Store use session**
*We remember user context and could return it any time and on any
app instance. So you can scale app, and start and stop it without
fair of bot amnesia.*

- **Easily scale**
*We focus on micro-services and especially on Docker. So you can
very easy scale any part of the system.*

- **Open source**
*MIT licence gives you many rights. And we are very open for your
contribution and would love to hear about your experience.*

and many other features

# News and Changelog


# Background

We are developing botstory mindful of the need to have easily reading API
which describe dialogues (scenario) of bots in Python language.
Key problem is async nature of any dialog -
we can wait answer from user for are months and should store context
until that. As well dialog structure should be simply and clear
and show sequence of questions and reactions.

Sure dialog can be made in diagrams but my thought that code should
clear enough to show story of dialog and should be open for modification.

As well it is too hard to use git for store versions of diagrams.

# Install

pip install botstory

# Examples

- [TODO bot]( - support fb messenger

## Linear dialog

Bot asks user about destination of space travelling.
- stateless story. it stores context of story (current question and results) somewhere (maybe DB)
@story.on('lets go!')
def stateless_story():
async def ask_destination(ctx):
return await story.ask('Where do you go?',

async def ask_origin(ctx):
return await story.ask('Where do you now?',

async def thanks(ctx):
return await story.say('Thanks!\n'
'Give me a minute I will find you right spaceship!',

## Forking of Dialog (bifurcations)

Bot asks user about destination 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():
async def request_destination(ctx):
return await story.ask('Where do you go?',
def stars():
async def receive_destination_options(ctx):
return await story.ask('Which star do you prefer?',
def planets():
async def request_origin(ctx):
return await story.ask('Which planet do you prefer?',
def other():
async def choose_from_top10_planets(ctx):
return await choose_option(top10_planets,
text='Here is the most popular places. Maybe you would like to choose one?',

async def receive_destination(ctx):
return await story.say('Thanks! Give me a minute I will find you right spaceship!',


## Reuse parts of Dialog (callable) and Dialog Loops


from botstory.ast import callable, loop, story_context
from botstory.middlewares import option, sticker, text
import emoji
import logging
from todo import reflection

# Loop version
async def show_list_next_page(ctx):
user_data = story_context.get_user_data(ctx)
page_index = user_data.get('page_index', 0)
list_title = user_data['list_title']
title_field = user_data['title_field']
page_length = user_data['page_length']
list_type = user_data.get('list_type', 'pure')
# render page of list ....

def pagination_loop():
async def show_zero_page(ctx):
if not await _show_list_next_page(ctx):
return callable.EndOfStory()

def list_loop():
def next_page():
async def show_part_of_list(ctx):
if not await show_list_next_page(ctx):
return loop.BreakLoop()
return None

def list_of_tasks_story():
async def list_of_tasks(ctx):'list of tasks')
return await pagination_list.pagination_loop(
list_title='List of actual tasks:',
page_length=os.environ.get('LIST_PAGE_LENGTH', 4),


# License


# Contributing

Contributions of all sizes are welcome.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

botstory-0.1.5.tar.gz (66.1 kB view hashes)

Uploaded source

Built Distribution

botstory-0.1.5-py3-none-any.whl (98.6 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page