This repository has been archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
112 lines (95 loc) · 3.2 KB
/
converter.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
# -*- coding: utf-8 -*-
"""
进行 xhup-club-api 与 cqhttp 之间的消息转换
"""
from aiocqhttp.message import Message, MessageSegment
from config import BotConfig
def is_at_me(msg, self_id):
self_id = int(self_id)
for item in msg:
if item['type'] == 'at' \
and int(item['data']['qq']) == self_id:
return True
elif item['type'] == 'text' \
and BotConfig.NICKNAME in item['data']['text']:
return True
return False
def extract_images(msg):
res = []
for item in msg:
if item['type'] == 'image':
img = item['data']
res.append({
"filename": img['file'],
"url": img['url'],
})
return res
def cq_2_xhup(info):
"""
将 cqhttp 消息转换成 xhup 需要的格式
:param info:
:return:
"""
res = {
"platform": 'qq',
"type": info['post_type'],
}
if res['type'] == "message":
return {
**res,
"message": { # 如果 update 类型是 message
"type": info.get("message_type"), # 'private' or 'group'
"user": {
"id": info.get('user_id'), # 用户 id,QQ 号等
"nickname": info['sender'].get('nickname'),
"role": info['sender'].get('role'), # 群组 owner/admin/member,非群组消息时,它为 None
},
"group": {
"id": info['group_id'], # 群 id
"name": info['group_id'], # 没有提供群名称,直接用 group_id 了。
"at_me": is_at_me(info['message'], info['self_id']), # 是否是 at 我
} if info['message_type'] == "group" else None,
"text": info['message'].extract_plain_text().strip(), # 消息的 text 部分。(去除掉了表情、at 和多媒体数据)
"images": extract_images(info['message']), # 图片路径
},
}
elif res['type'] == "notice":
pass
# return {
# **res,
# "notice": { # 如果 update 类型是 notice 的话
# # TODO 暂时未实现
# }
# }
def xhup_2_cq(info):
"""
将 xhup 消息转换成 cqhttp 需要的格式
:param info:
:return:
"""
reply_msg = info['message']
msg_type = reply_msg['type']
res = {
"message_type": msg_type,
"user_id": None,
"group_id": None,
"message": Message(),
}
if msg_type == "private":
res['user_id'] = reply_msg['user']['id']
elif msg_type == "group":
res['group_id'] = reply_msg['group']['id']
at_members = reply_msg['group']['at_members']
if at_members:
for m_id in at_members:
res['message'].append(
MessageSegment.at(m_id))
# 组建 Message
if reply_msg['text']:
res['message'].append(
MessageSegment.text(reply_msg['text']))
# if reply_msg['images']: # 图片暂不支持
# for img in reply_msg['images']:
# info['message'].append(
# MessageSegment.image())
return res