Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: go-cqhttp上报消息格式为array时发送任意消息报错TypeError: expected string or bytes-like object #436

Closed
pokebox opened this issue Apr 25, 2023 · 9 comments
Labels
bug? Bug或Bug修复相关 dependencies 依赖库相关 IM: aiocqhttp onebot协议适配器相关

Comments

@pokebox
Copy link

pokebox commented Apr 25, 2023

部署方式

手动部署

登录框架

go-cqhttp

系统环境

Ubuntu 20.04 LTS

Python环境

Python 3.10.6

异常情况

运行python main.py并确认连接上go-cqhttp完成登录后,发送任意消息时均导致报错并卡死。
其中go-cqhttp的上报消息格式是 post-format: array,当修改为post-format: string后工作正常。
请考虑支持使用array格式的消息

报错信息

[2023-04-25 11:05:51.524] base_events.py (1744) - [ERROR] : Task exception was never retrieved
future: <Task finished name='Task-2' coro=<CQHTTP.ws_event() done, defined at /home/user/.local/lib/python3.10/site-packages/nakuru/application.py:56> exception=TypeError('expected string or bytes-like object')>
Traceback (most recent call last):
File "/home/user/.local/lib/python3.10/site-packages/nakuru/application.py", line 69, in ws_event
received_data = MessageTypes[received_data["message_type"]].parse_obj(received_data)
File "pydantic/main.py", line 526, in pydantic.main.BaseModel.parse_obj
File "/home/user/.local/lib/python3.10/site-packages/nakuru/event/models.py", line 33, in init
message = parser.parseChain(message)
File "/home/user/.local/lib/python3.10/site-packages/nakuru/misc.py", line 104, in parseChain
text = self.plainToCQ(text)
File "/home/user/.local/lib/python3.10/site-packages/nakuru/misc.py", line 72, in plainToCQ
cqIndex = self.__getCQIndex(text)
File "/home/user/.local/lib/python3.10/site-packages/nakuru/misc.py", line 50, in __getCQIndex
for m in re.compile("([CQ:(.+?)])").finditer(text):
TypeError: expected string or bytes-like object

@pokebox pokebox added the bug? Bug或Bug修复相关 label Apr 25, 2023
@pokebox pokebox changed the title [Bug]: go-cqhttp上报消息格式为array时发送任意消息时报错TypeError: expected string or bytes-like object [Bug]: go-cqhttp上报消息格式为array时发送任意消息报错TypeError: expected string or bytes-like object Apr 25, 2023
@RockChinQ RockChinQ added dependencies 依赖库相关 IM: aiocqhttp onebot协议适配器相关 labels Apr 25, 2023
@RockChinQ
Copy link
Owner

RockChinQ commented Apr 25, 2023

麻烦参考 #402 开启调试模式之后贴上详细输出信息;你这是使用插件发送消息时的报错吗?

@pokebox
Copy link
Author

pokebox commented Apr 25, 2023

麻烦参考 #402 开启调试模式之后贴上详细输出信息;你这是使用插件发送消息时的报错吗?

手动安装,没有安装和使用任何其他插件,上面的内容其实就是完整的报错信息。
附上其他的部分:

