From 5a0b774c22e914339e6019332e6517797360e979 Mon Sep 17 00:00:00 2001 From: Changhua Date: Thu, 21 Sep 2023 20:53:01 +0800 Subject: [PATCH] Impl get chatroom members --- clients/python/roomdata.proto | 21 ++++++++++++++ clients/python/wcferry/client.py | 38 ++++++++++++++++++++++++-- clients/python/wcferry/roomdata_pb2.py | 27 ++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 clients/python/roomdata.proto create mode 100644 clients/python/wcferry/roomdata_pb2.py diff --git a/clients/python/roomdata.proto b/clients/python/roomdata.proto new file mode 100644 index 0000000..22f307e --- /dev/null +++ b/clients/python/roomdata.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; +package com.iamteer.wcf; + +message RoomData { + + message RoomMember { + string wxid = 1; + string name = 2; + int32 state = 3; + } + + repeated RoomMember members = 1; + + int32 field_2 = 2; + int32 field_3 = 3; + int32 field_4 = 4; + int32 room_capacity = 5; + int32 field_6 = 6; + int64 field_7 = 7; + int64 field_8 = 8; +} diff --git a/clients/python/wcferry/client.py b/clients/python/wcferry/client.py index ee98b06..63daf92 100644 --- a/clients/python/wcferry/client.py +++ b/clients/python/wcferry/client.py @@ -19,6 +19,7 @@ import pynng import requests from google.protobuf import json_format from wcferry import wcf_pb2 +from wcferry.roomdata_pb2 import RoomData from wcferry.wxmsg import WxMsg @@ -536,9 +537,9 @@ class Wcf(): } friends = [] for cnt in self.get_contacts(): - if (cnt["wxid"].endswith("@chatroom") # 群聊 - or cnt["wxid"].startswith("gh_") # 公众号 - or cnt["wxid"] in not_friends.keys() # 其他杂号 + if (cnt["wxid"].endswith("@chatroom") or # 群聊 + cnt["wxid"].startswith("gh_") or # 公众号 + cnt["wxid"] in not_friends.keys() # 其他杂号 ): continue friends.append(cnt) @@ -629,3 +630,34 @@ class Wcf(): req.m.wxids = wxids.replace(" ", "") rsp = self._send_request(req) return rsp.status + + def get_chatroom_members(self, roomid: str) -> List[dict]: + """获取群成员 + + Args: + roomid (str): 群的 id + + Returns: + List[dict]: 群成员列表: [{wxid: 昵称}, ...] + """ + pass + contacts = self.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;") + contacts = {contact["UserName"]: contact["NickName"]for contact in contacts} + crs = self.query_sql("MicroMsg.db", f"SELECT RoomData FROM ChatRoom WHERE ChatRoomName = '{roomid}'") + if not crs: + return [] + + bs = crs[0].get("RoomData") + if not bs: + return [] + + crd = RoomData() + crd.ParseFromString(bs) + if not bs: + return [] + + names = {} + for member in crd.members: + names[member.wxid] = member.name if member.name else contacts.get(member.wxid, "") + + return names diff --git a/clients/python/wcferry/roomdata_pb2.py b/clients/python/wcferry/roomdata_pb2.py new file mode 100644 index 0000000..23ab48a --- /dev/null +++ b/clients/python/wcferry/roomdata_pb2.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: roomdata.proto +"""Generated protocol buffer code.""" +from google.protobuf.internal import builder as _builder +from google.protobuf import descriptor as _descriptor +from google.protobuf import descriptor_pool as _descriptor_pool +from google.protobuf import symbol_database as _symbol_database +# @@protoc_insertion_point(imports) + +_sym_db = _symbol_database.Default() + + + + +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0eroomdata.proto\x12\x0f\x63om.iamteer.wcf\"\xf7\x01\n\x08RoomData\x12\x35\n\x07members\x18\x01 \x03(\x0b\x32$.com.iamteer.wcf.RoomData.RoomMember\x12\x0f\n\x07\x66ield_2\x18\x02 \x01(\x05\x12\x0f\n\x07\x66ield_3\x18\x03 \x01(\x05\x12\x0f\n\x07\x66ield_4\x18\x04 \x01(\x05\x12\x15\n\rroom_capacity\x18\x05 \x01(\x05\x12\x0f\n\x07\x66ield_6\x18\x06 \x01(\x05\x12\x0f\n\x07\x66ield_7\x18\x07 \x01(\x03\x12\x0f\n\x07\x66ield_8\x18\x08 \x01(\x03\x1a\x37\n\nRoomMember\x12\x0c\n\x04wxid\x18\x01 \x01(\t\x12\x0c\n\x04name\x18\x02 \x01(\t\x12\r\n\x05state\x18\x03 \x01(\x05\x62\x06proto3') + +_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) +_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'roomdata_pb2', globals()) +if _descriptor._USE_C_DESCRIPTORS == False: + + DESCRIPTOR._options = None + _ROOMDATA._serialized_start=36 + _ROOMDATA._serialized_end=283 + _ROOMDATA_ROOMMEMBER._serialized_start=228 + _ROOMDATA_ROOMMEMBER._serialized_end=283 +# @@protoc_insertion_point(module_scope)