nonebot-plugin-aawfonebot 0.23.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nonebot_plugin_aawfonebot-0.23.1/PKG-INFO +7 -0
- nonebot_plugin_aawfonebot-0.23.1/nonebot_plugin_aawfonebot/__init__.py +18 -0
- nonebot_plugin_aawfonebot-0.23.1/nonebot_plugin_aawfonebot/media.py +103 -0
- nonebot_plugin_aawfonebot-0.23.1/nonebot_plugin_aawfonebot/router.py +137 -0
- nonebot_plugin_aawfonebot-0.23.1/nonebot_plugin_aawfonebot.egg-info/PKG-INFO +7 -0
- nonebot_plugin_aawfonebot-0.23.1/nonebot_plugin_aawfonebot.egg-info/SOURCES.txt +9 -0
- nonebot_plugin_aawfonebot-0.23.1/nonebot_plugin_aawfonebot.egg-info/dependency_links.txt +1 -0
- nonebot_plugin_aawfonebot-0.23.1/nonebot_plugin_aawfonebot.egg-info/requires.txt +2 -0
- nonebot_plugin_aawfonebot-0.23.1/nonebot_plugin_aawfonebot.egg-info/top_level.txt +1 -0
- nonebot_plugin_aawfonebot-0.23.1/pyproject.toml +17 -0
- nonebot_plugin_aawfonebot-0.23.1/setup.cfg +4 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nonebot-plugin-aawfonebot
|
|
3
|
+
Version: 0.23.1
|
|
4
|
+
Summary: OneBot V11 adapter entrypoint for the shared AAWarframe core
|
|
5
|
+
Requires-Python: ==3.12.*
|
|
6
|
+
Requires-Dist: nonebot-plugin-aawfcore==0.23.1
|
|
7
|
+
Requires-Dist: nonebot-adapter-onebot<3,>=2.4.6
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""AAWarframe OneBot V11 前端:仅负责输入事件、图片与平台输出。"""
|
|
2
|
+
|
|
3
|
+
from nonebot import require
|
|
4
|
+
from nonebot.plugin import PluginMetadata
|
|
5
|
+
|
|
6
|
+
__plugin_meta__ = PluginMetadata(
|
|
7
|
+
name="aawfonebot",
|
|
8
|
+
description="AAWarframe OneBot V11 协议插件",
|
|
9
|
+
usage="使用帮助命令查看 Warframe 机器人支持的查询功能",
|
|
10
|
+
type="application",
|
|
11
|
+
supported_adapters={"~onebot.v11"},
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
require("nonebot_plugin_aawfcore")
|
|
15
|
+
|
|
16
|
+
from .router import setup # noqa: E402
|
|
17
|
+
|
|
18
|
+
setup()
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""OneBot V11 引用消息/图片地址读取;仅负责协议媒体 IO。"""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import logging
|
|
5
|
+
from urllib.parse import urljoin, urlsplit
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
from nonebot.adapters.onebot.v11 import Message
|
|
9
|
+
|
|
10
|
+
MAX_IMAGE_BYTES = 12 * 1024 * 1024
|
|
11
|
+
ALLOWED_DOMAINS = ("qq.com", "qq.com.cn", "qpic.cn")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def download_image(url: str) -> bytes | None:
|
|
15
|
+
async with httpx.AsyncClient(timeout=25) as client:
|
|
16
|
+
for _ in range(4):
|
|
17
|
+
parts = urlsplit(url)
|
|
18
|
+
domain = (parts.hostname or "").lower()
|
|
19
|
+
if (
|
|
20
|
+
parts.scheme not in {"http", "https"}
|
|
21
|
+
or not any(domain == item or domain.endswith("." + item) for item in ALLOWED_DOMAINS)
|
|
22
|
+
or parts.username or parts.password
|
|
23
|
+
or parts.port not in {None, 80, 443}
|
|
24
|
+
):
|
|
25
|
+
return None
|
|
26
|
+
async with client.stream("GET", url, follow_redirects=False) as response:
|
|
27
|
+
if response.is_redirect:
|
|
28
|
+
url = urljoin(url, response.headers.get("location", ""))
|
|
29
|
+
continue
|
|
30
|
+
response.raise_for_status()
|
|
31
|
+
declared = response.headers.get("content-length")
|
|
32
|
+
if declared and declared.isdecimal() and int(declared) > MAX_IMAGE_BYTES:
|
|
33
|
+
raise ValueError("图片超过 12 MB")
|
|
34
|
+
data = bytearray()
|
|
35
|
+
async for chunk in response.aiter_bytes(chunk_size=65536):
|
|
36
|
+
data.extend(chunk)
|
|
37
|
+
if len(data) > MAX_IMAGE_BYTES:
|
|
38
|
+
raise ValueError("图片超过 12 MB")
|
|
39
|
+
return bytes(data)
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def decode_inline(value: str) -> bytes | None:
|
|
44
|
+
if not value.startswith("base64://"):
|
|
45
|
+
return None
|
|
46
|
+
encoded = value[len("base64://"):]
|
|
47
|
+
if len(encoded) > (MAX_IMAGE_BYTES + 2) // 3 * 4 + 16:
|
|
48
|
+
raise ValueError("图片超过 12 MB")
|
|
49
|
+
image = base64.b64decode(encoded, validate=True)
|
|
50
|
+
if len(image) > MAX_IMAGE_BYTES:
|
|
51
|
+
raise ValueError("图片超过 12 MB")
|
|
52
|
+
return image
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
logger = logging.getLogger(__name__)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def image_segments(message):
|
|
59
|
+
return [item for item in Message(message) if item.type == "image"]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def message_image(bot, event) -> bytes:
|
|
63
|
+
images = image_segments(event.message)
|
|
64
|
+
if not images:
|
|
65
|
+
reply = getattr(event, "reply", None)
|
|
66
|
+
original = getattr(event, "original_message", event.message)
|
|
67
|
+
reply_id = (
|
|
68
|
+
reply.message_id if reply else next(
|
|
69
|
+
(s.data.get("id") for s in original if s.type == "reply"), None
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
if reply_id is not None:
|
|
73
|
+
try:
|
|
74
|
+
data = await bot.get_msg(message_id=int(reply_id))
|
|
75
|
+
images = image_segments(data.get("message") or data.get("raw_message", ""))
|
|
76
|
+
except Exception:
|
|
77
|
+
logger.debug("引用消息读取失败", exc_info=True)
|
|
78
|
+
if not images and reply:
|
|
79
|
+
images = image_segments(reply.message)
|
|
80
|
+
if len(images) != 1:
|
|
81
|
+
raise ValueError("请在“分析”后附上一张紫卡图片,或引用一张图片。")
|
|
82
|
+
image = images[0].data
|
|
83
|
+
urls = []
|
|
84
|
+
if image.get("file") and not image["file"].startswith("base64://"):
|
|
85
|
+
try:
|
|
86
|
+
info = await bot.get_image(file=image["file"])
|
|
87
|
+
if info.get("url"):
|
|
88
|
+
urls.append(info["url"])
|
|
89
|
+
except Exception:
|
|
90
|
+
logger.debug("查询图片地址失败", exc_info=True)
|
|
91
|
+
urls.extend(
|
|
92
|
+
address for address in (image.get("url"), image.get("file"))
|
|
93
|
+
if address and address not in urls
|
|
94
|
+
)
|
|
95
|
+
for address in urls:
|
|
96
|
+
if inline := decode_inline(address):
|
|
97
|
+
return inline
|
|
98
|
+
try:
|
|
99
|
+
if image_data := await download_image(address):
|
|
100
|
+
return image_data
|
|
101
|
+
except httpx.HTTPError:
|
|
102
|
+
continue
|
|
103
|
+
raise ValueError("无法读取图片,请重新上传后分析。")
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""OneBot V11 仅收发消息,通过共享核心 HTTP API 执行业务。"""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import base64
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
from nonebot import get_bots, get_driver, on_message
|
|
8
|
+
from nonebot.adapters import Bot, Event
|
|
9
|
+
from nonebot.adapters.onebot.v11 import Message, MessageSegment
|
|
10
|
+
from nonebot.matcher import Matcher
|
|
11
|
+
from nonebot.typing import T_State
|
|
12
|
+
from nonebot_plugin_aawfcore.client import CoreClient, decode_image
|
|
13
|
+
|
|
14
|
+
from .media import message_image
|
|
15
|
+
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
client = CoreClient()
|
|
18
|
+
task: asyncio.Task | None = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def session(bot: Bot, event: Event):
|
|
22
|
+
user_id = str(getattr(event, "user_id", event.get_user_id()))
|
|
23
|
+
group = getattr(event, "group_id", None)
|
|
24
|
+
return str(bot.self_id), str(group) if group is not None else None, user_id
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def outbound(segments: list[dict]) -> Message:
|
|
28
|
+
message = Message()
|
|
29
|
+
for part in segments:
|
|
30
|
+
kind = part["type"]
|
|
31
|
+
if kind == "text":
|
|
32
|
+
message += MessageSegment.text(part["text"])
|
|
33
|
+
elif kind == "image":
|
|
34
|
+
message += MessageSegment.image(decode_image(part))
|
|
35
|
+
elif kind == "at":
|
|
36
|
+
message += MessageSegment.at(part["user_id"])
|
|
37
|
+
elif kind == "reply":
|
|
38
|
+
message += MessageSegment.reply(int(part["message_id"]))
|
|
39
|
+
else:
|
|
40
|
+
raise ValueError(f"不支持的 OneBot 消息段:{kind}")
|
|
41
|
+
return message
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
async def accept(bot: Bot, event: Event, matcher: Matcher, state: T_State):
|
|
45
|
+
bot_id, group_id, user_id = session(bot, event)
|
|
46
|
+
|
|
47
|
+
async def execute_query():
|
|
48
|
+
image_base64 = None
|
|
49
|
+
if state["aawf_feature"] == "riven_analysis":
|
|
50
|
+
async with client.ocr_download():
|
|
51
|
+
data = await message_image(bot, event)
|
|
52
|
+
image_base64 = base64.b64encode(data).decode("ascii")
|
|
53
|
+
return await client.query(
|
|
54
|
+
feature=state["aawf_feature"], arguments=state["aawf_arguments"],
|
|
55
|
+
bot_id=bot_id, group_id=group_id, user_id=user_id,
|
|
56
|
+
image_base64=image_base64,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
try:
|
|
60
|
+
if state["aawf_feature"] == "riven_analysis":
|
|
61
|
+
async with client.ocr_admission():
|
|
62
|
+
result = await execute_query()
|
|
63
|
+
else:
|
|
64
|
+
result = await execute_query()
|
|
65
|
+
message = outbound(result["segments"])
|
|
66
|
+
except ValueError as exc:
|
|
67
|
+
message = Message(MessageSegment.text(str(exc)))
|
|
68
|
+
except Exception:
|
|
69
|
+
logger.exception("OneBot 核心 API 查询失败")
|
|
70
|
+
message = Message(MessageSegment.text("查询暂不可用,请稍后重试。"))
|
|
71
|
+
await matcher.finish(message)
|
|
72
|
+
|
|
73
|
+
async def notifications():
|
|
74
|
+
while True:
|
|
75
|
+
try:
|
|
76
|
+
bots = {
|
|
77
|
+
str(bot.self_id): bot for bot in get_bots().values()
|
|
78
|
+
if getattr(bot, "type", None) == "OneBot V11"
|
|
79
|
+
}
|
|
80
|
+
if bots:
|
|
81
|
+
for item in await client.claim(list(bots), protocol="onebot"):
|
|
82
|
+
try:
|
|
83
|
+
await bots[item["bot_id"]].send_group_msg(
|
|
84
|
+
group_id=int(item["group_id"]),
|
|
85
|
+
message=outbound(item["segments"]),
|
|
86
|
+
)
|
|
87
|
+
except asyncio.CancelledError:
|
|
88
|
+
raise
|
|
89
|
+
except Exception as exc:
|
|
90
|
+
logger.warning("OneBot 订阅通知发送失败", exc_info=True)
|
|
91
|
+
await client.ack(item["id"], ok=False, error=type(exc).__name__)
|
|
92
|
+
else:
|
|
93
|
+
await client.ack(item["id"], ok=True)
|
|
94
|
+
except asyncio.CancelledError:
|
|
95
|
+
raise
|
|
96
|
+
except Exception:
|
|
97
|
+
logger.exception("OneBot 订阅通知轮询异常")
|
|
98
|
+
await asyncio.sleep(5)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def setup():
|
|
102
|
+
async def rule(bot: Bot, event: Event, state: T_State) -> bool:
|
|
103
|
+
if getattr(bot, "type", None) != "OneBot V11":
|
|
104
|
+
return False
|
|
105
|
+
_, group_id, user_id = session(bot, event)
|
|
106
|
+
try:
|
|
107
|
+
route = await client.route(
|
|
108
|
+
text=event.get_plaintext(), starts=list(get_driver().config.command_start),
|
|
109
|
+
user_id=user_id, group_id=group_id,
|
|
110
|
+
)
|
|
111
|
+
except Exception:
|
|
112
|
+
logger.exception("核心 API 命令匹配失败")
|
|
113
|
+
return False
|
|
114
|
+
if not route["matched"]:
|
|
115
|
+
return False
|
|
116
|
+
state["aawf_feature"] = route["feature"]
|
|
117
|
+
state["aawf_arguments"] = route["arguments"]
|
|
118
|
+
return True
|
|
119
|
+
|
|
120
|
+
matcher = on_message(rule=rule, block=True, priority=10)
|
|
121
|
+
matcher.handle()(accept)
|
|
122
|
+
|
|
123
|
+
driver = get_driver()
|
|
124
|
+
|
|
125
|
+
@driver.on_startup
|
|
126
|
+
async def startup():
|
|
127
|
+
global task
|
|
128
|
+
task = asyncio.create_task(notifications())
|
|
129
|
+
|
|
130
|
+
@driver.on_shutdown
|
|
131
|
+
async def shutdown():
|
|
132
|
+
global task
|
|
133
|
+
if task is not None:
|
|
134
|
+
task.cancel()
|
|
135
|
+
await asyncio.gather(task, return_exceptions=True)
|
|
136
|
+
task = None
|
|
137
|
+
await client.close()
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nonebot-plugin-aawfonebot
|
|
3
|
+
Version: 0.23.1
|
|
4
|
+
Summary: OneBot V11 adapter entrypoint for the shared AAWarframe core
|
|
5
|
+
Requires-Python: ==3.12.*
|
|
6
|
+
Requires-Dist: nonebot-plugin-aawfcore==0.23.1
|
|
7
|
+
Requires-Dist: nonebot-adapter-onebot<3,>=2.4.6
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
pyproject.toml
|
|
2
|
+
nonebot_plugin_aawfonebot/__init__.py
|
|
3
|
+
nonebot_plugin_aawfonebot/media.py
|
|
4
|
+
nonebot_plugin_aawfonebot/router.py
|
|
5
|
+
nonebot_plugin_aawfonebot.egg-info/PKG-INFO
|
|
6
|
+
nonebot_plugin_aawfonebot.egg-info/SOURCES.txt
|
|
7
|
+
nonebot_plugin_aawfonebot.egg-info/dependency_links.txt
|
|
8
|
+
nonebot_plugin_aawfonebot.egg-info/requires.txt
|
|
9
|
+
nonebot_plugin_aawfonebot.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nonebot_plugin_aawfonebot
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "nonebot-plugin-aawfonebot"
|
|
7
|
+
version = "0.23.1"
|
|
8
|
+
description = "OneBot V11 adapter entrypoint for the shared AAWarframe core"
|
|
9
|
+
requires-python = "==3.12.*"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"nonebot-plugin-aawfcore==0.23.1",
|
|
12
|
+
"nonebot-adapter-onebot>=2.4.6,<3",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[tool.setuptools.packages.find]
|
|
16
|
+
where = ["."]
|
|
17
|
+
include = ["nonebot_plugin_aawfonebot*"]
|