-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added helper functions for channel and user lists to make the API a bit ... #6
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,11 @@ class SlackClient(object): | |
|
||
def __init__(self, token): | ||
self.token = token | ||
self.channels = {} | ||
self.ul_by_id = {} | ||
self.ul_by_name = {} | ||
self.update_channel_lists_dict() | ||
self.update_user_lists_dicts() | ||
|
||
def _make_request(self, method, params): | ||
"""Make request to API endpoint | ||
|
@@ -26,7 +31,26 @@ def _make_request(self, method, params): | |
raise SlackError(result['error']) | ||
return result | ||
|
||
def chat_post_message(self, channel, text, **params): | ||
def channelname_to_channelid(self, channelname, update_channel_list=True): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function could use some work. I don't know about all of this "tries" stuff, and this while loop. I don't think it's necessary. |
||
tries = 0 | ||
|
||
if channelname[0] == 'C': | ||
return channelname | ||
|
||
while tries < 1: | ||
if channelname in self.channels: | ||
return self.channels[channelname]['id'] | ||
else: | ||
if tries == 0 and update_channel_list is True: | ||
self.update_channel_lists_dict() | ||
elif tries != 0: | ||
raise SlackError("channel not found") | ||
|
||
tries += 1 | ||
|
||
|
||
|
||
def chat_post_message(self, channel, text, username="cirtbot", **params): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "cirtbot"? Let's keep username optional, eh? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoops, sorry about that. |
||
"""chat.postMessage | ||
|
||
This method posts a message to a channel. | ||
|
@@ -35,12 +59,255 @@ def chat_post_message(self, channel, text, **params): | |
https://api.slack.com/methods/chat.postMessage | ||
""" | ||
method = 'chat.postMessage' | ||
|
||
params.update({ | ||
'channel': channel, | ||
'channel': self.channelname_to_channelid(channel), | ||
'text': text, | ||
'username': username | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
def chat_delete(self, channel, ts, **params): | ||
"""chat.delete | ||
|
||
Deletes a message by timestamp | ||
https://api.slack.com/methods/chat.delete | ||
""" | ||
|
||
method = 'chat.delete' | ||
params.update({ | ||
'channel': self.channelname_to_channelid(channel), | ||
'ts': ts, | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
def chat_update(self, channel, ts, text, **params): | ||
"""chat.update | ||
|
||
Updates a message by timestamp | ||
https://api.slack.com/methods/chat.update | ||
""" | ||
|
||
method = 'chat.update' | ||
params.update({ | ||
'channel': self.channelname_to_channelid(channel), | ||
'ts': ts, | ||
'text': text, | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
def channel_history(self, channel, count=10, do_prettify=True, **params): | ||
"""channels.history | ||
|
||
This method gets the history for a particular channel | ||
https://api.slack.com/methods/channels.history | ||
""" | ||
|
||
# Verify count is between 1 and 1000 per Slack documentation | ||
if count < 1 or count > 1000: | ||
raise SlackError("count parameter out of range (must be 1-1000)") | ||
|
||
method = 'channels.history' | ||
|
||
params.update({ | ||
'channel': self.channelname_to_channelid(channel), | ||
'count': count, | ||
}) | ||
|
||
ret = self._make_request(method, params) | ||
|
||
if do_prettify: | ||
for u in ret['messages']: | ||
if 'user' in u: | ||
u['user'] = self.ul_by_id[u['user']] | ||
|
||
return ret | ||
|
||
def channel_list(self, exclude_archived="1", **params): | ||
"""channels.list | ||
|
||
Gets a list of channels | ||
https://api.slack.com/methods/channels.list | ||
""" | ||
method = 'channels.list' | ||
|
||
params.update({ | ||
'exclude_archived': exclude_archived, | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
def update_channel_lists_dict(self): | ||
"""Updates the channel list dict""" | ||
|
||
cl = self.channel_list(exclude_archived=0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use full variable names? I know |
||
for c in cl['channels']: | ||
self.channels[c['name']] = c | ||
|
||
def users_list(self, **params): | ||
"""users.list | ||
|
||
Gets a list of users | ||
https://api.slack.com/methods/users.list | ||
""" | ||
method = 'users.list' | ||
|
||
return self._make_request(method, params) | ||
|
||
def update_user_lists_dicts(self): | ||
"""updates the user list dictionary to avoid repeated queries""" | ||
|
||
ul = self.users_list() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with these variables. |
||
for u in ul['members']: | ||
self.ul_by_id[u['id']] = u | ||
self.ul_by_name[u['name']] = u | ||
|
||
# Special Slackbottery | ||
self.ul_by_id[u'USLACKBOT'] = {u'status': None, u'profile':{}, u'name': 'Slackbot'} | ||
|
||
def auth_test(self, **params): | ||
"""auth.test | ||
|
||
Performs test of authentication | ||
https://api.slack.com/methods/auth.test | ||
""" | ||
|
||
method = "auth.test" | ||
return self._make_request(method, params) | ||
|
||
def channels_info(self, channel, **params): | ||
"""channels.info | ||
|
||
Gets info for a channel | ||
https://api.slack.com/methods/channels.info | ||
""" | ||
|
||
method = 'channels.info' | ||
params.update({ | ||
'channel' : self.channelname_to_channelid(channel) | ||
}) | ||
return self._make_request(method, params) | ||
|
||
def channels_invite(self, channel, user, **params): | ||
"""channels.invite | ||
|
||
Invite a user to a channel | ||
https://api.slack.com/methods/channels.invite | ||
""" | ||
|
||
method = 'channels.invite' | ||
params.update({ | ||
'channel': self.channelname_to_channelid(channel), | ||
'user': self.ul_by_name[user]['id'] | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
def channels_join(self, channel, **params): | ||
"""channels.join | ||
|
||
Joins a channel | ||
https://api.slack.com/methods/channels.join | ||
""" | ||
|
||
method = 'channels.join' | ||
params.update({ | ||
'name': self.channelname_to_channelid(channel), | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
def channels_leave(self, channel, **params): | ||
"""channels.leave | ||
|
||
Joins a channel | ||
https://api.slack.com/methods/channels.leave | ||
""" | ||
|
||
method = 'channels.leave' | ||
params.update({ | ||
'channel': self.channelname_to_channelid(channel), | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
def channels_mark(self, channel, ts, **params): | ||
"""channels.mark | ||
|
||
Moves the read curser in a channel | ||
https://api.slack.com/methods/channels.mark | ||
""" | ||
|
||
method = 'channels.mark' | ||
params.update({ | ||
'channel': self.channelname_to_channelid(channel), | ||
'ts': ts, | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
def channels_setPurpose(self, channel, purpose, **params): | ||
"""channels.setPurpose | ||
|
||
Set the purpose of a channel | ||
https://api.slack.com/methods/channels.setPurpose | ||
""" | ||
|
||
method = 'channels.setPurpose' | ||
params.update({ | ||
'channel': self.channelname_to_channelid(channel), | ||
'purpose': purpose, | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
def channels_setTopic(self, channel, topic, **params): | ||
"""channels.setTopic | ||
|
||
Sets the topic for a channel | ||
https://api.slack.com/methods/channels.setTopic | ||
""" | ||
|
||
method = 'channels.setTopic' | ||
params.update({ | ||
'channel': self.channelname_to_channelid(channel), | ||
'topic': topic, | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
def emoji_list(self, **params): | ||
"""emoji.list | ||
|
||
Get the list of emojis | ||
https://api.slack.com/methods/emoji.list | ||
""" | ||
|
||
method = 'emoji.list' | ||
|
||
return self._make_request(method, params) | ||
|
||
def stars_list(self, user, count=100, page=1, **params ): | ||
"""stars.list | ||
|
||
Get list of all stars by user | ||
https://api.slack.com/methods/stars.list | ||
""" | ||
|
||
method = 'stars.list' | ||
|
||
params.update({ | ||
'user': self.ul_by_name[user]['id'], | ||
'count': count, | ||
'page': page, | ||
}) | ||
|
||
return self._make_request(method, params) | ||
|
||
|
||
|
||
class SlackHandler(logging.Handler): | ||
"""A logging handler that posts messages to a Slack channel! | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constructor shouldn't be performing any implicit actions.
Perhaps move these two functions that make external calls to a separate function, such as
setup()
orinit()
that the user can call.