-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
76 lines (63 loc) · 2.31 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import time
from slackclient import SlackClient
SLACK_TOKEN = os.environ.get('SLACK_TOKEN')
slack_client = SlackClient(SLACK_TOKEN)
#Set using the "set" command in windows
def list_channels():
channels_call = slack_client.api_call("channels.list")
if channels_call.get('ok'):
return channels_call['channels']
return None
def channel_info(channel_id):
channel_info = slack_client.api_call("channels.info", channel=channel_id)
if channel_info:
return channel_info['channel']
return None
def send_message(channel_id,message):
slack_client.api_call(
"chat.postMessage",
channel = channel_id,
text=message,
username = 'Comms Bot',
icon_emoji=':robot_face:'
)
def get_messages(slack_args, messages, filter_func):
history = slack_client.api_call("channels.history", **slack_args)
print("HISTROY",history)
last_ts = history['messages'][-1]['ts'] if (history['has_more'] and history) else False
filtered = list(filter(filter_func, history['messages']))
all_messages = messages + filtered
print('Fetched {} messages. {} Total now.'.format(len(filtered), len(all_messages)))
return {
'messages': all_messages,
'last_ts': last_ts,
}
def scrape_slack(slack_args, filter_func = lambda x: x):
results = get_messages(slack_args, [], filter_func)
while results['last_ts']:
slack_args['latest'] = results['last_ts']
results = get_messages(sc, slack_args, results['messages'], filter_func)
print('Done fetching messages. Found {} in total.'.format(len(results['messages'])))
return results['messages']
if __name__ == '__main__':
channels = list_channels()
slack_args = {
'channel': "CFRV15GBU",
'oldest': "",
}
if channels:
print("Channels: ")
for c in channels:
#print(c['name'] + " (" + c['id'] + ")")
detailed_info = channel_info(c['id'])
#if detailed_info:
#print(detailed_info.keys())
#if c['name'] == 'communications_office':
#send_message(c['id'], "Testing comms feed bot,ignore")
print("GETTING MESSAGES...")
msgs = scrape_slack(slack_args)
print(msgs)
print('-----')
else:
print("Unable to authenticate.")