def create_users_table(channel_name, channel_id, DATABASE_URL): ## read all user_ids bot_token = os.environ.get("SLACK_BOT_TOKEN") client = WebClient(token=bot_token) response = client.conversations_members(channel=channel_id) user_ids = response["members"][1:] # connect to database conn = psycopg2.connect(DATABASE_URL, sslmode='require') cur = conn.cursor() ## create table cur.execute( f"CREATE TABLE if not exists users_{channel_name} (user_id VARCHAR(20) PRIMARY KEY, participate INTEGER, virtual INTEGER)" ) ## insert each user into the table for user_id in user_ids: cur.execute( f"INSERT INTO users_{channel_name}(user_id, participate, virtual) VALUES (\'{user_id}\', 0, 0)" ) conn.commit() cur.close() conn.close()
def get_instructors_ids(client: WebClient) -> Set[str]: ''' Gets the slack_ids of the instructors to save them from spam. ''' response = client.conversations_members(channel="C01RBSVJ1D5") instructors_ids = response["members"] return set(instructors_ids)
def return_joining_channels(say, logger, context): '''tell channel names where the bot joins `@BOTNAME where` ''' client = WebClient(token=slack_bot_token) # if you want only channels, ch['is_channel'] will be good filter channels = [] for ch in client.conversations_list()['channels']: members = client.conversations_members(channel=ch['id'])['members'] if bot_user_id in members: channels.append(ch['name']) if len(channels) > 0: say(msgs.imin().format('\n'.join(channels))) else: say(msgs.noplace())
def get_users_in_channel(client: WebClient, *, channel_id: str, team_id: str, **kwargs) -> list[str]: result = client.conversations_members(channel=channel_id) return result["members"]