This repository was archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslack_upload.py
executable file
·139 lines (108 loc) · 2.87 KB
/
slack_upload.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 15 16:43:14 2020
@author: leespitzley
"""
# https://stackoverflow.com/questions/43464873/how-to-upload-files-to-slack-using-file-upload-and-requests
import requests
import yaml
#%% upload file
#%% upload forecast file to Slack
def upload_forecast_file(token):
my_file = {
'file' : ('last_ten_alb.csv', open('last_ten_alb.csv', 'rb'), 'csv')
}
payload={
"filename":"last_ten_alb.csv",
"token":token,
"channels":['#forecasting'],
}
r = requests.post("https://slack.com/api/files.upload", params=payload, files=my_file)
print(r)
print(r.text)
#%% read file to bot
def read_bot_submission(token):
"""
Download files sent to the bot
Not functional
Args:
token (TYPE): DESCRIPTION.
Returns:
None.
"""
payload ={
"token":token,
"channels":'#B01AJRTQJLE',
"types" : "csv"}
headers = {'Content-Type': 'application/json'}
get = requests.get('https://slack.com/api/files.list', params=payload, headers=headers)
print(get)
print(get.json()["files"][0]["name"])
print(get.text)
# with open()
#%% files to the bot
def view_bot_conversations(token):
"""
View people who have interacted with the bot
Args:
token (TYPE): DESCRIPTION.
Returns:
None.
"""
# U01ARHWDLN6 # bot
# user: U018ZK7MD7D
payload ={
"token":token,
"channels":'U01ARHWDLN6'}
headers = {'Content-Type': 'application/json'}
get = requests.get('https://slack.com/api/conversations.members', params=payload)
print(get)
#print(get.json()["files"][0]["name"])
print(get.text)
for convo in get.json()["channels"]:
print(convo["name"])
#%% convo list
def view_convo_history(token):
"""
See what has been sent to the bot
Not working?
Args:
token (TYPE): DESCRIPTION.
Returns:
None.
"""
payload ={
"token":token,
"types": "im",
"channel": "D01AJSEBX38"}
headers = {'Content-Type': 'application/json'}
get = requests.get('https://slack.com/api/conversations.history', params=payload)
print(get)
#print(get.json()["files"][0]["name"])
print(get.text)
for convo in get.json()["channel"]:
print(convo["user"])
#%% get bot info
def get_bot_info(token):
"""
Get the bot ID
Args:
token (TYPE): DESCRIPTION.
Returns:
None.
"""
payload = {"token":token,
}
bot= requests.get('https://slack.com/api/auth.test', params=payload)
print(bot.json())
print(bot.text)
#%% run
if __name__ == '__main__':
# load up the token
filepath = "tokens.yaml"
with open(filepath, 'r') as f:
keys = yaml.safe_load(f)
# post data to Slack
token = keys['token']
upload_forecast_file(token)