呃呃重写了解析图片消息的

This commit is contained in:
Pyhtagodzilla 2025-07-19 11:48:46 +08:00
parent 8b7cc1c55b
commit 06266378e1
2 changed files with 4 additions and 93 deletions

5
.gitignore vendored
View File

@ -1 +1,4 @@
tests
tests
.env
src/data/pics
src/logs

View File

@ -1,92 +0,0 @@
from ncatbot.core import BotClient, GroupMessage, PrivateMessage
# from ncatbot.core import (MessageChain, Text, Image)
from ncatbot.utils import get_log
import requests
bot = BotClient()
_log = get_log()
@bot.private_event()
async def on_receive_images_private_event(msg: PrivateMessage):
_log.info(msg)
if "url=https://" not in msg.raw_message:
return
else:
try:
file_name = msg.raw_message.split(",")[1].split("=")[1]
image_url = await bot.api.get_image(file_name)
real_url = image_url['data']['url']
except IndexError as e:
_log.debug(e)
real_url = "None"
file_name = "None"
except TypeError as e:
_log.debug(e)
real_url = "None"
file_name = "None"
if real_url == "None":
return
else:
response = requests.get(real_url)
img = response.content
with open(f"data/pics/{file_name}", 'wb') as f:
f.write(img)
# file_name = msg.raw_message.split(",")[1].split("=")[1]
# image_url = await bot.api.get_image(file_name)
# real_url = image_url['data']['url']
#
# _log.debug(f"image's real url: {real_url}")
#
# try:
# response = requests.get(real_url)
# img = response.content
# with open(f"data/pics/{file_name}", 'wb') as f:
# f.write(img)
# except Exception as e:
# _log.error(e)
# pass
@bot.group_event()
async def on_receive_images_group_event(msg: GroupMessage):
_log.info(msg)
if "url=https://" not in msg.raw_message:
return
else:
try:
file_name = msg.raw_message.split(",")[1].split("=")[1]
image_url = await bot.api.get_image(file_name)
real_url = image_url['data']['url']
except IndexError as e:
_log.debug(e)
real_url = "None"
file_name = "None"
except TypeError as e:
_log.debug(e)
real_url = "None"
file_name = "None"
if real_url == "None":
return
else:
response = requests.get(real_url)
img = response.content
with open(f"data/pics/{file_name}", 'wb') as f:
f.write(img)
if __name__ == "__main__":
api = bot.run(
bt_uin="203758258",
root="1670671958",
ws_uri="ws://192.168.10.78:3001",
ws_token="",
webui_uri="http://192.168.10.78:6099",
webui_token="@Jtbx2mtblj"
)