Add support for fetching direct messages

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-11-16 15:52:42 -05:00
parent 18aa2a5375
commit 3027ec5ba7
4 changed files with 57 additions and 2 deletions

View File

@ -12,6 +12,7 @@ from . import api
from . import auth
from . import channels
from . import groups
from . import im
from . import users

52
slack/im.py Normal file
View File

@ -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)

View File

@ -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

View File

@ -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()