[2023-04-25 11:24:02.031] host.py (90) - [INFO] : 加载插件
[2023-04-25 11:24:02.031] host.py (94) - [DEBUG] : {}
[2023-04-25 11:24:02.031] switch.py (45) - [DEBUG] : 加载开关数据
[2023-04-25 11:24:02.032] settings.py (38) - [DEBUG] : 加载plugins/settings.json数据
[2023-04-25 11:24:02.032] host.py (107) - [INFO] : 初始化插件
[2023-04-25 11:24:02.032] main.py (211) - [INFO] : 使用账号: 22*****51
[2023-04-25 11:24:02.032] selector_events.py (54) - [DEBUG] : Using selector: EpollSelector
[2023-04-25 11:24:02.109] application.py (59) - [INFO] : Protocol: connected
[2023-04-25 11:24:14.040] main.py (274) - [INFO] : host: 192.168.1.3, port: 6700, http_port: 5700
[2023-04-25 11:24:14.041] main.py (275) - [CRITICAL] : 程序启动完成,如长时间未显示 "Protocol: connected" ,并且不回复消息,请检查config.py中的nakuru_config是否正确
[2023-04-25 11:24:19.046] connectionpool.py (1003) - [DEBUG] : Starting new HTTPS connection (1): api.github.com:443
[2023-04-25 11:24:20.004] connectionpool.py (456) - [DEBUG] : https://api.github.com:443 "GET /repos/RockChinQ/QChatGPT/releases HTTP/1.1" 200 None
[2023-04-25 11:24:20.007] main.py (304) - [INFO] : 当前已是最新版本
[2023-04-25 11:24:20.008] connectionpool.py (1003) - [DEBUG] : Starting new HTTPS connection (1): api.github.com:443
[2023-04-25 11:24:20.907] connectionpool.py (456) - [DEBUG] : https://api.github.com:443 "GET /repos/RockChinQ/QChatGPT/contents/res/announcement.json HTTP/1.1" 200 576
[2023-04-25 11:24:25.141] base_events.py (1744) - [ERROR] : Task exception was never retrieved
future: <Task finished name='Task-2' coro=<CQHTTP.ws_event() done, defined at /home/user/.local/lib/python3.10/site-packages/nakuru/application.py:56> exception=TypeError('expected string or bytes-like object')>
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.10/site-packages/nakuru/application.py", line 69, in ws_event
    received_data = MessageTypes[received_data["message_type"]].parse_obj(received_data)
  File "pydantic/main.py", line 526, in pydantic.main.BaseModel.parse_obj
  File "/home/user/.local/lib/python3.10/site-packages/nakuru/event/models.py", line 33, in __init__
    message = parser.parseChain(message)
  File "/home/user/.local/lib/python3.10/site-packages/nakuru/misc.py", line 104, in parseChain
    text = self.plainToCQ(text)
  File "/home/user/.local/lib/python3.10/site-packages/nakuru/misc.py", line 72, in plainToCQ
    cqIndex = self.__getCQIndex(text)
  File "/home/user/.local/lib/python3.10/site-packages/nakuru/misc.py", line 50, in __getCQIndex
    for m in re.compile("(\[CQ:(.+?)])").finditer(text):
TypeError: expected string or bytes-like object
[2023-04-25 11:25:00.056] ratelimit.py (55) - [DEBUG] : 清空当前分钟的对话次数

@RockChinQ
Copy link
Owner

发了什么内容给机器人?

@pokebox
Copy link
Author

pokebox commented Apr 25, 2023

发了什么内容给机器人?

发送任何文字都会导致报错。无论是!开头的指令或是纯中文/英文文本

@RockChinQ
Copy link
Owner

提供一下gocq的版本以及输出

@pokebox
Copy link
Author

pokebox commented Apr 25, 2023

请注意留意消息格式,这里go-cqhttp使用的是array格式的上报消息,示例如下:

{"post_type":"message","message_type":"private","time":1682421865,"self_id":2200000051,"sub_type":"friend","font":0,"sender":{"age":0,"nickname":"PokeBox","sex":"unknown","user_id":930000005},"message_id":1750478322,"user_id":930000005,"target_id":2200000051,"message":[{"type":"text","data":{"text":"hi"}}],"raw_message":"hi"}

和string格式的上报消息不同,需要对消息格式做处理。
下面是能正常使用的string的消息格式:

{"post_type":"message","message_type":"private","time":1682421386,"self_id":2200000051,"sub_type":"friend","message_id":1642095065,"user_id":930000005,"target_id":2200000051,"message":"!help","raw_message":"!help","font":0,"sender":{"age":0,"nickname":"PokeBox","sex":"unknown","user_id":930000005}}

@pokebox
Copy link
Author

pokebox commented Apr 25, 2023

报错导致的原因与go-cqhttp版本并无太大关系,当我使用的go-cqhttp版本是v1.0.0-rc5,目前几乎所有版本均支持string或array两种消息格式,设置方法为编辑config.yml文件,找到message下的post-format选项,并把值设置为array:

message:
  # 上报数据类型
  # 可选: string,array
  post-format: array

重启go-cqhttp和QChatGPT后,给机器人发送任意消息,即可复现上述bug。

@RockChinQ
Copy link
Owner

这应该是依赖库nakuru的兼容性问题

@RockChinQ
Copy link
Owner

RockChinQ commented Apr 25, 2023

已在由我们维护的nakuru-project 分支仓库中跟踪此问题 idoknow/nakuru-project-idk#2 ,目前请先尝试设置消息格式为纯文本,或尝试使用mirai

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug? Bug或Bug修复相关 dependencies 依赖库相关 IM: aiocqhttp onebot协议适配器相关
Projects
None yet
Development

No branches or pull requests

2 participants