-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlackOff.py
More file actions
76 lines (62 loc) · 3.01 KB
/
SlackOff.py
File metadata and controls
76 lines (62 loc) · 3.01 KB
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
import webbrowser
import glob
from imgurpython import ImgurClient
client_id = 'YOURS GOES HERE'
client_secret = 'YOURS GOES HERE'
imgur_client = ImgurClient(client_id, client_secret)
BOT_ID = 'YOURS GOES HERE' # os.environ.get("BOT_ID")
EXAMPLE_COMMAND = "do"
COMMAND_1 = "channel name"
COMMAND_2 = "channel users"
# instantiate Slack & Twilio clients
slack_client = SlackClient('YOURS GOES HERE')
def handle_reaction(reaction):
if reaction and len(reaction) > 0:
for output in reaction:
if 'reaction' in output:
#print("\n")
print(reaction)
var1 = reaction[0]['item']
#print(var1['type'])
#print("\n")
if var1['type'] == 'file' and reaction[0]['reaction'] == 'white_check_mark':
#print(var1['file'])
#print('hello?')
temp = slack_client.api_call("files.info", timeout=5, file=var1['file'])
#print(temp)
if (temp['file']['pretty_type'] == 'JPEG' or temp['file']['pretty_type'] == 'PNG') and \
reaction[0]['user'] == temp['file']['user']:
var3 = temp['file']['url_private']
var2 = temp['file']['url_private_download']
webbrowser.open(var2)
time.sleep(2)
list_of_files = glob.glob('C:\\Users\\Natalie\\Downloads\\*.jpg')
list_of_files2 = glob.glob('C:\\Users\\Natalie\\Downloads\\*.png')# * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
latest_file2 = max(list_of_files2, key=os.path.getctime)
file_list = [latest_file, latest_file2]
newest = max(file_list, key=os.path.getctime)
image = imgur_client.upload_from_path(newest, anon=False)
user_dm = slack_client.api_call('im.open', user=reaction[0]['user'])
#print(user_dm)
response = "You can find it here: {0}".format(image['link'])
slack_client.api_call("chat.postMessage", channel=user_dm['channel']['id'],text=response, as_user=True)
time.sleep(5)
os.remove(latest_file)
return None
if __name__ == "__main__":
READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
print("hello???")
if slack_client.rtm_connect():
print("StarterBot connected and running!")
time.sleep(1)
while True:
time.sleep(1)
handle_reaction(slack_client.rtm_read())
else:
print(BOT_ID)
print(os.environ.get('SLACK_BOT_TOKEN'))
print("Connection failed. Invalid Slack token or bot ID?")