-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
42 lines (34 loc) · 1.48 KB
/
Copy pathmain.py
File metadata and controls
42 lines (34 loc) · 1.48 KB
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
from astrbot.api.event import filter, AstrMessageEvent
from astrbot.api.star import Context, Star, register
from astrbot.api import logger
from astrbot.core.config.astrbot_config import AstrBotConfig
import httpx
import json
@register("http_bridge", "FlyModeZ", "", "0.1.3")
class HttpBridge(Star):
def __init__(self, context: Context, config: AstrBotConfig):
super().__init__(context)
self.http_endpoint = config.get("http_endpoint")
async def initialize(self):
self.client = httpx.AsyncClient(http2=True, timeout=10.0)
logger.info("HTTP 客户端已初始化。")
async def terminate(self):
if self.client:
await self.client.aclose()
logger.info("HTTP 客户端已关闭。")
# @filter.event_message_type(filter.EventMessageType.ALL, priority=10)
@filter.command("fm")
async def bridge(self, event: AstrMessageEvent):
event.stop_event()
before, sep, after = event.message_str.partition("fm")
m = after if sep else before
payload = {
"user": event.unified_msg_origin,
"text": m.strip()
}
async with self.client.stream("POST", self.http_endpoint, json=payload) as response:
response.raise_for_status()
async for line in response.aiter_lines():
if line:
data = json.loads(line)
yield event.plain_result(data.get("text"))