-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
111 lines (62 loc) · 2.71 KB
/
bot.py
File metadata and controls
111 lines (62 loc) · 2.71 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
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
import re
import requests
import json
from mattermost_bot.bot import listen_to
from mattermost_bot.bot import respond_to
@respond_to('hi', re.IGNORECASE)
def hi(message):
message.reply('I can understand hi or HI!')
@respond_to('I love you')
def love(message):
message.reply('I love you too!')
@listen_to('Can someone help me?')
def help_me(message):
# Message is replied to the sender (prefixed with @user)
message.reply('Yes, I can!')
# Message is sent on the channel
# message.send('I can help everybody!')
@listen_to('https://app.box.com/s/[0-9A-Za-z]*', re.IGNORECASE)
def box_file(message):
channel_id = message.get_channel_id()
message_body = message.get_message()
bearer = "<box_developer_token>"
url = "https://api.box.com/2.0/shared_items"
boxapi_header = "shared_link=" + message_body
auth_header = "Bearer " + bearer
headers = {'Boxapi': boxapi_header, 'authorization': auth_header}
response = requests.get(url, headers=headers)
output = json.loads(response.text)
box_id = output['id']
box_name = output['name']
url = "https://api.box.com/2.0/files/" + box_id + "/content"
headers = {'authorization': auth_header}
response = requests.get(url, headers=headers)
send_file = response.content
url = "<url_of_the_mattermost>/api/v1/users/login"
payload = "{\n \"name\": \"<team_name>\",\n \"email\": \"sh*********@gmail.com\",\n \"password\": \"b*********\"\n}"
headers = {'content-type': "application/json"}
response = requests.request("POST", url, data=payload, headers=headers)
token_id = response.headers['Token']
url = "<url_of_the_mattermost>/api/v1/users/me"
auth_header = "Bearer " + token_id
headers = {'authorization': auth_header}
response = requests.request("GET", url, headers=headers)
url = "<url_of_the_mattermost>/api/v1/files/upload"
auth_header = "Bearer " + token_id
headers = {'authorization': auth_header}
files = {'files': send_file}
files = {'files': (box_name, send_file)}
data = {'channel_id': channel_id}
response = requests.post(url, files=files, data=data, headers=headers)
output = json.loads(response.text)
filenamesT = output['filenames']
filenames = filenamesT[0]
url = "<url_of_the_mattermost>/api/v1/channels/" + channel_id + "/create"
auth_header = "Bearer " + token_id
headers = {'authorization': auth_header}
string = filenames.split("/")
time='1462822960652'
joinString = string[2]+":"+time
filenames = "[\"" + filenames + "\"]"
payload = "{\"filenames\": " + filenames + ",\"message\":\"\",\"channel_id\": \"" + string[1] + "\",\"pending_post_id\": \"" + joinString + "\",\"user_id\": \"" + string[2] + "\" ,\"create_at\":" + time + "}"
response = requests.request("POST", url, data=payload, headers=headers)