From 6f9deef6815fcb4e8208a30e67304786b3d25ebc Mon Sep 17 00:00:00 2001 From: Jacob Henry Date: Wed, 28 Nov 2018 11:12:16 -0500 Subject: [PATCH] Made DM more reliable (now doesn't rely on a pre-existing DM channel) --- job_commands.py | 13 +++++++------ periodicals.py | 6 ++---- slack_util.py | 10 ---------- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/job_commands.py b/job_commands.py index 9c3c12d..f6dd1a9 100644 --- a/job_commands.py +++ b/job_commands.py @@ -21,13 +21,14 @@ def alert_user(slack: SlackClient, brother: scroll_util.Brother, saywhat: str) - DM a brother saying something. Wrapper around several simpler methods """ # We do this as a for loop just in case multiple people reg. to same scroll for some reason (e.g. dup accounts) + succ = False for slack_id in identifier.lookup_brother_userids(brother): - dm_id = slack_util.im_channel_for_id(slack, slack_id) - if dm_id: - # Give a dummy msg dict, since we won't actually be using anything in it - slack_util.send_message(slack, saywhat, dm_id) - else: - print("Warning: unable to find dm for brother {}".format(brother)) + slack_util.send_message(slack, saywhat, slack_id) + succ = True + + # Warn if we never find + if not succ: + print("Warning: unable to find dm for brother {}".format(brother)) T = TypeVar("T") diff --git a/periodicals.py b/periodicals.py index 159abee..e365b9d 100644 --- a/periodicals.py +++ b/periodicals.py @@ -85,11 +85,9 @@ class RemindJobs(slack_util.Passive): # For each, send them a DM success = False for slack_id in assignee_ids: - dm_id = slack_util.im_channel_for_id(slack, slack_id) msg = "{}, you still need to do {}".format(a.assignee.name, a.job.pretty_fmt()) - if dm_id: - success = True - slack_util.send_message(slack, msg, dm_id) + success = True + slack_util.send_message(slack, msg, slack_id) # Warn on failure if not success: diff --git a/slack_util.py b/slack_util.py index bc078fa..d3e2f2e 100644 --- a/slack_util.py +++ b/slack_util.py @@ -41,16 +41,6 @@ def send_message(slack: SlackClient, text: str, channel: str, thread: str = None return slack.api_call("chat.postMessage", **kwargs) -def im_channel_for_id(slack: SlackClient, user_id: str) -> Optional[str]: - conversations = slack.api_call("conversations.list", types="im") - if conversations["ok"]: - channels = conversations["channels"] - for channel in channels: - if channel["is_im"] and channel["user"] == user_id: - return channel["id"] - return None - - def message_stream(slack: SlackClient) -> Generator[dict, None, None]: """ Generator that yields messages from slack.