From 867b1edb245def43a726aa03f3143ba1b0dd3200 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 17 Nov 2016 10:28:53 -0500 Subject: [PATCH] Improve slackpost script I also enable posting to IMs and return any error that we receive when attempting to post. Signed-off-by: Anna Schumaker --- slack/__init__.py | 12 ++++++---- slack/chat.py | 4 ++-- slack/threads.py | 2 +- slackpost.py | 58 ++++++++++++++++++++--------------------------- 4 files changed, 34 insertions(+), 42 deletions(-) diff --git a/slack/__init__.py b/slack/__init__.py index 24a67ce..473a9e3 100644 --- a/slack/__init__.py +++ b/slack/__init__.py @@ -42,8 +42,10 @@ def user(): return __USER def find_thread(thread_id): - [ res ] = [ c for c in channels.list() if c.id() == thread_id ] or [ None ] - if res: - return res - [ res ] = [ g for g in groups.list() if g.id() == thread_id ] or [ None ] - return res + for c in channels.list(): + if c.id() == thread_id: return c + for g in groups.list(): + if g.id() == thread_id: return g + for i in im.list(): + if i.id() == thread_id: return i + return None diff --git a/slack/chat.py b/slack/chat.py index a468ce1..720a5d0 100644 --- a/slack/chat.py +++ b/slack/chat.py @@ -87,5 +87,5 @@ class Message: def post_message(channel, text): - api.call("chat.postMessage", token = auth.token(), channel = channel, - text = text, as_user = True, parse = "full") + return api.call("chat.postMessage", token = auth.token(), channel = channel, + text = text, as_user = True, parse = "full")["ok"] diff --git a/slack/threads.py b/slack/threads.py index 122e213..a41c53e 100644 --- a/slack/threads.py +++ b/slack/threads.py @@ -74,4 +74,4 @@ class Thread: return self.__unread def post(self, text): - chat.post_message(self.__id, text) + return chat.post_message(self.__id, text) diff --git a/slackpost.py b/slackpost.py index ee3fcd3..b2d294c 100755 --- a/slackpost.py +++ b/slackpost.py @@ -1,68 +1,58 @@ #!/usr/bin/python # Copyright 2015 (c) Anna Schumaker. # - +import re import slack import sys -import quopri from email.parser import Parser - headers = Parser().parsestr(sys.stdin.read()) # # Only allow replies that come from the slack user's email address. # -sender = headers["sender"].replace(".", "") -target = slack.user().email().replace(".", "") -if sender != target: +sender = headers.get("sender", headers.get("from")) +match = re.search("<(.*?)@(.*?)>", sender) +addr = match.group(0)[1:-1] if match else sender +if addr.replace(".", "") != slack.user().email().replace(".", ""): print("Reply not from user!") sys.exit(1) # -# Look for our marker text to help find thread id +# Find the thread id in the message payload # -reply = "--- Reply above this line ---" -payload = headers.get_payload() -if payload.find(reply) == -1: +reply = "--- Reply above this line ---(\s*?)thread=(.*?)\n" +match = re.search(reply, headers.get_payload()) +if match == None: print("Reply line missing!") sys.exit(1) -split = headers.get_payload().split(reply) + +thread_id = match.group(0).rsplit("=")[-1].rstrip() # -# We should have a two element list. The thread id -# is in the text at the start of the second element. +# Parse the message payload to find the text that was sent. +# Note that we make the following assumptions: # -quoted = quopri.decodestring(split[-1]).decode() -thread_id = quoted.split()[0] -if thread_id.find("thread=") == -1: - print("Thread ID missing!") - sys.exit(1) -thread_id = thread_id[7:] +# - Quoted text will always begin with ">" +# - The email reply will always have "Reply sent on " text +# +message = headers.get_payload() +message = re.sub(">(.*?)\n", "", message).rstrip() +split = message.split("\n") +message = "\n".join(split[:-1]).rstrip() # -# Loop over each line of the reply, strip out special characters, -# and remove any "Reply sent on " text. Note that we -# make the following assumptions: +# Post the message to the thread # -# - The email reply will always have "Reply sent on " text -# - There will always be a blank line between content and replied-on text -# -lines = [] -for line in split[0].split("\n"): - lines += [ line.strip() ] -while lines[-1] != "": - lines.pop(len(lines) - 1) -text = ' '.join(lines).strip() - - thread = slack.find_thread(thread_id) if thread == None: print("No such thread: %s!" % thread_id) sys.exit(1) -thread.post(text) +if thread.post(message) == False: + print("Error posting to thread: %s" % thread_id) + sys.exit(1)