-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_message.py
51 lines (40 loc) · 1.41 KB
/
post_message.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
#!/usr/bin/env python3
import json
import sys
import os
import requests
def post_message(msg, group_id, API_ROOT, key):
files = []
if "files" in msg:
for file in msg["files"]:
r = requests.post(
API_ROOT + "/files",
files={
"file": file,
},
headers={"x-auth-token": key},
)
file = r.json()
print(file)
files.append(file["id"])
r = requests.post(
API_ROOT + "/messages",
data=json.dumps({"group": group_id, "data": msg["data"], "files": files}),
headers={"Content-Type": "application/json", "x-auth-token": key},
)
if __name__ == "__main__":
API_ROOT = sys.argv[1]
TOKEN = sys.argv[2]
message_dir = sys.argv[3]
group_id = sys.argv[4]
msg = {"files": []}
if os.path.isdir(message_dir + os.sep + "files"):
for filepath in os.listdir(message_dir + os.sep + "files"):
with open(message_dir + os.sep + "files" + os.sep + filepath, "rb") as file:
msg["files"].append(file.read())
with open(message_dir + os.sep + "title.txt", "r") as file:
msg["title"] = file.read()
with open(message_dir + os.sep + "text.txt", "r") as file:
msg["text"] = file.read()
msg["data"] = {"title": msg["title"], "text": msg["text"]}
post_message(msg, group_id, API_ROOT, TOKEN)