-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
203 lines (136 loc) · 5.05 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from msilib.schema import Error
import requests;
import json;
global group_ans_pool2;
group_ans_pool2 = {}
host_addr = 'http://127.0.0.1:5700/'
class CommandError(Exception):
pass
class Executer:
def __init__(self):
self.handle_dict = {};
def add(self, name:str, func):
self.handle_dict["name"] = func
def __call__(self, api_name : str, paras : dict):
func = self.handle_dict.get(api_name);
if func == None:
raise CommandError("command not found")
return func(paras)
class Event:
def __init__(self, handle, api_request = [], **kwargs):
self.api_request = api_request
self.handle = handle
self.executer = None
def set_executer(self, executer : Executer):
self.executer = executer
def __call__(self, user_id, channel_id, *args):
return self.handle(user_id, channel_id, self.executer, *args)
class event_pool:
def __init__(self):
self.cmdpool = {}
self.apipool = {}
def add(self, command : str, event : Event):
api_exec = Executer()
#for i in
def get_group_ans_pool():
return group_ans_pool2;
def send_private_message (user_id, message, auto_escape=False, group_id = 0):
if(len(message) >=128):
r1 = send_private_message (user_id, message[:120], auto_escape, group_id)
r2 = send_private_message (user_id, message[120:], auto_escape, group_id)
return r1 + r2;
group_id = 0; ##disallow private msg via group
#return;
url = host_addr + 'send_private_msg'
if (group_id == 0):
d = {'user_id':user_id,'message': message,'auto_escape': auto_escape}
else:
d = {'user_id':user_id, 'group_id':group_id,'message': message,'auto_escape': auto_escape}
r = requests.post(url, data=d)
return [json.loads(r.text)["data"]]
def send_private_message_by_group (user_id, group_id, message, auto_escape=False):
#return;
url = host_addr + 'send_private_msg'
d = {'user_id':user_id, 'group_id':group_id,'message': message,'auto_escape': auto_escape}
r = requests.post(url, data=d)
return json.loads(r.text)["data"]
def send_group_message(group_id, message, auto_escape=False):
print(message);
if group_id == 864011653:
return;
pass
# return;
url = host_addr + 'send_group_msg'
d = {'group_id':group_id,'message': message,'auto_escape': auto_escape}
r = requests.post(url, data=d)
return json.loads(r.text)["data"]
def send_discuss_message(discuss_id, message, auto_escape=False):
url = host_addr + 'send_group_msg'
d = {'discuss_id':discuss_id,'message': message,'auto_escape': auto_escape}
r = requests.post(url, data=d)
return json.loads(r.text)["data"]
def get_group_member_info(groupid, qqid):
url = host_addr + 'get_group_member_info'
d = {'group_id':groupid, 'user_id':qqid,'no_cache':True}
r = requests.post(url, data=d)
js = json.loads(r.text);
#print(js);
return js["data"]
# "owner" "admin" "member"
def get_group_member_role(groupid, qqid):
data = get_group_member_info(groupid, qqid);
return data["role"]
def set_group_ban(qqid, groupid, duration):
url = host_addr + 'set_group_ban'
d = {'group_id':groupid,'user_id': qqid,'duration': duration}
#print(d);
r = requests.post(url, data=d)
def get_group_member_title(groupid, qqid):
data = get_group_member_info(groupid, qqid);
d = data['title'];
#print(data);
return d
def get_group_member_card(groupid, qqid):
data = get_group_member_info(groupid, qqid);
d = data['card'];
#print(data);
return d
def clean_cache():
url = host_addr + 'clean_cache'
d = {}
r = requests.post(url, data=d)
return {}
def set_group_kick(qqid, groupid):
url = host_addr + 'set_group_kick'
d = {'group_id':groupid,'user_id': qqid,'reject_add_request': False}
r = requests.post(url, data=d)
return {};
def delete_msg(msgid):
url = host_addr + 'delete_msg'
d = {'message_id':msgid}
r = requests.post(url, data=d)
return {};
def get_msg(msgid):
url = host_addr + 'get_msg'
d = {'message_id':msgid}
r = requests.post(url, data=d)
return json.loads(r.text)["data"];
def get_msg_sender(msgid):
data = get_msg(msgid);
#print(data)
return data["sender"]["user_id"];
def set_group_add_request(flag, type, approve = True, reason = ""):
url = host_addr + 'set_group_add_request'
d = {'flag':flag,'type':type,'approve':approve,'reason':reason }
r = requests.post(url, data=d)
return json.loads(r.text)["data"];
def set_friend_add_request(flag, approve = True, remark = ""):
url = host_addr + 'set_friend_add_request'
d = {'flag':flag,'approve':approve,'remark':remark }
r = requests.post(url, data=d)
return json.loads(r.text)["data"];
def set_group_card(group_id, user_id, card = ""):
url = host_addr + 'set_group_card'
d = {'group_id':group_id,'user_id':user_id,'card':card}
r = requests.post(url, data=d)
return json.loads(r.text)["data"];