-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendmessage.py
More file actions
77 lines (69 loc) · 2.27 KB
/
sendmessage.py
File metadata and controls
77 lines (69 loc) · 2.27 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
import json
import requests
from requests_toolbelt import MultipartEncoder
def get_tenant_access_token(app_id, app_secret):
url = "https://open.f.mioffice.cn/open-apis/auth/v3/tenant_access_token/internal"
headers = {
"Content-Type": "application/json"
}
payload = {
"app_id": app_id,
"app_secret": app_secret
}
response = requests.post(url, json=payload, headers=headers)
data = response.json()
if response.status_code == 200 and data.get("code") == 0:
return data["tenant_access_token"]
else:
print("Error getting token:", data)
return None
def send(token, email, msg):
url = "https://open.f.mioffice.cn/open-apis/im/v1/messages"
params = {"receive_id_type":"email"}
msgContent = {
"text": msg,
}
req = {
"receive_id": email,
"msg_type": "text",
"content": json.dumps(msgContent)
}
payload = json.dumps(req)
headers = {
'Authorization': token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, params=params, headers=headers, data=payload)
print(response.content)
def send_file(token, email, file_id):
url = "https://open.f.mioffice.cn/open-apis/im/v1/messages"
params = {"receive_id_type":"email"}
msgContent = {
"file_key": file_id,
}
req = {
"receive_id": email,
"msg_type": "file",
"content": json.dumps(msgContent)
}
payload = json.dumps(req)
headers = {
'Authorization': token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, params=params, headers=headers, data=payload)
print(response.content)
def upload(token, filename, filepath):
url = "https://open.f.mioffice.cn/open-apis/im/v1/files"
form = {'file_type': 'xls',
'file_name': filename,
'file': (filename, open(filepath, 'rb'), "application/vnd.ms-excel")}
multi_form = MultipartEncoder(form)
headers = {
'Authorization': token,
}
headers['Content-Type'] = multi_form.content_type
response = requests.request("POST", url, headers=headers, data=multi_form)
print(response.content)
response_json = response.json()
return response_json["data"]["file_key"]