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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-11-17 10:28:53 -05:00
parent 265522cb17
commit 867b1edb24
4 changed files with 34 additions and 42 deletions

View File

@ -42,8 +42,10 @@ def user():
return __USER return __USER
def find_thread(thread_id): def find_thread(thread_id):
[ res ] = [ c for c in channels.list() if c.id() == thread_id ] or [ None ] for c in channels.list():
if res: if c.id() == thread_id: return c
return res for g in groups.list():
[ res ] = [ g for g in groups.list() if g.id() == thread_id ] or [ None ] if g.id() == thread_id: return g
return res for i in im.list():
if i.id() == thread_id: return i
return None

View File

@ -87,5 +87,5 @@ class Message:
def post_message(channel, text): def post_message(channel, text):
api.call("chat.postMessage", token = auth.token(), channel = channel, return api.call("chat.postMessage", token = auth.token(), channel = channel,
text = text, as_user = True, parse = "full") text = text, as_user = True, parse = "full")["ok"]

View File

@ -74,4 +74,4 @@ class Thread:
return self.__unread return self.__unread
def post(self, text): def post(self, text):
chat.post_message(self.__id, text) return chat.post_message(self.__id, text)

View File

@ -1,68 +1,58 @@
#!/usr/bin/python #!/usr/bin/python
# Copyright 2015 (c) Anna Schumaker. # Copyright 2015 (c) Anna Schumaker.
# #
import re
import slack import slack
import sys import sys
import quopri
from email.parser import Parser from email.parser import Parser
headers = Parser().parsestr(sys.stdin.read()) headers = Parser().parsestr(sys.stdin.read())
# #
# Only allow replies that come from the slack user's email address. # Only allow replies that come from the slack user's email address.
# #
sender = headers["sender"].replace(".", "") sender = headers.get("sender", headers.get("from"))
target = slack.user().email().replace(".", "") match = re.search("<(.*?)@(.*?)>", sender)
if sender != target: addr = match.group(0)[1:-1] if match else sender
if addr.replace(".", "") != slack.user().email().replace(".", ""):
print("Reply not from user!") print("Reply not from user!")
sys.exit(1) 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 ---" reply = "--- Reply above this line ---(\s*?)thread=(.*?)\n"
payload = headers.get_payload() match = re.search(reply, headers.get_payload())
if payload.find(reply) == -1: if match == None:
print("Reply line missing!") print("Reply line missing!")
sys.exit(1) 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 # Parse the message payload to find the text that was sent.
# is in the text at the start of the second element. # Note that we make the following assumptions:
# #
quoted = quopri.decodestring(split[-1]).decode() # - Quoted text will always begin with ">"
thread_id = quoted.split()[0] # - The email reply will always have "Reply sent on <whenever>" text
if thread_id.find("thread=") == -1: #
print("Thread ID missing!") message = headers.get_payload()
sys.exit(1) message = re.sub(">(.*?)\n", "", message).rstrip()
thread_id = thread_id[7:] split = message.split("\n")
message = "\n".join(split[:-1]).rstrip()
# #
# Loop over each line of the reply, strip out special characters, # Post the message to the thread
# and remove any "Reply sent on <whenever>" text. Note that we
# make the following assumptions:
# #
# - The email reply will always have "Reply sent on <whenever>" 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) thread = slack.find_thread(thread_id)
if thread == None: if thread == None:
print("No such thread: %s!" % thread_id) print("No such thread: %s!" % thread_id)
sys.exit(1) sys.exit(1)
thread.post(text) if thread.post(message) == False:
print("Error posting to thread: %s" % thread_id)
sys.exit(1)