diff --git a/slack/__init__.py b/slack/__init__.py index 7dd929b..31bc673 100644 --- a/slack/__init__.py +++ b/slack/__init__.py @@ -11,6 +11,7 @@ os.chdir(os.path.dirname(path)) from . import api from . import auth from . import channels +from . import groups from . import users diff --git a/slack/channels.py b/slack/channels.py index 9194729..c0f16b8 100644 --- a/slack/channels.py +++ b/slack/channels.py @@ -12,17 +12,9 @@ class Channel(threads.Thread): def __init__(self, json): threads.Thread.__init__(self, json) self.__member = json.get("is_member", False) - self.__unread = json.get("unread_count", None) - self.__last_ts = json.get("last_read", None) - def fetch_info(self): - json = info(self.__id) - if json != None: - self.__unread = json.get("unread_count", None) - self.__last_ts = json.get("last_read", None) - - def id(self): - return self.__id + def do_fetch_info(self): + return info(self.id()) def is_member(self): return self.__member @@ -30,11 +22,6 @@ class Channel(threads.Thread): def post(self, text): chat.postMessage(self.__id, text) - def unread_count(self): - if self.__unread == None: - self.fetch_info() - return self.__unread - def read(self): if self.__last_ts == None: self.fetch_info() diff --git a/slack/groups.py b/slack/groups.py new file mode 100644 index 0000000..8de0ae5 --- /dev/null +++ b/slack/groups.py @@ -0,0 +1,32 @@ +# +# Copyright 2015 (c) Anna Schumaker. +# +from . import api +from . import auth +from . import threads + + +class Group(threads.Thread): + def __init__(self, json): + threads.Thread.__init__(self, json) + + def do_fetch_info(self): + return info(self.id()) + + + +def info(group): + ret = api.call("groups.info", token = auth.token(), channel = group) + if ret["ok"] == False: + return None + return ret["group"] + +def list(): + ret = api.call("groups.list", token = auth.token()) + if ret["ok"] == False: + return None + + gr_list = [] + for group in ret["groups"]: + gr_list += [ Group(group) ] + return gr_list diff --git a/slack/threads.py b/slack/threads.py index 9a10fd4..271d3d2 100644 --- a/slack/threads.py +++ b/slack/threads.py @@ -3,12 +3,20 @@ # +# +# This class represents a slack "thread", which could be a channel, group, +# or chat. Note that child classes need to implement the following methods: +# +# - do_fetch_info(): Call .info to find unread count and timestamp. +# 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("latin-1") self.__purpose = json["purpose"].get("value", "").encode("utf-8").decode("latin-1") + self.__unread = json.get("unread_count", None) + self.__last_ts = json.get("last_read", None) def __str__(self): topic="" @@ -16,5 +24,22 @@ class Thread: topic = "\nTopic: %s" % self.__topic return "%s: %s%s" % (self.__name, self.__purpose, topic) + def fetch_info(self): + json = self.do_fetch_info() + if json != None: + self.__unread = json.get("unread_count", None) + self.__last_ts = json.get("last_read", None) + + def id(self): + return self.__id + + def is_member(self): + return True + def name(self): return self.__name + + def unread_count(self): + if self.__unread == None: + self.fetch_info() + return self.__unread