This commit is contained in:
Jacob Henry 2018-09-13 00:32:33 -04:00
parent 8d1168d5eb
commit 5443b72d71
1 changed files with 19 additions and 11 deletions

30
main.py
View File

@ -2,7 +2,7 @@ from collections import OrderedDict
import google_api as google # For read drive import google_api as google # For read drive
from slackclient import SlackClient # Obvious from slackclient import SlackClient # Obvious
from slack_util import * import slack_util
import scroll_util import scroll_util
import identifier import identifier
@ -21,6 +21,9 @@ kill_switch_file = open("killswitch.txt", 'r')
kill_switch = next(kill_switch_file).strip() kill_switch = next(kill_switch_file).strip()
kill_switch_file.close() kill_switch_file.close()
# Enable to use dummy
DEBUG_MODE = True
def main(): def main():
wrapper = ClientWrapper() wrapper = ClientWrapper()
@ -46,33 +49,38 @@ def main():
# Add kill switch # Add kill switch
wrapper.add_hook(kill_switch, die) wrapper.add_hook(kill_switch, die)
# Add help
def list_hooks(slack, msg, match):
slack_util.reply(slack, msg, "\n".join(wrapper.hooks.keys()))
wrapper.add_hook("bot help", list_hooks)
wrapper.listen() wrapper.listen()
# Callback to list command hooks
# Callback to die
def die(*args): def die(*args):
print("Got kill switch") print("Got kill switch")
exit() exit()
DEBUG_MODE = False
class ClientWrapper(object): class ClientWrapper(object):
def __init__(self): def __init__(self):
# Init slack # Init slack
if DEBUG_MODE: if DEBUG_MODE:
self._slack = FakeClient() self.slack = FakeClient()
else: else:
self._slack = SlackClient(SLACK_API) self.slack = SlackClient(SLACK_API)
# Hooks go regex -> callback on (slack, msg, match) # Hooks go regex -> callback on (slack, msg, match)
self._hooks = OrderedDict() self.hooks = OrderedDict()
def add_hook(self, pattern, callback): def add_hook(self, pattern, callback):
self._hooks[pattern] = callback self.hooks[pattern] = callback
def listen(self): def listen(self):
feed = message_stream(self._slack) feed = slack_util.message_stream(self.slack)
for msg in feed: for msg in feed:
print(msg) print(msg)
@ -87,12 +95,12 @@ class ClientWrapper(object):
# Handle Message # Handle Message
text = msg['text'].strip() text = msg['text'].strip()
success = False success = False
for regex, callback in self._hooks.items(): for regex, callback in self.hooks.items():
match = re.match(regex, text, flags=re.IGNORECASE) match = re.match(regex, text, flags=re.IGNORECASE)
if match: if match:
success = True success = True
print("Matched on callback {}".format(callback)) print("Matched on callback {}".format(callback))
callback(self._slack, msg, match) callback(self.slack, msg, match)
break break
if not success: if not success: