def main(): cfg = read_config() slack = WebClient(token=cfg['oauth_token']) plmw_channel_id = get_plmw_channel_id(slack) # set(id): Slack IDs for all members of #plmw plmw_channel_members = get_plmw_channel_members(slack, plmw_channel_id) # dict(email, id): All users on Slack all_slack_users = get_all_slack_users(slack) # set(email): People who should be on #plmw plmw_emails = set(read_plmw_emails(cfg['plmw_emails'])) # set(id): Slack IDs of people who should be on #plmw may_add_to_plmw = { all_slack_users[email] for email in plmw_emails if email in all_slack_users } # set(id): Slack IDs of people who are not on #plmw, but should be will_add_to_plmw = may_add_to_plmw - plmw_channel_members # Sanity check if len(will_add_to_plmw) > 0: print("Email addresses of people we are going to add:") all_slack_users_inv = {v: k for k, v in all_slack_users.items()} for id in will_add_to_plmw: print(all_slack_users_inv[id]) else: print("Nobody to add") return if len(sys.argv) == 2 and sys.argv[1] == "side-effect": slack.conversations_invite(channel=plmw_channel_id, users=','.join(will_add_to_plmw)) else: print("Use ./plmwbot.py side-effect to actually add these people") return
def user_add(channel, users): """ Invite users to join a slack channel. The bot must be a member of the channel. :param channel: The identifier of the Slack channel to invite the users to :param users: The identifiers of the specified users (List of up to 1000) :return: Response object (Dictionary) """ if not settings.SLACK_TOKEN: return {'ok': False, 'error': 'config_error'} client = WebClient(token=settings.SLACK_TOKEN) try: response = client.conversations_invite(channel=channel, users=users) assert response['ok'] is True return {'ok': response['ok']} except SlackApiError as e: assert e.response['ok'] is False return e.response
class Slack: def __init__(self, token, channel_name=None, channel_id=None): self.client = WebClient(token) self.channel_id = channel_id channels = self.client.conversations_list( types='public_channel,private_channel') if channel_name: for channel in channels['channels']: if channel['name'] == channel_name: self.channel_id = channel['id'] break if not self.channel_id: self.channel_id = self.client.conversations_create( name=channel_name.lower(), is_private=True)['channel']['id'] admins = [ u['id'] for u in self.client.users_list()['members'] if u.get('is_admin') or u.get('is_owner') ] self.client.conversations_invite(channel=self.channel_id, users=admins) def send_snippet(self, title, initial_comment, code, code_type='python', thread_ts=None): return self.client.files_upload( channels=self.channel_id, title=title, initial_comment=initial_comment.replace('<br>', ''), content=code, filetype=code_type, thread_ts=thread_ts)['ts'] def send_exception_snippet(self, domain, event, code_type='python', thread_ts=None): message = traceback.format_exc() + '\n\n\n' + dumps(event, indent=2) subject = 'Error occurred in ' + domain self.send_snippet(subject, subject, message, code_type=code_type, thread_ts=thread_ts) def send_raw_message(self, blocks, thread_ts=None): return self.client.chat_postMessage(channel=self.channel_id, blocks=blocks, thread_ts=thread_ts)['ts'] def update_raw_message(self, ts, blocks): self.client.chat_update(channel=self.channel_id, blocks=blocks, ts=ts) def get_perm_link(self, ts): return self.client.chat_getPermalink(channel=self.channel_id, message_ts=ts)['permalink'] def send_message(self, message, attachment=None, thread_ts=None): blocks = [{ 'type': 'section', 'text': { 'type': 'mrkdwn', 'text': message.replace('<br>', '') } }, { 'type': 'divider' }] if attachment: blocks[0]['accessory'] = { 'type': 'button', 'text': { 'type': 'plain_text', 'text': attachment['text'], 'emoji': True }, 'url': attachment['value'] } return self.send_raw_message(blocks, thread_ts)