Skip to content

Commit 0bc9828

Browse files
✨ Feature: 支持自定义 QQ WebSocket Gateway URL (#209)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
1 parent e7dae1d commit 0bc9828

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

nonebot/adapters/qq/adapter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ async def run_bot_websocket(self, bot_info: BotInfo) -> None:
152152
try:
153153
gateway_info = await bot.shard_url_get()
154154
ws_url = URL(gateway_info.url)
155+
if self.qq_config.qq_custom_gateway_url:
156+
ws_url = self.qq_config.qq_custom_gateway_url
155157
except Exception as e:
156158
log(
157159
"ERROR",

nonebot/adapters/qq/config.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
from nonebot.compat import PYDANTIC_V2, ConfigDict
1+
from typing import Any
2+
3+
from nonebot.compat import PYDANTIC_V2, ConfigDict, field_validator
24
from pydantic import BaseModel, Field, HttpUrl
5+
from yarl import URL
36

47

58
class Intents(BaseModel):
@@ -58,3 +61,21 @@ class Config(BaseModel):
5861
qq_auth_base: HttpUrl = Field("https://bots.qq.com/app/getAppAccessToken") # type: ignore
5962
qq_verify_webhook: bool = True
6063
qq_bots: list[BotInfo] = Field(default_factory=list)
64+
65+
qq_custom_gateway_url: URL | None = None
66+
67+
@field_validator("qq_custom_gateway_url", mode="before")
68+
@classmethod
69+
def validate_gateway_url(cls, v: Any) -> URL | None:
70+
if v is None:
71+
return v
72+
url = URL(v) if isinstance(v, str) else v
73+
if not isinstance(url, URL):
74+
raise TypeError("Invalid gateway url type")
75+
if url.scheme not in ("ws", "wss"):
76+
raise ValueError(
77+
f"Gateway URL scheme must be ws:// or wss://, got {url.scheme}://"
78+
)
79+
if not url.host:
80+
raise ValueError("Gateway URL must have a valid host")
81+
return url

0 commit comments

Comments
 (0)