diff --git a/main.py b/main.py index 8b0ae68..5cd0efa 100644 --- a/main.py +++ b/main.py @@ -63,6 +63,9 @@ class ClientWrapper(object): else: self.slack = SlackClient(SLACK_API) + # For overriding output channel + self.debug_slack = slack_util.SlackDebugCondom(self.slack) + # Hooks go regex -> callback on (slack, msg, match) self.hooks = [] @@ -83,15 +86,24 @@ class ClientWrapper(object): continue # Handle Message - text = msg['text'].strip() + msg['text'] = msg['text'].strip() + + # If first few letters DEBUG, use debug slack + if msg['text'][:6] == "DEBUG ": + slack_to_use = self.debug_slack + msg['text'] = msg['text'][6:] + print("Debug handling \"{}\"".format(msg['text'])) + else: + slack_to_use = self.slack + success = False for hook in self.hooks: - if hook.check(self.slack, msg): + if hook.check(slack_to_use, msg): success = True break if not success: - print("No hit on {}".format(text)) + print("No hit on {}".format(msg['text'])) # run main diff --git a/slack_util.py b/slack_util.py index 795134e..8d3f111 100644 --- a/slack_util.py +++ b/slack_util.py @@ -1,5 +1,7 @@ from time import sleep import re +import channel_util +from typing import Any """ Slack helpers. Separated for compartmentalization @@ -23,6 +25,28 @@ def reply(slack, msg, text, in_thread=True, to_channel=None): slack.rtm_send_message(channel=to_channel, message=text) +class SlackDebugCondom(object): + def __init__(self, actual_slack): + self.actual_slack = actual_slack + + def __getattribute__(self, name: str) -> Any: + # Specialized behaviour + if name == "rtm_send_message": + # Flub some args + def override_send_message(*args, **kwargs): + print("Overriding: {} {}".format(args, kwargs)) + kwargs["channel"] = channel_util.BOTZONE + kwargs["thread"] = None + self.actual_slack.rtm_send_message(*args, **kwargs) + return override_send_message + else: + # Default behaviour. Try to give the self first, elsewise give the child + try: + return super(SlackDebugCondom, self).__getattribute__(name) + except AttributeError: + return self.actual_slack.__getattribute__(name) + + def message_stream(slack): """ Generator that yields messages from slack.