Skip to content

Commit

Permalink
Merge pull request #39 from 1qa2ws3ed4rf1/onebotv11fix
Browse files Browse the repository at this point in the history
Onebot v11 协议问题:AttributeError: 'int' object has no attribute 'encode…
  • Loading branch information
This-is-XiaoDeng authored Oct 12, 2024
2 parents 1b9463d + 12a31a0 commit d54ba6b
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions network/v11/http_post.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import aiohttp
from utils.logger import get_logger
import asyncio
import utils.event as event
Expand Down Expand Up @@ -30,7 +31,21 @@ def __del__(self) -> None:
asyncio.create_task(
self.push_event(event.get_event_object("meta", "lifecycle", "disable"))
)
async def post_request(self,url, data, headers):
"""
尝试修复post bug
args:
url:目标url
data:json data
headers:头
"""
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data, headers=headers) as response:
return response.status,response
except aiohttp.ClientError as e:
print(f"An error occurred: {e}")
async def push_event(self, _event: dict) -> None:
"""
推送事件
Expand All @@ -40,24 +55,25 @@ async def push_event(self, _event: dict) -> None:
"""
event = await translator.translate_event(_event)
async with httpx.AsyncClient(timeout=self.config["timeout"]) as client:
response = await client.post(

response_code,response = await self.post_request(
self.config["url"],
content=(content := json.dumps(event)),
headers=self.get_headers(content),
json.dumps(event),
self.get_headers(json.dumps(event))
)
if response.status_code == 204:
if response_code == 204:
return
elif response.status_code == 200:
elif response_code == 200:
content = response.json()
logger.debug(f"收到快速操作请求:{content}")
await quick_operation.handle_quick_operation(content, event)
else:
logger.warning(f"上报事件时发生错误:{response.status_code}")

def get_headers(self, content: str) -> dict:
headers = {
"Content-Type": "application/json",
"X-Self-ID": client.user.id, # type: ignore
"X-Self-ID": str(client.user.id), # type: ignore
}
if self.config["secret"]:
headers["X-Signature"] = (
Expand Down

0 comments on commit d54ba6b

Please sign in to comment.