能够接收到私聊(暂未测试)和群组的图片信息,并将图片保存在本地。
This commit is contained in:
parent
64b1875c22
commit
8b7cc1c55b
19
docs/踩坑.md
Normal file
19
docs/踩坑.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
## ncatbot 开发踩的坑
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
由于一上来就采用了远程开发的模式,所以在bot连接的过程中,需要在 ```bot.run()``` 函数中另外传入参数
|
||||||
|
```python
|
||||||
|
bot.run(
|
||||||
|
bt_uin = "bot account",
|
||||||
|
root = "bot's master account",
|
||||||
|
ws_uri = "ws://",
|
||||||
|
ws_token = "Your token",
|
||||||
|
webui_uri = "http://Your server:port",
|
||||||
|
webui_token = "Your token"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
具体的信息在 [配置项文档](https://docs.ncatbot.xyz/guide/kfcvme50/#%E9%85%8D%E7%BD%AE%E9%A1%B9%E5%88%97%E8%A1%A8) 中提及。
|
||||||
|
一些报错是由于webui的token所导致的,默认值为napcat,另外连接都需要带上协议,例如连接webui时需要带上 ```http://```
|
||||||
|
---
|
||||||
|
|
92
src/main.py
92
src/main.py
@ -0,0 +1,92 @@
|
|||||||
|
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"
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user