# # Copyright 2015 (c) Anna Schumaker. # from . import api from . import auth from . import chat from . import threads from . import users class Channel(threads.Thread): def __init__(self, json): threads.Thread.__init__(self, json) self.__member = json.get("is_member", False) def do_fetch_info(self): return info(self.id()) def is_member(self): return self.__member def post(self, text): chat.postMessage(self.__id, text) def read(self): if self.__last_ts == None: self.fetch_info() return history(self.__id, self.__last_ts) def history(channel, timestamp): ret = api.call("channels.history", token = auth.token(), channel = channel, oldest = timestamp) if ret["ok"] == False: return None # Read original message list o_list = [] for message in ret["messages"]: o_list += [ chat.Message(message) ] o_list.sort() # Merge together messages from the same user m_list = [ o_list[0] ] for msg in o_list[1:]: if msg.user() == m_list[-1].user(): m_list[-1].merge(msg) else: m_list += [ msg ] if len(m_list) > 0: mark(channel, m_list[-1].ts()) return m_list def info(channel): ret = api.call("channels.info", token = auth.token(), channel = channel) if ret["ok"] == False: return None return ret["channel"] def list(): ret = api.call("channels.list", token = auth.token()) if ret["ok"] == False: return None ch_list = [] for channel in ret["channels"]: ch_list += [ Channel(channel) ] return ch_list def mark(channel, timestamp): api.call("channels.mark", token = auth.token(), channel = channel, ts = timestamp)