Created
February 15, 2019 07:44
-
-
Save manasmbellani/8f9e66bfc7e1e53cc5b220bb4adebcb9 to your computer and use it in GitHub Desktop.
slack_get_messages_from_channel.py - Gets messages from slack channel with various filters individually or combined e.g. get last 50 messages from a channel, posted by user X, over last 2 days
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
NUM_SECS_IN_DAY=86400 | |
import json | |
import time | |
from datetime import datetime | |
from argparse import ArgumentParser | |
from slackclient import SlackClient | |
parser = ArgumentParser(description="Read messages from the specified slack channel") | |
parser.add_argument("-t", "--slack-token", action="store", dest="slack_token", required=True, | |
help="Slack API Token") | |
parser.add_argument("-c", "--channel-id", action="store", dest="channel_id", required=True, | |
help=("Channel ID - obtained for a channel by reading URL when" | |
" accessing channel in Slack Client.")) | |
parser.add_argument("-n", "--number-messages-to-read", action="store", | |
dest="number_messages_to_read", | |
default=100, | |
help="Number of messages to read in an attempt") | |
parser.add_argument("-u", "--username", action="store", | |
dest="username_to_filter_by", | |
help="Filter messages by username (generally used for apps)") | |
parser.add_argument("-uid", "--user-id", action="store", | |
dest="userid_to_filter_by", | |
help="Filter messages by userid (generally used by users)") | |
parser.add_argument("-d", "--days", action="store", | |
dest="days_to_filter_by", | |
help="Filter messages to show only ones posted in last X days") | |
parser.add_argument("-o", "--outfile", action="store", dest="outfile", | |
default="out-messages-in-channel.txt", | |
help="Output file for storing messages in channel") | |
args = parser.parse_args() | |
channel_id = args.channel_id | |
number_messages_to_read = args.number_messages_to_read | |
slack_token = args.slack_token | |
username_to_filter_by = args.username_to_filter_by | |
userid_to_filter_by = args.userid_to_filter_by | |
days_to_filter_by = args.days_to_filter_by | |
outfile = args.outfile | |
print("[*] Initializing Slack Client with the slack token provided") | |
sc = SlackClient(slack_token) | |
print("[*] Reading {} messages from channel: {} via SlackClient's channel:history function".format(number_messages_to_read, channel_id)) | |
out = sc.api_call("channels.history", channel=channel_id, count=number_messages_to_read) | |
print("[*] Parsing response to ensure that there were not errors") | |
if "ok" in out: | |
if out["ok"] == True: | |
print("[+] Messages downloaded ok.") | |
if "messages" in out: | |
out_messages = out["messages"] | |
else: | |
out_messages = [] | |
if username_to_filter_by: | |
print("[*] Filtering messages by username: {}".format(username_to_filter_by)) | |
out_with_username = [] | |
num_messages_found_matching_criteria = 0 | |
for message in out_messages: | |
if "username" in message and message["username"].lower() == username_to_filter_by.lower(): | |
num_messages_found_matching_criteria += 1 | |
out_with_username.append(message) | |
print("[+] Number of messages found matching username: {} criteria: {}".format(username_to_filter_by, num_messages_found_matching_criteria)) | |
out_messages = out_with_username | |
if userid_to_filter_by: | |
print("[*] Filtering messages by userid: {}".format(userid_to_filter_by)) | |
out_with_userid = [] | |
num_messages_found_matching_criteria = 0 | |
for message in out_messages: | |
if "user" in message and message["user"].lower() == userid_to_filter_by.lower(): | |
num_messages_found_matching_criteria += 1 | |
out_with_userid.append(message) | |
print("[+] Number of messages found matching user: {} criteria: {}".format(userid_to_filter_by, num_messages_found_matching_criteria)) | |
out_messages = out_with_userid | |
if days_to_filter_by: | |
now = datetime.now() | |
now_epoch = time.mktime(now.timetuple()) | |
print("[*] Filtering messages for ones posted in the last {} days".format(days_to_filter_by)) | |
out_within_specified_days = [] | |
num_messages_found_matching_criteria = 0 | |
for message in out_messages: | |
message_epoch_time = float(message["ts"]) | |
# print("[v] message_epoch_time = {}, now_epoch = {}".format(message_epoch_time, now_epoch)) | |
if (now_epoch - message_epoch_time) < (int(days_to_filter_by) * NUM_SECS_IN_DAY): | |
num_messages_found_matching_criteria += 1 | |
out_within_specified_days.append(message) | |
print("[+] Number of messages found matching days to filter by: {} criteria: {}".format(days_to_filter_by, num_messages_found_matching_criteria)) | |
out_messages = out_within_specified_days | |
print("[*] Writing messages in output as JSON to file: {}".format(outfile)) | |
formatted_out = json.dumps(out_messages, indent=4) | |
with open(outfile, "w+") as f: | |
f.write(formatted_out) | |
else: | |
print("[-] Error occurred when downloading the messages from channel: {}".format(channel_id)) | |
if "error" in out: | |
error = out["error"] | |
print("[-] Error that occurred: {}".format(error)) | |
else: | |
print("[-] No 'error' key status returned in JSON output - something went wrong.") | |
print("[-] Dumping the entire response: ") | |
formatted_out = json.dumps(out, indent=4) | |
print("{}".format(formatted_out)) | |
else: | |
print("[-] No 'ok' key status returned in JSON output - something went wrong.") | |
print("[-] Dumping the entire response: ") | |
formatted_out = json.dumps(out, indent=4) | |
print("{}".format(formatted_out)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment