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

无法显示群用户详细信息

Open huangmu168 opened this issue 6 years ago • 9 comments

from_contact = msg.talker()
text = msg.text()
room = msg.room()

if room is not None:
    member_list = await room.member_list()
    print(member_list)
    for member in member_list:
        print(member)

在群内发言后,没反应

huangmu168 avatar Jun 19 '20 09:06 huangmu168

@huangmu168 this code is a fragment of your bot file. Please try to put more code to let me know more details.

wj-Mcat avatar Jun 19 '20 09:06 wj-Mcat

import os import asyncio import requests import json import time

from wechaty import ( Contact, FileBox, Message, Wechaty, )

from wechaty_puppet import ( # type: ignore FileBox, MessagePayload, MessageQueryFilter, MessageType, get_logger )

async def on_message(self, msg: Message): """ Message Handler for the Bot """

#print('file_box',msg.to_file_box())
#await msg.to_file_box()
#await msg.to_image()
#print('msg的类型是:',msg.message_type())
#print('msg的payload:',msg.payload())
#文本/默认的表情 的类型是7;图片的类型是6;语音的类型是2
#视频的类型是15;链接的类型14;名片类型是3;小程序 9
#对message类型的判别
message_type_dic={  0 :'Unkown', 1 :'附件/文件', 2 :'音频', 3 :'名片', 4 : '',6: '图片', 7:'文本/表情', 15:'视频', 
                    4:'历史记录',9:'小程序',11:'转账',  12:'红包', 14:'链接',8:'位置',
}

from_contact = msg.talker()
text = msg.text()
room = msg.room()

if room is not None:
    member_list = await room.member_list()
    print(member_list)
    for member in member_list:
        print(member)

if msg.type() == msg.Type.MESSAGE_TYPE_AUDIO:
    file = await self.puppet.message_file(msg.message_id)
    print(file)

'''#发送本地图片的方法--可用
with open('./avatar.jpg', 'rb') as f:
    content = base64.b64encode(f.read())
    file_box = FileBox.from_base64(name='avatar.jpg', base64=content)
    await conversation.say(file_box)
'''

'''#获取用户信息列表--存在问题
async def contact_list(self) -> ContactListResponse:
    request = ContactListRequest()

    return await self._unary_unary(
        "/wechaty.Puppet/ContactList", request, ContactListResponse,
    )
'''

if msg.message_type() == 7 :#文本类
    if True:
        msg_text = msg.text()
        payload = {'key_word': msg_text}
        ret = requests.get("http://127.0.0.1:7003/auto_reply", params=payload)
        result_dic = eval(ret.text)

        if result_dic['action']=='reply':
            reply_dic=result_dic['reply_info']
            #回复文本
            if reply_dic['re_msg_type']=='text':
                if msg.room() is not None:
                    re_talker = str(msg.talker())[9:-1]
                    await msg.say('@'+re_talker+'  '+ reply_dic['re_msg'])
                else:
                    time.sleep(0.5)
                    await msg.say(reply_dic['re_msg'])
            #回复图片
            if reply_dic['re_msg_type']=='images':
                img_name = reply_dic['re_msg']
                file_box = FileBox.from_url(
                        'http://interface.yundatahome.com:18895/imgs/'+img_name,
                        name=img_name
                        )
                if msg.room() is not None:
                    re_talker = str(msg.talker())[9:-1]
                    await msg.say('@'+re_talker)
                    time.sleep(0.5)
                    await msg.say(file_box)
                else:
                    time.sleep(0.5)
                    await msg.say(file_box)#这个msg对象是当前msg对象

#测试服务是否正常
if msg.text() == 'ding' or msg.text() == '我通过了你的朋友验证请求,现在我们可以开始聊天了':
    await msg.say('dong')
    img_name = '1592097714.png'
    file_box = FileBox.from_url(
        'http://interface.yundatahome.com:18895/imgs/'+img_name,
        name=img_name
    )
    await msg.say(file_box)#这个msg对象是当前msg对象

async def on_scan(qrcode: str, status: int): """ Scan Handler for the Bot """ print(qrcode, status) # TODO: To be written

async def on_login(user: Contact): """ Login Handler for the Bot """ print(user) # TODO: To be written

async def main(): """ Async Main Entry """ # # Make sure we have set WECHATY_PUPPET_HOSTIE_TOKEN in the environment variables. # #将token加入环境变量 os.environ.setdefault('WECHATY_PUPPET', "wechaty-puppet-hostie") os.environ.setdefault('WECHATY_PUPPET_HOSTIE_TOKEN', "i-dnot-wanto-tellu") #os.environ['WECHATY_PUPPET']='wechaty-puppet-hostie' #os.environ['WECHATY_PUPPET_HOSTIE_TOKEN']='i-dnot-wanto-tellu'

if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ:
    print('''
        Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables
        You need a TOKEN to run the Java Wechaty. Please goto our README for details
        https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token
    ''')

bot = Wechaty()

bot.on('scan',      on_scan)
bot.on('login',     on_login)
bot.on('message',   on_message)

await bot.start()

print('[Python Wechaty] Ding Dong Bot started.')

asyncio.run(main())

huangmu168 avatar Jun 19 '20 09:06 huangmu168

if msg.type() == msg.Type.MESSAGE_TYPE_AUDIO: file = await self.puppet.message_file(msg.message_id) print(file)

can not work also!!!!

huangmu168 avatar Jun 19 '20 09:06 huangmu168

@huangmu168 first, on_message function has not been excuted because that the params of the on_message is not correct.

You should change the params of the on_message:

async def on_message(msg: Message):

I recommend you that you should use OOP style to write a bot. refert to oop-bot

wj-Mcat avatar Jun 19 '20 09:06 wj-Mcat

I tried the oop-bot, it's great. so, the new question is --How to get more infos from the member in room? thx a lot

huangmu168 avatar Jun 19 '20 10:06 huangmu168

There are some concept that your should know:RoomMember is a type of the Contact.

members = await room.member_list()
for member in members:
    await member.ready()

You shuold call the ready function to load more detail info, the same as Room, Friendship

wj-Mcat avatar Jun 19 '20 10:06 wj-Mcat

another erro was happened, cannot say anything

#测试服务是否正常 if text == 'ding': #await msg.say('dong') conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() await conversation.say('dong')

        img_name = '1592097714.png'
        file_box = FileBox.from_url(
            'http://interface.yundatahome.com:18895/imgs/'+img_name,
            name=img_name
        )
        await conversation.say(file_box)#这个msg对象是当前msg对象

huangmu168 avatar Jun 19 '20 10:06 huangmu168

There are some concept that your should know:RoomMember is a type of the Contact.

members = await room.member_list()
for member in members:
    await member.ready()

You shuold call the ready function to load more detail info, the same as Room, Friendship

I test it, use ready or not, it's no diffrent ,that's wared

huangmu168 avatar Jun 19 '20 10:06 huangmu168

now, I understand. oop版本的,里面的信息,仅仅只能被处理一次,不能被多次处理,也就是说,在message里面写再多的功能都没有用,他只处理第一个功能。哈哈哈。

所以你得检查一下什么原因?还是这就是你这个代码的本意所在?

huangmu168 avatar Jun 19 '20 11:06 huangmu168