diff --git a/channel_util.py b/channel_util.py index fa30010..8287e3f 100644 --- a/channel_util.py +++ b/channel_util.py @@ -1,13 +1,15 @@ - -import shelve import slack_util DB_NAME = "channel_priveleges" +# Useful channels +GENERAL = "C0CFHPNEM" +COMMAND_CENTER_ID = "GCR631LQ1" +SLAVES_TO_THE_MACHINE_ID = "C9WUQBYNP" +BOTZONE = "C3BF2MFKM" + # Define our patterns channel_check_pattern = r"channel id\s*(.*)" -# channel_check_pattern = r"channel id <#(.*)>" -# identify_other_pattern = r"<@(.*)>\s+has scroll\s+(.*)" def channel_check_callback(slack, msg, match): diff --git a/dummy.py b/dummy.py new file mode 100644 index 0000000..8bd43b1 --- /dev/null +++ b/dummy.py @@ -0,0 +1,22 @@ +import time +import channel_util + +msg = { + "type": "message", + "channel": channel_util.COMMAND_CENTER_ID, + "user": "U0Q1PKL92", + "text": "nagjobs tuesday", + "ts": "1355517523.000005" +} + + +class FakeClient(object): + def rtm_send_message(self, channel=None, message="", thread=None, to_channel=None): + print("Sent \"{}\" to channel {}".format(message, channel)) + + def rtm_connect(self, with_team_state=None, auto_reconnect=None): + return True + + def rtm_read(self): + time.sleep(4) + return [msg] diff --git a/job_nagger.py b/job_nagger.py index 3a76fcb..6738d98 100644 --- a/job_nagger.py +++ b/job_nagger.py @@ -1,27 +1,55 @@ import identifier import scroll_util import slack_util +import google_api +import channel_util -nag_pattern = r"nag (.*)" +nag_pattern = r"nagjobs (tuesday|thursday)" + + +SHEET_ID = "1lPj9GjB00BuIq9GelOWh5GmiGsheLlowPnHLnWBvMOM" +eight_jobs = "House Jobs!A2:C25" # Format: Job Day Bro +fiftythree_jobs = "House Jobs!E2:G6" + +# For matching purposes +tuesday = "tuesday" +thursday = "thursday" + + +class Job(object): + def __init__(self, row): + self.job_name, self.day, self.brother_name = row + self.day = self.day.lower().strip() + + def lookup_brother_slack_id(self): + brother_dict = scroll_util.find_by_name(self.brother_name) + return identifier.lookup_brother_userids(brother_dict) def nag_callback(slack, msg, match): - # Get who we want to nag - name = match.group(1) + # Only allow in - # Find them using scroll shit - brother = scroll_util.find_by_name(name) + # Get the day + day = match.group(1).lower() - # Get the associated user ids - ids = identifier.lookup_brother_userids(brother) + # Get the spreadsheet section + jobs = google_api.get_sheet_range(SHEET_ID, eight_jobs) + jobs = jobs + google_api.get_sheet_range(SHEET_ID, fiftythree_jobs) + jobs = [Job(r) for r in jobs] - # Nag them each - if ids: - result = "Hey" - for user_id in ids: - result += " <@{}>".format(user_id) - result += "!" - else: - result = "Nobody has identified themselves as {} ({})... Sad!".format(brother["name"], brother["scroll"]) + # Filter to day + jobs = [j for j in jobs if j.day == day] - slack_util.reply(slack, msg, result, in_thread=False) + # Nag each + response = "Do your jobs! They are as follows:\n" + for job in jobs: + response += "{} -- ".format(job.job_name) + ids = job.lookup_brother_slack_id() + if ids: + for id in ids: + response += "<@{}> ".format(id) + else: + response += "{} (scroll not found. Please register for @ notifications!)".format(job.brother_name) + response += "\n" + + slack_util.reply(slack, msg, response, in_thread=False, to_channel=channel_util.BOTZONE) diff --git a/main.py b/main.py index 882ca4b..e5f6619 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ import identifier import re import channel_util import job_nagger +from dummy import FakeClient # Read api token from file api_file = open("apitoken.txt", 'r') @@ -39,7 +40,7 @@ def main(): # Added channel utility wrapper.add_hook(channel_util.channel_check_pattern, channel_util.channel_check_callback) - # Add test nagging functionality + # Add nagging functionality wrapper.add_hook(job_nagger.nag_pattern, job_nagger.nag_callback) # Add kill switch @@ -53,10 +54,16 @@ def die(*args): exit() +DEBUG_MODE = False + + class ClientWrapper(object): def __init__(self): # Init slack - self._slack = SlackClient(SLACK_API) + if DEBUG_MODE: + self._slack = FakeClient() + else: + self._slack = SlackClient(SLACK_API) # Hooks go regex -> callback on (slack, msg, match) self._hooks = OrderedDict() @@ -73,6 +80,10 @@ class ClientWrapper(object): if msg.get("subtype") is not None: continue + # Never deal with general + if msg.get("channel") == channel_util.GENERAL: + continue + # Handle Message text = msg['text'].strip() success = False diff --git a/slack_util.py b/slack_util.py index f528cab..336a742 100644 --- a/slack_util.py +++ b/slack_util.py @@ -5,16 +5,19 @@ Slack helpers. Separated for compartmentalization """ -def reply(slack, msg, text, in_thread=True): +def reply(slack, msg, text, in_thread=True, to_channel=None): """ Sends message with "text" as its content to the channel that message came from """ - channel = msg['channel'] - thread_id = msg['ts'] + # If no channel specified, just do same as msg + if to_channel is None: + to_channel = msg['channel'] + + # Send in a thread by default if in_thread: - slack.rtm_send_message(channel=channel, message=text, thread=thread_id) + slack.rtm_send_message(channel=to_channel, message=text, thread=msg['ts']) else: - slack.rtm_send_message(channel=channel, message=text) + slack.rtm_send_message(channel=to_channel, message=text) def message_stream(slack):