diff --git a/identifier.py b/identifier.py index cd55087..92bbe82 100644 --- a/identifier.py +++ b/identifier.py @@ -119,7 +119,7 @@ def lookup_slackid_brother(slack_id: str) -> Optional[scroll_util.Brother]: return None -async def lookup_brother_userids(brother: scroll_util.Brother) -> List[str]: +def lookup_brother_userids(brother: scroll_util.Brother) -> List[str]: """ Returns a list of all userids associated with the given brother. diff --git a/main.py b/main.py index 9fc0e55..53785a2 100644 --- a/main.py +++ b/main.py @@ -57,6 +57,14 @@ def main() -> None: help_callback = management_commands.list_hooks_callback_gen(wrap.hooks) wrap.add_hook(slack_util.Hook(help_callback, pattern=management_commands.bot_help_pattern)) + # Schedule? + async def do_later(slk: SlackClient, msg: dict, match: typing.Match): + time = int(match.group(1)) + await asyncio.sleep(time) + slack_util.reply(slk, msg, "hello!") + + wrap.add_hook(slack_util.Hook(do_later, "pingme\s+(\d+)")) + event_loop = asyncio.get_event_loop() event_loop.run_until_complete(wrap.listen()) diff --git a/management_commands.py b/management_commands.py index 4da9866..a801f46 100644 --- a/management_commands.py +++ b/management_commands.py @@ -6,7 +6,7 @@ import channel_util import slack_util -async def list_hooks_callback_gen(hooks: List[slack_util.Hook]): +def list_hooks_callback_gen(hooks: List[slack_util.Hook]) -> slack_util.Callback: # noinspection PyUnusedLocal async def callback(slack, msg, match): slack_util.reply(slack, msg, "\n".join(hook.pattern for hook in hooks)) diff --git a/slack_util.py b/slack_util.py index 365b1c9..db7cc2f 100644 --- a/slack_util.py +++ b/slack_util.py @@ -1,10 +1,10 @@ -from time import sleep import re +from time import sleep +from typing import Any, Optional, Generator, Match, Callable, List, Coroutine from slackclient import SlackClient import channel_util -from typing import Any, Optional, Generator, Match, Callable, List, Awaitable, Coroutine """ Slack helpers. Separated for compartmentalization @@ -82,9 +82,13 @@ def message_stream(slack: SlackClient) -> Generator[dict, None, None]: print("Connection failed - retrying") +MsgAction = Coroutine[Any, Any, None] +Callback = Callable[[SlackClient, dict, Match], MsgAction] + + class Hook(object): def __init__(self, - callback: Coroutine[[SlackClient, dict, Match]], + callback: Callback, pattern: str = None, channel_whitelist: Optional[List[str]] = None, channel_blacklist: Optional[List[str]] = None):