Using asyncio to handle messages as futures
This commit is contained in:
parent
08437e7a1c
commit
bd3d948e8c
1 changed files with 11 additions and 12 deletions
|
@ -1,12 +1,11 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import traceback
|
import traceback
|
||||||
import urllib.parse
|
|
||||||
|
|
||||||
from quart import Quart, request
|
from quart import Quart, request
|
||||||
import structlog
|
import structlog
|
||||||
|
|
||||||
import butterrobot.logging
|
import butterrobot.logging # noqa
|
||||||
from butterrobot.config import SLACK_TOKEN, LOG_LEVEL, ENABLED_PLUGINS
|
from butterrobot.config import ENABLED_PLUGINS
|
||||||
from butterrobot.objects import Message
|
from butterrobot.objects import Message
|
||||||
from butterrobot.plugins import get_available_plugins
|
from butterrobot.plugins import get_available_plugins
|
||||||
from butterrobot.platforms import PLATFORMS
|
from butterrobot.platforms import PLATFORMS
|
||||||
|
@ -22,6 +21,12 @@ enabled_plugins = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_message(platform: str, message: Message):
|
||||||
|
for plugin in enabled_plugins:
|
||||||
|
async for response_message in plugin.on_message(message):
|
||||||
|
asyncio.ensure_future(available_platforms[platform].methods.send_message(response_message))
|
||||||
|
|
||||||
|
|
||||||
@app.before_serving
|
@app.before_serving
|
||||||
async def init_platforms():
|
async def init_platforms():
|
||||||
for platform in PLATFORMS.values():
|
for platform in PLATFORMS.values():
|
||||||
|
@ -31,7 +36,7 @@ async def init_platforms():
|
||||||
available_platforms[platform.ID] = platform
|
available_platforms[platform.ID] = platform
|
||||||
logger.info("platform setup completed", platform=platform.ID)
|
logger.info("platform setup completed", platform=platform.ID)
|
||||||
except platform.PlatformInitError as error:
|
except platform.PlatformInitError as error:
|
||||||
logger.error(f"platform init error", error=error, platform=platform.ID)
|
logger.error("Platform init error", error=error, platform=platform.ID)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/<platform>/incoming", methods=["POST"])
|
@app.route("/<platform>/incoming", methods=["POST"])
|
||||||
|
@ -48,7 +53,7 @@ async def incoming_platform_message_view(platform, path=None):
|
||||||
return response.data, response.status_code
|
return response.data, response.status_code
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Error parsing message",
|
"Error parsing message",
|
||||||
platform=platform,
|
platform=platform,
|
||||||
error=error,
|
error=error,
|
||||||
traceback=traceback.format_exc(),
|
traceback=traceback.format_exc(),
|
||||||
|
@ -58,13 +63,7 @@ async def incoming_platform_message_view(platform, path=None):
|
||||||
if not message or message.from_bot:
|
if not message or message.from_bot:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
for plugin in enabled_plugins:
|
asyncio.ensure_future(handle_message(platform, message))
|
||||||
if result := await plugin.on_message(message):
|
|
||||||
if isinstance(result, Message):
|
|
||||||
result = [result]
|
|
||||||
|
|
||||||
for out_message in result:
|
|
||||||
await available_platforms[platform].methods.send_message(out_message)
|
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue