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.65k stars 233 forks source link

用Friendship的add()添加好友不成功且报错 #167

Closed huangmu168 closed 3 years ago

huangmu168 commented 3 years ago

代码如下:

        be_add_friend = await Friendship.search(phone='18954072510') #自己测试号
        print(be_add_friend)
        print(be_add_friend.contact_id)
        await be_add_friend.ready()
        await Friendship.add(contact=be_add_friend , hello='大哥你好,求助')

提示如下错误:

--- Logging error --- Traceback (most recent call last): File "/usr/local/python38/lib/python3.8/logging/init.py", line 1081, in emit msg = self.format(record) File "/usr/local/python38/lib/python3.8/logging/init.py", line 925, in format return fmt.format(record) File "/usr/local/python38/lib/python3.8/logging/init.py", line 664, in format record.message = record.getMessage() File "/usr/local/python38/lib/python3.8/logging/init.py", line 369, in getMessage msg = msg % self.args TypeError: not all arguments converted during string formatting Call stack: File "ding-dong-bot.py", line 78, in asyncio.run(main()) File "/usr/local/python38/lib/python3.8/asyncio/runners.py", line 43, in run return loop.run_until_complete(main) File "/usr/local/python38/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete self.run_forever() File "/usr/local/python38/lib/python3.8/asyncio/base_events.py", line 570, in run_forever self._run_once() File "/usr/local/python38/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once handle._run() File "/usr/local/python38/lib/python3.8/asyncio/events.py", line 81, in _run self._context.run(self._callback, *self._args) File "/usr/local/python38/lib/python3.8/site-packages/wechaty/wechaty.py", line 471, in message_listener await self._plugin_manager.emit_events('message', msg) File "/usr/local/python38/lib/python3.8/site-packages/wechaty/plugin.py", line 329, in emit_events await plugin.on_message(msg) File "/www/wwwroot/wx_robot/wechaty_bot/myWxPrograms.py", line 483, in on_message be_add_friend = await Friendship.search(phone='18954072510') #自己测试号 File "/usr/local/python38/lib/python3.8/site-packages/wechaty/user/friendship.py", line 93, in search log.info('search() <%s, %s>', cls, weixin, phone) Message: 'search() <%s, %s>' Arguments: (<class 'wechaty.user.friendship.Friendship'>, None, '18954072510') 2021-02-27 10:04:37,540 - Contact - INFO - load contact <Contact <$search$-18954072510> <毒边存一>> Contact <$search$-18954072510> <毒边存一> $search$-18954072510 2021-02-27 10:04:37,541 - FriendShip - INFO - add() <$search$-18954072510, 大哥你好,卡友求助> 2021-02-27 10:04:37,544 - Wechaty - ERROR - internal error <(<Status.INTERNAL: 13>, 'ticket is required', None)>

wj-Mcat commented 3 years ago

Great Code

async def on_ready(_):
    global bot
    friend: Contact = await bot.Friendship.search(phone='18954072510')
    if friend:
        await bot.Friendship.add(friend, hello='hello, I"m Bob.')
from wechaty import Wechaty, Contact
import asyncio

class MyBot(Wechaty):
    async def on_ready(self, _):
        friend: Contact = await self.Friendship.search(phone='18954072510')
        if friend:
            await self.Friendship.add(friend, hello='hello, I"m Bob.')

async def start():
    await MyBot().start()

asyncio.run(start())

BTW

you should use bot instance to fetch {Contact, Room, Friendship, Message, UrlLink, MiniProgram, Tag}.

eg:

# event style
bot.Room.find_all()
# oop style
self.Room.find_all()

not

Room.find_all()
wj-Mcat commented 3 years ago

If you don't have any problems, I will close this issue tonight. Hope the answer can resolve your problem.

huangmu168 commented 3 years ago

么有问题了,可以关闭,多谢