Made DM more reliable (now doesn't rely on a pre-existing DM channel)

This commit is contained in:
Jacob Henry 2018-11-28 11:12:16 -05:00
parent fe058e2ca8
commit 6f9deef681
3 changed files with 9 additions and 20 deletions

View File

@ -21,12 +21,13 @@ def alert_user(slack: SlackClient, brother: scroll_util.Brother, saywhat: str) -
DM a brother saying something. Wrapper around several simpler methods 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) # 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): for slack_id in identifier.lookup_brother_userids(brother):
dm_id = slack_util.im_channel_for_id(slack, slack_id) slack_util.send_message(slack, saywhat, slack_id)
if dm_id: succ = True
# Give a dummy msg dict, since we won't actually be using anything in it
slack_util.send_message(slack, saywhat, dm_id) # Warn if we never find
else: if not succ:
print("Warning: unable to find dm for brother {}".format(brother)) print("Warning: unable to find dm for brother {}".format(brother))

View File

@ -85,11 +85,9 @@ class RemindJobs(slack_util.Passive):
# For each, send them a DM # For each, send them a DM
success = False success = False
for slack_id in assignee_ids: 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()) msg = "{}, you still need to do {}".format(a.assignee.name, a.job.pretty_fmt())
if dm_id:
success = True success = True
slack_util.send_message(slack, msg, dm_id) slack_util.send_message(slack, msg, slack_id)
# Warn on failure # Warn on failure
if not success: if not success:

View File

@ -41,16 +41,6 @@ def send_message(slack: SlackClient, text: str, channel: str, thread: str = None
return slack.api_call("chat.postMessage", **kwargs) 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]: def message_stream(slack: SlackClient) -> Generator[dict, None, None]:
""" """
Generator that yields messages from slack. Generator that yields messages from slack.