From 3027ec5ba7f259e3429c9ce91cc0803a617569e2 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 16 Nov 2016 15:52:42 -0500 Subject: [PATCH] Add support for fetching direct messages Signed-off-by: Anna Schumaker --- slack/__init__.py | 1 + slack/im.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++ slack/threads.py | 5 +++-- slackmail.py | 1 + 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 slack/im.py diff --git a/slack/__init__.py b/slack/__init__.py index 6a50cb7..24a67ce 100644 --- a/slack/__init__.py +++ b/slack/__init__.py @@ -12,6 +12,7 @@ from . import api from . import auth from . import channels from . import groups +from . import im from . import users diff --git a/slack/im.py b/slack/im.py new file mode 100644 index 0000000..66954b4 --- /dev/null +++ b/slack/im.py @@ -0,0 +1,52 @@ +# +# Copyright 2016 (c) Anna Schumaker. +# +from . import api +from . import auth +from . import threads +from . import users + +class IM(threads.Thread): + def __init__(self, json): + threads.Thread.__init__(self, json) + self.__uid = json.get("user") + self.__user = users.info(self.__uid) + + def name(self): + return "Chat With %s" % self.__user.name() + + def do_fetch_info(self): + return info(self.id(), self.__uid) + + def do_fetch_messages(self, ts): + return history(self.id(), ts) + + def do_mark_messages(self, ts): + mark(self.id(), ts) + + + +def history(im, timestamp): + ret = api.call("im.history", token = auth.token(), channel = im) + if ret["ok"] == False: + return None + return ret["messages"] + +def info(im, uid): + ret = api.call("im.open", token = auth.token(), user = uid, return_im = True) + if ret["ok"] == False: + return None + return ret["channel"] + +def list(): + ret = api.call("im.list", token = auth.token()) + if ret["ok"] == False: + return None + + im_list = [] + for im in ret["ims"]: + im_list += [ IM(im) ] + return im_list + +def mark(im, timestamp): + api.call("im.mark", token = auth.token(), channel = im, ts = timestamp) diff --git a/slack/threads.py b/slack/threads.py index eb13144..122e213 100644 --- a/slack/threads.py +++ b/slack/threads.py @@ -16,8 +16,8 @@ class Thread: def __init__(self, json): self.__id = json.get("id", 0) self.__name = json.get("name", "").title() - self.__topic = json["topic"].get("value", "").encode("utf-8").decode(errors="replace") - self.__purpose = json["purpose"].get("value", "").encode("utf-8").decode(errors="replace") + self.__topic = json.get("topic", {}).get("value", "").encode("utf-8").decode(errors="replace") + self.__purpose = json.get("purpose", {}).get("value", "").encode("utf-8").decode(errors="replace") self.__unread = json.get("unread_count", None) self.__last_ts = json.get("last_read", None) @@ -32,6 +32,7 @@ class Thread: if json != None: self.__unread = json.get("unread_count", None) self.__last_ts = json.get("last_read", None) + self.__id = json.get("id", 0) def id(self): return self.__id diff --git a/slackmail.py b/slackmail.py index 8c3bece..c952b12 100644 --- a/slackmail.py +++ b/slackmail.py @@ -38,4 +38,5 @@ def mail_threads(thread_list): mail_threads(slack.channels.list()) mail_threads(slack.groups.list()) +mail_threads(slack.im.list()) smtp.quit()