def post_message(token, channel, text, bot_name): raise Exception("TRIED TO POST A MESSAGE TO SLACK!") slack_api.call_slack('chat.postMessage', { 'token': token, 'channel': channel, 'text': text, 'username': bot_name })
def get_message_history(token, channel, ts_start, ts_end): # 1000 messages is the maximum allowed by the API. history_raw = slack_api.call_slack('channels.history', {'token': token, 'channel': channel, 'latest': ts_end, 'oldest': ts_start, 'count': 1000}) return [{'user': message['user'], 'ts': message['ts']} for message in history_raw['messages'] if (message['type'] == 'message' and 'user' in message and 'ts' in message)]
def get_message_history(token, channel_id, channel_name, days, now_datetime, use_fake_data=False, should_log_raw_channel_history=False): ts = timestamp_for_days_ago(now_datetime, days) print("Going back as far as...") print(DT.datetime.utcfromtimestamp(ts).strftime('%Y-%m-%dT%H:%M:%SZ')) if (use_fake_data): print('using fake data, not loading from slack') with open('sensitive/channels.history.json') as f: history_raw = json.load(f) else: # TODO: 'conversations.history' will allow private channels (e.g. fundamentals course) # but they're not otherwise equivalent. conversations.history yields lower numbers. history_raw = slack_api.call_slack('channels.history', { 'token': token, 'channel': channel_id, 'oldest': ts, 'count': 1000 }) # 1000 messages is the maximum allowed by the API. if (should_log_raw_channel_history): with open('sensitive/channels.history.json', 'w') as f: f.write(json_pp(history_raw)) if 'has_more' in history_raw and history_raw['has_more']: print( 'WARNING: messages missing from this report. Try reducing the number of days to report upon.' ) return (history_raw, [{ 'user': message['user'], 'ts': message['ts'] } for message in history_raw['messages'] if ( message['type'] == 'message' and 'user' in message and 'ts' in message) ])
#! /usr/bin/env python3 # Hit the Slack API to get a list of users with their internal Slack # IDs. # # Usage: python3 list_channels.py < api_token.txt > input_channel.csv # cp input_channel.csv output_channel.csv # # Remove all but two lines from each file: input_channel.csv should # have a header and the line for the channel you want to # monitor. Similarly, output_channel.csv should have a header and the # line for the channel you want to send the report to. import slack_api import csv import sys token = input().strip() channel_list = slack_api.call_slack('conversations.list', { 'token': token, 'types': 'private_channel' }) channel_list_writer = csv.writer(sys.stdout) channel_list_writer.writerow(['channel_id', 'channel_name']) for channel in channel_list['channels']: channel_list_writer.writerow([channel['id'], channel['name']])
#! /usr/bin/env python3 # Hit the Slack API to get a list of users with their internal Slack # IDs. # # Usage: python3 list_users.py < api_token.txt > users.csv # # Typically you'll manually edit users.csv to pare the list down to # the active people you want to monitor. import slack_api import csv import sys token = input().strip() user_list = slack_api.call_slack('users.list', {'token': token}) user_list_writer = csv.writer(sys.stdout) user_list_writer.writerow(['user_id', 'user_name']) for member in user_list['members']: user_list_writer.writerow([member['id'], member['name']])
#! /usr/bin/env python3 # Hit the Slack API to get a list of users with their internal Slack # IDs. # # Usage: python3 list_channels.py < api_token.txt > input_channel.csv # cp input_channel.csv output_channel.csv # # Remove all but two lines from each file: input_channel.csv should # have a header and the line for the channel you want to # monitor. Similarly, output_channel.csv should have a header and the # line for the channel you want to send the report to. import slack_api import csv import sys token = input().strip() channel_list = slack_api.call_slack('channels.list', {'token': token}) channel_list_writer = csv.writer(sys.stdout) channel_list_writer.writerow(['channel_id', 'channel_name']) for channel in channel_list['channels']: channel_list_writer.writerow([channel['id'], channel['name']])
def post_message(token, channel, text, bot_name): slack_api.call_slack('chat.postMessage', {'token': token, 'channel': channel, 'text': text, 'username': bot_name})