forked from JHue58/ChatLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimuse.py
411 lines (368 loc) · 14 KB
/
simuse.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import json
import time
from typing import Type
import requests as r
#取得数据包
def Get_data():
data_file = open(r'data.json', 'r', encoding='utf-8-sig')
data = data_file.read()
data = json.loads(data)
host = data['host']
if host.find(':') == -1:
data['host'] = data['host'] + ':' + data['port']
try:
host.strip(':', ':')
except:
pass
return data
#取得缓存的事件
def Get_Meesage():
time.sleep(0.5)
try:
tempfile = open(r'messagetemp.sim', 'r', encoding='utf-8')
except:
return 0
try:
Message = eval(tempfile.read())
except:
tempfile.close()
return 0
tempfile.close()
return Message
#激活会话ID(不要单独调用)
def Check_Session(host, session, qq):
url = 'http://' + host + '/bind'
data_in = dict(sessionKey=session, qq=qq)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
return (res['code'])
#取得会话ID (getsession:传入不为0的int值可让函数返回已激活的会话ID)
#0-已启动mah但未登录qq,1-未启动mah或mah配置错误
def Get_Session(data, getsession=0):
host = data['host']
verifyKey = data['Key']
qq = data['qq']
url = 'http://' + host + '/verify'
data_in = dict(verifyKey=verifyKey)
try:
res = r.request('post', url, json=data_in)
except:
return 1
else:
res = json.loads(res.text)
session = res['session']
code = Check_Session(host, session, qq)
if (getsession == 0 and code == 0):
data.update(session=session)
return data
elif (getsession != 0 and code == 0):
return session
else:
#print('创建失败')
return 0
#接收消息 (若传入deal=0,则表示返回不经过简化的原消息信息和事件)
def Fetch_Message(data, deal=1):
if type(data) == type(0):
raise ConnectionError('未与api-http取得连接,或mirai未登录')
host = data['host']
session = data['session']
url = 'http://' + host + '/fetchMessage' + '?sessionKey=' + session + '&count=10'
res = r.request('get', url)
res = json.loads(res.text)
Message = res['data']
if deal == 1:
Message = Fetch_Message_info(Message)
return Message
elif deal == 0:
return Message
else:
return 0
#接受消息/事件信息(简化处理)
def Fetch_Message_info(Message):
messagemain = dict()
messageinfo = dict()
Message_c = []
for i in Message:
if i['type'] != 'GroupMessage' and i['type'] != 'FriendMessage':
Message_c.append(i.copy())
else:
messageinfo.update(type=i['type'])
messageinfo.update(messagechain=i['messageChain'])
senderinfo = i['sender']
messageinfo.update(sender=senderinfo['id'])
if i['type'] == 'GroupMessage':
groupinfo = senderinfo['group']
messageinfo.update(group=groupinfo['id'])
Message_c.append(messageinfo.copy())
if str(Message_c) == '[]':
return 0
else:
return (Message_c)
#发送消息(target_type:1-群,2-私聊,3-临时会话;message_type:1-文字,2-图片;path:传入不为0的int值可发送本地图片,默认为url)
##target_type为3,传入的target应当为一个字典dict,格式:{'qq':'123','group':'123'}
#返回值为本次发送的消息id
def Send_Message(data, target, target_type, message, message_type, path=0):
host = data['host']
session = data['session']
if target_type == 1 and message_type == 1:
#print('1')
url = 'http://' + host + '/sendGroupMessage'
messageinfo = []
messagechain = dict(type='Plain', text=message)
messageinfo.append(messagechain.copy())
elif target_type == 2 and message_type == 1:
#print('2')
url = 'http://' + host + '/sendFriendMessage'
messageinfo = []
messagechain = dict(type='Plain', text=message)
messageinfo.append(messagechain.copy())
elif target_type == 1 and message_type == 2:
#print('3')
url = 'http://' + host + '/sendGroupMessage'
messageinfo = []
if path == 0:
messagechain = dict(type='Image', url=message)
messageinfo.append(messagechain.copy())
elif path != 0:
messagechain = dict(type='Image', path=message)
messageinfo.append(messagechain.copy())
elif target_type == 2 and message_type == 2:
#print('4')
url = 'http://' + host + '/sendFriendMessage'
messageinfo = []
if path == 0:
messagechain = dict(type='Image', url=message)
messageinfo.append(messagechain.copy())
elif path != 0:
messagechain = dict(type='Image', path=message)
messageinfo.append(messagechain.copy())
elif target_type == 3 and message_type == 1:
#print('5')
url = 'http://' + host + '/sendTempMessage'
messageinfo = []
messagechain = dict(type='Plain', text=message)
messageinfo.append(messagechain.copy())
elif target_type == 3 and message_type == 2:
#print('6')
url = 'http://' + host + '/sendTempMessage'
messageinfo = []
if path == 0:
messagechain = dict(type='Image', url=message)
messageinfo.append(messagechain.copy())
elif path != 0:
messagechain = dict(type='Image', path=message)
messageinfo.append(messagechain.copy())
else:
return 0
if target_type == 3:
data_in = dict(sessionKey=session, messageChain=messageinfo)
data_in.update(target)
else:
data_in = dict(sessionKey=session,
target=target,
messageChain=messageinfo)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
if res['code'] == 0:
return res['messageId']
#若了解mah的消息链后,可以用此函数发送消息链
#返回值为本次发送的消息id
def Send_Message_Chain(data, target, target_type, messagechain):
host = data['host']
session = data['session']
if target_type == 1:
url = 'http://' + host + '/sendGroupMessage'
elif target_type == 2:
url = 'http://' + host + '/sendFriendMessage'
elif target_type == 3:
url = 'http://' + host + '/sendTempMessage'
else:
return 0
if target_type == 3:
data_in = dict(sessionKey=session, messageChain=messagechain)
data_in.update(target)
else:
data_in = dict(sessionKey=session,
target=target,
messageChain=messagechain)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
if res['code'] == 0:
return res['messageId']
#获取数据包,填入mah配置的地址host端口号port和密钥verifyKey以及登录mirai的qq,若已获取会话ID,则可填入会话IDsession
def Creat_data(host='127.0.0.1', port='8080', verifyKey='', qq='', session=''):
host = str(host)
port = str(port)
verifyKey = str(verifyKey)
qq = str(qq)
host = host + ':' + port
data = dict(host=host, Key=verifyKey, qq=qq, session=session)
return data
#撤回消息,messageid为消息的id
def Recall_Message(data, messageid):
session = data['session']
host = data['host']
url = 'http://' + host + '/recall'
data_in = dict(sessionKey=session, target=messageid)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
return res
#禁言成员(target为指定群,member为对象qq号,time为禁言时间,单位秒,若不传入member,则默认全体禁言)
#需要有相关权限
def Mute(
data,
target,
member='0',
time=0,
):
session = data['session']
host = data['host']
if member != '0':
url = 'http://' + host + '/mute'
data_in = dict(sessionKey=session,
target=target,
memberId=member,
time=time)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
return res
else:
url = 'http://' + host + '/muteAll'
data_in = dict(sessionKey=session, target=target)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
return res
#解除禁言(target为指定群,member为对象qq号,若不传入member,则默认解除全体禁言)
#需要有相关权限
def Unmute(data, target, member='0'):
session = data['session']
host = data['host']
if member != '0':
url = 'http://' + host + '/unmute'
data_in = dict(sessionKey=session, target=target, memberId=member)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
return res
else:
url = 'http://' + host + '/unmuteAll'
data_in = dict(sessionKey=session, target=target)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
return res
#踢出群成员(target为指定群,member为对象qq号)
#需要有相关权限
def Kick(data, target, member):
session = data['session']
host = data['host']
url = 'http://' + host + '/kick'
data_in = dict(sessionKey=session, target=target, memberId=member)
res = r.request("post", url, json=data_in)
res = json.loads(res.text)
return res
#设置群 target为指定群,config为需设置内容,数据类型为字典dict,或在传参时指定参数名称传入
#例:Group_Config(data,target,name='ok',announcement='test'……)
#config中的参数名称:name(str)-群名,announcement(str)-群公告,confessTalk(bool)-是否开启坦白说;
#allowMemberInvite(bool)-是否允许群员邀请,autoApprove(bool)-是否开启自动审批入群,anonymousChat(bool)-是否允许匿名聊天
#需要有相关权限
def Group_Config(data, target, **config):
session = data['session']
host = data['host']
url = 'http://' + host + '/groupConfig'
data_in = dict(sessionKey=session, target=target, config=config)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
return res
#设置群成员资料,target为指定群,memberid为群成员QQ,config为需设置内容,数据类型为字典dict,或在传参时指定参数名称传入
#例:Member_Info(data,target,memberid,name='ok',specialTitle='test'……)
#config中的参数名称:name(str)-群名片,specialTitle(str)-群头衔
#需要有相关权限
def Member_Info(data, target, memberid, **config):
session = data['session']
host = data['host']
url = 'http://' + host + '/memberInfo'
data_in = dict(sessionKey=session,
target=target,
memberId=memberid,
info=config)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
return res
#退出群聊,target为指定群
def Quit(data, target):
session = data['session']
host = data['host']
url = 'http://' + host + '/quit'
data_in = dict(sessionKey=session, target=target)
res = r.request('post', url, json=data_in)
res = json.loads(res.text)
return res
#取得bot的好友列表,返回值为列表
#参数名称:id-qq号,nickname-好友昵称,remark-备注
def Get_Friend(data):
session = data['session']
host = data['host']
url = 'http://' + host + '/friendList?sessionKey=' + session
res = r.request('get', url)
res = json.loads(res.text)
List = res['data']
return List
#取得bot的群列表,返回值为列表
#参数名称:id-群号,name-群昵称,permission-权限
def Get_Group(data):
session = data['session']
host = data['host']
url = 'http://' + host + '/groupList?sessionKey=' + session
res = r.request('get', url)
res = json.loads(res.text)
List = res['data']
return List
#取得指定群的设置信息,返回值为字典
#参数名称:name-群名称,confessTalk-是否开启坦白说,allowMemberInvite-是否允许群员邀请,autoApprove-是否开启自动审批入群,anonymousChat-是否允许匿名聊天
def Get_Groupinfo(data, target):
session = data['session']
host = data['host']
target = str(target)
url = 'http://' + host + '/groupConfig?sessionKey=' + session + '&target=' + target
res = r.request('get', url)
res = json.loads(res.text)
return res
#取得指定群的群成员列表,返回值为列表
#参数名称:id-qq号,memberName-群名片,specialTitle-群头衔,permission-权限
#若传入deal=0,则还可返回群成员的joinTimestamp-入群时间戳,lastSpeakTimestamp-最后一次发言时间戳,muteTimeRemaining-禁言剩余时间
def Get_Groupmember(data, target, deal=1):
if deal != 1 and deal != 0:
return 0
session = data['session']
host = data['host']
target = str(target)
url = 'http://' + host + '/memberList?sessionKey=' + session + '&target=' + target
res = r.request('get', url)
res = json.loads(res.text)
List = res['data']
for i in List:
i.pop('group')
if deal != 0:
i.pop('joinTimestamp')
i.pop('lastSpeakTimestamp')
i.pop('muteTimeRemaining')
return List
#取得指定群的群成员资料,返回值为字典
#参数名称:id-qq号,memberName-群名片,specialTitle-群头衔,permission-权限,muteTimeRemaining-禁言剩余时间
#若传入deal=0,则还可返回joinTimestamp-入群时间戳,lastSpeakTimestamp-最后一次发言时间戳
def Get_memberinfo(data, target, id, deal=1):
session = data['session']
host = data['host']
target = str(target)
id = str(id)
url = 'http://' + host + '/memberInfo?sessionKey=' + session + '&target=' + target + '&memberId=' + id
res = r.request('get', url)
res = json.loads(res.text)
res.pop('group')
if deal == 1:
res.pop('joinTimestamp')
res.pop('lastSpeakTimestamp')
return res
elif deal == 0:
return res
else:
return 0