Changeset View
Changeset View
Standalone View
Standalone View
contrib/buildbot/slackbot.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
class SlackBot(): | class SlackBot: | ||||
def __init__(self, clientClass, token, channels): | def __init__(self, clientClass, token, channels): | ||||
self.client = clientClass(token=token) | self.client = clientClass(token=token) | ||||
self.channels = channels | self.channels = channels | ||||
def postMessage(self, channelIn, message): | def postMessage(self, channelIn, message): | ||||
channel = None | channel = None | ||||
if channelIn and channelIn[0] == 'U': | if channelIn and channelIn[0] == "U": | ||||
channel = channelIn | channel = channelIn | ||||
if channelIn in self.channels: | if channelIn in self.channels: | ||||
channel = self.channels[channelIn] | channel = self.channels[channelIn] | ||||
if not channel: | if not channel: | ||||
raise AssertionError( | raise AssertionError( | ||||
"Invalid channel: Channel must be a user ID or configured with a channel name") | "Invalid channel: Channel must be a user ID or configured with a" | ||||
" channel name" | |||||
) | |||||
self.client.chat_postMessage(channel=channel, text=message) | self.client.chat_postMessage(channel=channel, text=message) | ||||
def getUsers(self): | def getUsers(self): | ||||
# Note: users.list only returns up to 500 users. If this limit is exceeded, | # Note: users.list only returns up to 500 users. If this limit is exceeded, | ||||
# pagination will need to be implemented. | # pagination will need to be implemented. | ||||
users = self.client.users_list() | users = self.client.users_list() | ||||
return users['members'] | return users["members"] | ||||
def getUserByName(self, username): | def getUserByName(self, username): | ||||
# Note: The Slack API does NOT provide a way to search for users and | # Note: The Slack API does NOT provide a way to search for users and | ||||
# recommends the below approach. This is not ideal, but will suffice | # recommends the below approach. This is not ideal, but will suffice | ||||
# while we have a low user count in ABC slack. | # while we have a low user count in ABC slack. | ||||
users = self.getUsers() | users = self.getUsers() | ||||
for user in users: | for user in users: | ||||
if username in [user['profile'][nameAttribute] for nameAttribute in [ | if username in [ | ||||
'real_name', | user["profile"][nameAttribute] | ||||
'real_name_normalized', | for nameAttribute in [ | ||||
'display_name', | "real_name", | ||||
'display_name_normalized', | "real_name_normalized", | ||||
]]: | "display_name", | ||||
"display_name_normalized", | |||||
] | |||||
]: | |||||
return user | return user | ||||
return None | return None | ||||
def formatMentionByName(self, username): | def formatMentionByName(self, username): | ||||
user = self.getUserByName(username) | user = self.getUserByName(username) | ||||
if user: | if user: | ||||
return f"<@{user['id']}>" | return f"<@{user['id']}>" | ||||
return None | return None |