wechaty / python-wechaty

Python Wechaty is a Conversational RPA SDK for Chatbot Makers written in Python
https://wechaty.readthedocs.io/zh_CN/latest/
Apache License 2.0
1.55k stars 229 forks source link

[Bug]: 无法获取联系人头像 #381

Open frostming opened 1 year ago

frostming commented 1 year ago

Environment

* the version of wechaty:                       0.8.66
* the version of wechaty_puppet:                0.4.23
* the version of wechaty_puppet_service:        0.8.10
* the token type: <unkonwn> token
- the version of wechaty docker container: [1.20.2]

Description

Whenever I tried to get the contact avatar by await contact.avatar(), the terminal logs the following error:

2022-12-16 15:43:40,814 - Wechaty - ERROR - internal error <argument of type 'NoneType' is not iterable>

Yes, there are no other related error logs except this line, which makes me really confusing. There seems to be no other documented way to get the avatar of a contact.

Minimum reproducible code

async def on_message(self, msg: Message) -> None:
    if msg.type() == MessageType.MESSAGE_TYPE_TEXT:
        text = format_msg_text(msg.text())
        if text:
            contact = msg.talker()
            await contact.avatar()  # <= this line logs error
        ...
frostming commented 1 year ago

Oh, I managed to get the traceback:

Traceback (most recent call last):
  File "/mnt/c/Workspace/GitHub/chatroom-syncer/src/chatroom_syncer/room_syncer.py", line 38, in on_message
    await contact.avatar()
  File "/mnt/c/Workspace/GitHub/chatroom-syncer/.venv/lib/python3.10/site-packages/wechaty/user/contact_self.py", line 36, in avatar
    file_box = await super().avatar(None)
  File "/mnt/c/Workspace/GitHub/chatroom-syncer/.venv/lib/python3.10/site-packages/wechaty/user/contact.py", line 580, in avatar
    avatar = await self.puppet.contact_avatar(
  File "/mnt/c/Workspace/GitHub/chatroom-syncer/.venv/lib/python3.10/site-packages/wechaty_puppet_service/puppet.py", line 563, in contact_avatar
    return FileBox.from_json(response.filebox)
  File "/mnt/c/Workspace/GitHub/chatroom-syncer/.venv/lib/python3.10/site-packages/wechaty_puppet/file_box/file_box.py", line 363, in from_json
    if 'boxType' not in json_obj:
TypeError: argument of type 'NoneType' is not iterable

It seems to be caused by API change and the response body is empty

wey-gu commented 1 year ago

Are you saying that this change of filebox --> file_box led to the filebox empty?

https://github.com/wechaty/python-wechaty-puppet-service/blob/5174faf061ea705dc887d615f1268a8c5d4b886a/src/wechaty_puppet_service/puppet.py#L561

I am not familiar with gRPC, but it looks like the python-wechaty-puppet-service side is still speaking the gRPC in a version without the (filebox --> file_box) change:

# print(response)
<wechaty_grpc.wechaty.puppet.ContactAvatarResponse object at 0x7f6aca819040>

# print(dir(response))
['FromString', 'SerializeToString', '__abstractmethods__', '__annotations__', '__bytes__', '__class__', '__dataclass_fields__', '__dataclass_params__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__post_init__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_abc_impl', '_betterproto', '_betterproto_meta', '_cls_for', '_get_field_default', '_get_field_default_gen', '_group_map', '_postprocess_single', '_serialized_on_wire', '_type_hint', '_unknown_fields',
'filebox', 'from_dict', 'from_json', 'parse', 'to_dict', 'to_json']
❯ pip freeze | grep wechaty
wechaty==0.8.66
wechaty-grpc==0.20.19
wechaty-puppet==0.4.23
wechaty-puppet-service==0.8.10

It's strange that the grpc was not tagged in this version in github

wey-gu commented 1 year ago

Or we understood this contact avatar interface wrongly, it's designed to get only the avatar of the bot himself/herself? Or the UOS puppet doesn't support such call(contactAvatar)?

frostming commented 1 year ago

Are you saying that this change of filebox --> file_box led to the filebox empty?

https://github.com/wechaty/python-wechaty-puppet-service/blob/5174faf061ea705dc887d615f1268a8c5d4b886a/src/wechaty_puppet_service/puppet.py#L561

I am not familiar with gRPC, but it looks like the python-wechaty-puppet-service side is still speaking the gRPC in a version without the (filebox --> file_box) change:

# print(response)
<wechaty_grpc.wechaty.puppet.ContactAvatarResponse object at 0x7f6aca819040>

# print(dir(response))
['FromString', 'SerializeToString', '__abstractmethods__', '__annotations__', '__bytes__', '__class__', '__dataclass_fields__', '__dataclass_params__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__post_init__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_abc_impl', '_betterproto', '_betterproto_meta', '_cls_for', '_get_field_default', '_get_field_default_gen', '_group_map', '_postprocess_single', '_serialized_on_wire', '_type_hint', '_unknown_fields',
'filebox', 'from_dict', 'from_json', 'parse', 'to_dict', 'to_json']
❯ pip freeze | grep wechaty
wechaty==0.8.66
wechaty-grpc==0.20.19
wechaty-puppet==0.4.23
wechaty-puppet-service==0.8.10

It's strange that the grpc was not tagged in this version in github

Good catch, so it is confirmed, the change is introduced in Oct, 2021, while the latest stable release of wechaty-grpc is in Feb, 2021, I will bump the version to see whether it works