#!/usr/bin/python # Copyright 2015 (c) Anna Schumaker. # import slack import sys 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: print("Reply not from user!") sys.exit(1) # # Look for our marker text to help find thread id # reply = "--- Reply above this line ---" payload = headers.get_payload() if payload.find(reply) == -1: print("Reply line missing!") sys.exit(1) split = headers.get_payload().split(reply) # # We should have a two element list. The thread id # is in the text at the start of the second element. # thread_id = split[-1].split()[0] if thread_id.find("thread=") == -1: print("Thread ID missing!") sys.exit(1) thread_id = thread_id[7:] # # 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: # # - 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() print(text) thread = slack.find_thread(thread_id) if thread == None: print("No such thread!") sys.exit(1) channel.post(text)