rougeth / bottery

[DEVELOPMENT-HALTED] :battery: A bot framework with batteries included
MIT License
344 stars 51 forks source link

Async handlers #77

Closed nicoddemus closed 7 years ago

nicoddemus commented 7 years ago

Currently the handlers are not async functions. For simple interactions that is fine, but if the bot needs to do some I/O (accessing a database, consulting a website, etc) an async function would scale better.

nicoddemus commented 7 years ago

Btw, it should be possible to support both async and normal functions without users having to do anything in special:

import aiohttp

def greet(message):
    return f"Hello! {message.user}"

async def list_jobs(message):
    async with aiohttp.ClientSession() as session:
        response = await session.get('http://my-jenkins-server.org/api/list-jobs')
        return 'All jobs: {}'.format(response.json()['jobs'])

patterns = [
    Pattern('hello', greet),
    Pattern('list jobs', list_jobs),
]        

On our message handling loop, we can use inspect.isawaitable to decide if we want to await on the handler or call it directly.

guidiego commented 7 years ago

I want to learn more about AsyncIO, could I get this one?

nicoddemus commented 7 years ago

Sure! I wonder now if we should make all handlers async. The user would just need to add await, we can verify that he does and would make our implementation simpler. @rougeth what's your opinion here?

rougeth commented 7 years ago

I believe the benefits of accepting both kind of handlers is greater them just async handlers. :)