nonebot / adapter-qq

NoneBot2 QQ 适配器 / QQ adapter for nonebot2
MIT License
210 stars 18 forks source link

Fix: 修复在使用file_image时,请求中的msg_id和content等字段失效 #50

Closed iRis7656 closed 1 year ago

iRis7656 commented 1 year ago

修复在使用file_image时,请求中的msg_id和content等字段丢失。这个问题会导致文本消息未发送或消息被识别为主动消息的情况

yanyongyu commented 1 year ago

剩余的字段不是都添加了吗,哪里丢了

https://github.com/nonebot/adapter-qqguild/blob/d4522024404d45334c17e1a8dac9d2c57181aa32/nonebot/adapters/qqguild/api/utils.py#L12-L22

iRis7656 commented 1 year ago

我的修改确实是不对的。但我发现data_[k] = (None, v.encode("utf-8"), "text/plain")中,encode后的字段没有被正常识别,不进行encode直接传入v消息正常。如图,上为原代码,下为删去encode后效果: Screenshot_20230824_161128

yanyongyu commented 1 year ago

如果要改成str的话需要修改nb本体,能否尝试最新nb master分支是否能发送encode后的内容

yanyongyu commented 1 year ago

看了下httpx源码,不管有没有encode最后都是会encode一下变成utf8 bytes的

https://github.com/encode/httpx/blob/0f61aa58d66680c239ce43c8cdd453e7dc532bfc/httpx/_multipart.py#L170-L173

iRis7656 commented 1 year ago

我换成最新master分支是一样的结果。既然都会encode这就很奇怪了,删掉确实有效果,希望能找到真正的解决办法吧。之前有群友也遇到一样的情况

yanyongyu commented 1 year ago

你是使用的httpx还是aiohttp

iRis7656 commented 1 year ago

这个是可以自己选的嘛,是指驱动器?只开启了DRIVER=~aiohttp

yanyongyu commented 1 year ago

你尝试一下使用httpx

iRis7656 commented 1 year ago

换成DRIVER=~httpx+~websockets后原代码正常运行了

yanyongyu commented 1 year ago

那看来是aiohttp这边的问题

yanyongyu commented 1 year ago

看aiohttp的代码,string也是utf-8转bytes的

https://github.com/aio-libs/aiohttp/blob/db2c274c91d17eb410225d504c83d4bd6971d0ae/aiohttp/payload.py#L247-L274

yanyongyu commented 1 year ago

看了下aiohttp在value是bytes,filename为none的时候会设置为field name,可能是这个问题

https://github.com/aio-libs/aiohttp/blob/db2c274c91d17eb410225d504c83d4bd6971d0ae/aiohttp/formdata.py#L56-L58

yanyongyu commented 1 year ago

破案了,就是aiohttp自动给bytes类型的字段加了filename导致频道丢了这部分内容