forked from iuiaoin/wechat-gptbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
101 lines (88 loc) · 2.61 KB
/
api.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
from utils import const
from utils.gen import gen_id
import requests
import json
from utils.log import logger
from utils.const import MessageType
def fetch(path, data):
base_data = {
"id": gen_id(),
"type": "null",
"roomid": "null",
"wxid": "null",
"content": "null",
"nickname": "null",
"ext": "null",
}
base_data.update(data)
url = f"http://{const.IP}:{const.PORT}/{path}"
response = requests.post(url, json={"para": base_data}, timeout=5)
return response.json()
def get_personal_info():
path = "/api/get_personal_info"
data = {
"type": MessageType.PERSONAL_INFO.value,
"content": "op:personal info",
}
try:
response = fetch(path, data)
content = json.loads(response["content"])
logger.info(
f"""
wechat login info:
nickName: {content['wx_name']}
account: {content['wx_code']}
wechatId: {content['wx_id']}
startTime: {response['time']}
"""
)
return content
except Exception as e:
logger.error("Get personal info failed!")
logger.exception(e)
# get sender's nickname in group chat
def get_sender_name(room_id, sender_id):
path = "api/getmembernick"
data = {
"type": MessageType.CHATROOM_MEMBER_NICK.value,
"wxid": sender_id,
"roomid": room_id or "null",
}
response = fetch(path, data)
return json.loads(response["content"])["nick"]
def send_txt(msg, wx_id):
path = "api/sendtxtmsg"
data = {
"type": MessageType.TXT_MSG.value,
"content": msg,
"wxid": wx_id,
}
response = fetch(path, data)
if response["status"] == const.SUCCESS:
logger.info("text sent successfully")
else:
logger.error(f"[Server Error]: {response.text}")
def send_image(img_path, wx_id):
path = "api/sendpic"
data = {
"type": MessageType.PIC_MSG.value,
"content": img_path,
"wxid": wx_id,
}
response = fetch(path, data)
if response["status"] == const.SUCCESS:
logger.info("image sent successfully")
else:
logger.error(f"[Server Error]: {response.text}")
def send_file(file_path, wx_id):
path = "api/sendattatch"
data = {
"type": MessageType.ATTACH_FILE.value,
"content": file_path,
"wxid": wx_id,
}
response = fetch(path, data)
if response["status"] == const.SUCCESS:
logger.info("file sent successfully")
else:
logger.error(f"[Server Error]: {response.text}")