python-wechaty icon indicating copy to clipboard operation
python-wechaty copied to clipboard

bug:python-wechaty库不能获取到微信备注(alias)

Open Huameng11 opened this issue 3 years ago • 13 comments

alias = await self.puppet.contact_alias(self.contact_id, new_alias) 此处语句进行备注设置时可以正常运行,但用于Get aias时,产生内部错误,internal error

Huameng11 avatar Sep 20 '21 04:09 Huameng11

python版本如果存在alias,则直接替换name字段了。

canwushuang avatar Sep 22 '21 09:09 canwushuang

python版本如果存在alias,则直接替换name字段了。

python版本如果存在alias,则直接替换name字段了。

嗷嗷,这样啊,谢谢你。这样的话,是只能set 备注了么

Huameng11 avatar Sep 22 '21 10:09 Huameng11

alias只能赋值不能获取,赋值 await contact.alias('newalias') 即可,contact的格式是Contact <@b543b89ada0184104fc219f22387b4b52702434250f66fa**> 。另外,wechaty守护程序需要重启,联系人才能使用新alias搜索,否则只能使用之前的alias。

canwushuang avatar Sep 22 '21 14:09 canwushuang

这样啊,明白了,谢谢你

Huameng11 avatar Sep 23 '21 01:09 Huameng11

看了下代码,获取alias可以用contact.payload.alias获取。 contacts: List[Contact] = await bot.Contact.find_all() for contact in contacts: contacts_dict[contact.contact_id] = (contact.name,contact.payload.alias,contact.type())

canwushuang avatar Sep 23 '21 10:09 canwushuang

嗷嗷,学到了,一会去试试。再次感谢大佬!

Huameng11 avatar Sep 23 '21 10:09 Huameng11

python版本如果存在alias,则直接替换name字段了。

Thanks for share your understanding about python-wechaty. But I want to correct you that if there is contact alias, it will return it from contact.alias() method. alias is different with name, and python-wechaty will not replace name with alias. It will only happen in mention_text method which whill return alias or name. @canwushuang hope that it will improve your understanding about python-wechaty. For details, you can read code from alias method defination.

@Huameng11 you can refer to ding-dong-bot-oop.py to checkout the example code in your learning.

wj-Mcat avatar Sep 23 '21 12:09 wj-Mcat

python版本如果存在alias,则直接替换name字段了。

Thanks for share your understanding about python-wechaty. But I want to correct you that if there is contact alias, it will return it from contact.alias() method. alias is different with name, and python-wechaty will not replace name with alias. It will only happen in mention_text method which whill return alias or name. @canwushuang hope that it will improve your understanding about python-wechaty. For details, you can read code from alias method defination.

@Huameng11 you can refer to ding-dong-bot-oop.py to checkout the example code in your learning.

Thanks for sharing! I traced the log and read the contact.py at the beginning, the private function ’str‘ in line 182, it return contact’s alias preferred.

canwushuang avatar Sep 23 '21 14:09 canwushuang

alias只能赋值不能获取,赋值 await contact.alias('newalias') 即可,contact的格式是Contact <@b543b89ada0184104fc219f22387b4b52702434250f66fa**> 。另外,wechaty守护程序需要重启,联系人才能使用新alias搜索,否则只能使用之前的alias。

有没有不用重启wechaty的方法呀?每次重启wechaty后都得重新登陆,contactid也都会改变

coderwithai avatar Oct 07 '21 03:10 coderwithai

怎么样能获取到 微信的昵称呀

ghost avatar Nov 10 '22 15:11 ghost

there is a name property in contact class, so you can get the name or alias with the following code:

await contact.ready()

# get the name
contact.name

# get the alias

await contact.alias()

you can read more on wechaty docs website.

wj-Mcat avatar Nov 11 '22 02:11 wj-Mcat

联系人类中有一个name属性,因此您可以使用以下代码获取名称或别名:

await contact.ready()

# get the name
contact.name

# get the alias

await contact.alias()

您可以在 wechaty文档网站上阅读更多内容。

class MyBot(Wechaty):

    async def on_message(self, contact: Contact) -> None:
       await contact.ready()
       print(contact.name)

asyncio.run(MyBot().start())

我尝试模仿文档里的方法,来获取 别名或昵称,但是失败了,本人刚学习py,如果作者能够提供完整的代码最好了,谢谢开发者!

2022-11-11 23:48:35,652 - Wechaty - ERROR - internal error <'Message' object has no attribute 'name'>

ghost avatar Nov 11 '22 15:11 ghost

The parameters of on_message is message instance, not contact. All error infos can be found in terminal. Hope that you can fix this small error.

wj-Mcat avatar Nov 12 '22 00:11 wj-Mcat