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:
parent
265522cb17
commit
867b1edb24
|
@ -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
|
||||
|
|
|
@ -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"]
|
||||
|
|
|
@ -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)
|
||||
|
|
58
slackpost.py
58
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 <whenever>" 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 <whenever>" text. Note that we
|
||||
# make the following assumptions:
|
||||
# Post the message to the thread
|
||||
#
|
||||
# - 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)
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue