diff options
author | Terje Larsen <terlar@gmail.com> | 2015-02-26 10:32:26 +0100 |
---|---|---|
committer | Terje Larsen <terlar@gmail.com> | 2015-08-26 09:13:42 +0200 |
commit | 23391bc78b6ea1c83c666afef54821d624ec5f7d (patch) | |
tree | 71af3ed2fd3a4d4bc9c9dfb0163c370591c22b63 /wee_slack.py | |
parent | de9054fdfaebde13af3765a87f495f28321ddb63 (diff) | |
download | wee-slack-23391bc78b6ea1c83c666afef54821d624ec5f7d.tar.gz |
Add Bot entity to be able to get the name
Right now all the bots are represented as their ID:s which is not very
useful, this fixes this.
This is my first python code so please let me know if something is
weird.
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/wee_slack.py b/wee_slack.py index bd314fc..21ce03f 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -126,6 +126,7 @@ class SlackServer(object): self.ws = None self.ws_hook = None self.users = SearchList() + self.bots = SearchList() self.channels = SearchList() self.connecting = False self.connected = False @@ -152,6 +153,9 @@ class SlackServer(object): self.users.append(user, user.get_aliases()) users.append(user, user.get_aliases()) + def add_bot(self, bot): + self.bots.append(bot) + def add_channel(self, channel): self.channels.append(channel, channel.get_aliases()) channels.append(channel, channel.get_aliases()) @@ -260,6 +264,9 @@ class SlackServer(object): for item in data["users"]: self.add_user(User(self, item["name"], item["id"], item["presence"], item["deleted"])) + for item in data["bots"]: + self.add_bot(Bot(self, item["name"], item["id"], item["deleted"])) + for item in data["channels"]: if "last_read" not in item: item["last_read"] = 0 @@ -771,6 +778,26 @@ class User(object): #reply = async_slack_api_request("im.open", {"channel":self.identifier,"ts":t}) async_slack_api_request(self.server.domain, self.server.token, "im.open", {"user": self.identifier, "ts": t}) +class Bot(object): + + def __init__(self, server, name, identifier, deleted=False): + self.server = server + self.name = name + self.identifier = identifier + self.deleted = deleted + + def __eq__(self, compare_str): + if compare_str == self.identifier or compare_str == self.name: + return True + else: + return False + + def __str__(self): + return "{}".format(self.identifier) + + def __repr__(self): + return "{}".format(self.identifier) + class Message(object): def __init__(self, message_json): @@ -1479,10 +1506,10 @@ def get_user(message_json, server): name = server.users.find(message_json['user']).name elif 'username' in message_json: name = u"-{}-".format(message_json["username"]) + elif 'bot_id' in message_json: + name = u"{}:]".format(server.bots.find(message_json["bot_id"]).name) elif 'service_name' in message_json: name = u"-{}-".format(message_json["service_name"]) - elif 'bot_id' in message_json: - name = u"-{}-".format(message_json["bot_id"]) else: name = u"" return name |