tencent-connect / botpy

QQ频道机器人PythonSDK
MIT License
581 stars 87 forks source link

在使用self.api.post_message被动发送本地图片时,推送失败 #119

Closed Gintamashiichan closed 2 years ago

Gintamashiichan commented 2 years ago

运行环境:Win10,py3.8

问题描述:在使用self.api.post_message发送本地图片时,出现 TypeError: Can not serialize value type: <class 'dict'>

日志:

Traceback (most recent call last):
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\formdata.py", line 143, in _gen_form_data
    part = payload.get_payload(
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\payload.py", line 73, in get_payload
    return PAYLOAD_REGISTRY.get(data, *args, **kwargs)
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\payload.py", line 112, in get
    raise LookupError()
aiohttp.payload.LookupError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\botpy\client.py", line 275, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\xx\Desktop\qq-guildbook\bot.py", line 39, in on_at_message_create
    await self.api.post_message(
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\botpy\api.py", line 518, in post_message
    return await self._http.request(route, json=payload)
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\botpy\http.py", line 118, in request
    async with self._session.request(method=route.method, url=route.url, **kwargs) as response:
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\client.py", line 1117, in __aenter__
    self._resp = await self._coro
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\client.py", line 492, in _request
    req = self._request_class(
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\client_reqrep.py", line 313, in __init__
    self.update_body_from_data(data)
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\client_reqrep.py", line 507, in update_body_from_data
    body = body()
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\formdata.py", line 168, in __call__
    return self._gen_form_data()
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\formdata.py", line 147, in _gen_form_data
    raise TypeError(
TypeError: Can not serialize value type: <class 'dict'>
 headers: {}
 value: {'message_id': '0884e6d78bb9e2aaf1990110ecf79a01384048b3c5bd9806'}

代码复现:

...
def download_img(img_url,path):
    r = requests.get(img_url, stream=True)
    if r.status_code == 200:
        open(os.getcwd()+path, 'wb').write(r.content)
        return os.getcwd()+path
    del r
...
class MyClient(botpy.Client):
    async def on_guild_member_add(self, member: Member):
        pass
    async def on_at_message_create(self, message: Message):        
        message_reference = Reference(message_id=message.id)
        a=await self.api.get_message(channel_id=message.channel_id, message_id=message.id)
        if str(a['message']['content'][23:len(a['message']['content'])]).startswith("xxx"):
            api=transfurutil.Tailapi("./config/bot.conf")
            context=api.getFursuitRand()
            await self.api.post_message(
                channel_id=message.channel_id,
                content="xx{} {}".format(context['data']['id'],context['data']['name']),
                file_image=download_img(context['data']['url'],'\cache\{}.jpg'.format(context['data']['id'])),#在尝试上传图片的这一步报错
                msg_id=message.id,
                message_reference=message_reference,
            )
...

这看起来像是aiohttp的序列化问题, 我在 https://bleepcoder.com/cn/aiohttp/421745175/typeerror-can-not-serialize-value-type-class-int 找到了相关问题,但是并没有找到合适的解决方案 我该如何解决这个问题?

GLGDLY commented 2 years ago

你目前在用的botpy版本是什么呢?我看了看报错的traceback,你目前应该不是在用最新的代码,可以尝试git clone使用github目前的最新代码试试

GLGDLY commented 2 years ago

对了,刚想起来,你这个问题是我在 #108 的pr中解决了, #109 这个issue提出的问题是跟你一样的