slackpost: Updates for current email formatting
- Look for thread= lines - Support posting to both channels and private groups Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
19496e737c
commit
c5e15161bd
|
@ -35,3 +35,10 @@ def team():
|
||||||
|
|
||||||
def user():
|
def user():
|
||||||
return __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
|
||||||
|
|
|
@ -25,9 +25,6 @@ class Channel(threads.Thread):
|
||||||
def is_member(self):
|
def is_member(self):
|
||||||
return self.__member
|
return self.__member
|
||||||
|
|
||||||
def post(self, text):
|
|
||||||
chat.postMessage(self.__id, text)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def history(channel, timestamp):
|
def history(channel, timestamp):
|
||||||
|
|
|
@ -51,6 +51,6 @@ class Message:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def postMessage(channel, text):
|
def post_message(channel, text):
|
||||||
api.call("chat.postMessage", token = auth.token(), channel = channel,
|
api.call("chat.postMessage", token = auth.token(), channel = channel,
|
||||||
text = text, as_user = True, parse = "full")
|
text = text, as_user = True, parse = "full")
|
||||||
|
|
|
@ -63,8 +63,10 @@ class Thread:
|
||||||
self.do_mark_messages(m_list[-1].ts())
|
self.do_mark_messages(m_list[-1].ts())
|
||||||
return m_list
|
return m_list
|
||||||
|
|
||||||
|
|
||||||
def unread_count(self):
|
def unread_count(self):
|
||||||
if self.__unread == None:
|
if self.__unread == None:
|
||||||
self.fetch_info()
|
self.fetch_info()
|
||||||
return self.__unread
|
return self.__unread
|
||||||
|
|
||||||
|
def post(self, text):
|
||||||
|
chat.post_message(self.__id, text)
|
||||||
|
|
67
slackpost.py
67
slackpost.py
|
@ -6,31 +6,62 @@ import slack
|
||||||
import sys
|
import sys
|
||||||
from email.parser import Parser
|
from email.parser import Parser
|
||||||
|
|
||||||
|
|
||||||
headers = Parser().parsestr(sys.stdin.read())
|
headers = Parser().parsestr(sys.stdin.read())
|
||||||
|
|
||||||
if headers['from'] != "Anna Schumaker <schumaker.anna@gmail.com>":
|
|
||||||
|
#
|
||||||
|
# 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)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
# Determine which channel to post to
|
#
|
||||||
chan_id = headers["X-Slack-Channel"]
|
# Look for our marker text to help find thread id
|
||||||
channel = None
|
#
|
||||||
|
reply = "--- Reply above this line ---"
|
||||||
for c in slack.channels.list():
|
payload = headers.get_payload()
|
||||||
if c.id() == chan_id:
|
if payload.find(reply) == -1:
|
||||||
channel = c
|
print("Reply line missing!")
|
||||||
break
|
|
||||||
|
|
||||||
if channel == None:
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
split = headers.get_payload().split(reply)
|
||||||
|
|
||||||
lines = headers.get_payload().strip().split("\n")
|
|
||||||
for i in range(len(lines)):
|
|
||||||
if len(lines[i]) == 0:
|
|
||||||
continue
|
|
||||||
if lines[i][0] == ">":
|
|
||||||
break
|
|
||||||
|
|
||||||
lines = lines[0:i-1]
|
#
|
||||||
|
# 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 <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()
|
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)
|
channel.post(text)
|
||||||
|
|
Loading…
Reference in New Issue