2022-10-16 16:50:22 +08:00
|
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import atexit
|
2023-02-25 21:58:00 +08:00
|
|
|
|
import base64
|
2022-10-16 16:50:22 +08:00
|
|
|
|
import logging
|
|
|
|
|
import os
|
2022-10-16 21:47:13 +08:00
|
|
|
|
import re
|
2022-10-16 16:50:22 +08:00
|
|
|
|
import sys
|
2023-02-27 23:36:17 +08:00
|
|
|
|
from queue import Queue
|
2022-10-16 16:50:22 +08:00
|
|
|
|
from threading import Thread
|
|
|
|
|
from time import sleep
|
2023-02-25 21:58:00 +08:00
|
|
|
|
from typing import Callable, List, Optional
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
2023-02-25 21:58:00 +08:00
|
|
|
|
import pynng
|
|
|
|
|
from google.protobuf import json_format
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
2022-10-19 20:41:23 +08:00
|
|
|
|
WCF_ROOT = os.path.abspath(os.path.dirname(__file__))
|
2022-10-16 16:50:22 +08:00
|
|
|
|
sys.path.insert(0, WCF_ROOT)
|
2023-02-25 21:58:00 +08:00
|
|
|
|
import wcf_pb2 # noqa
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
2023-03-09 20:03:53 +08:00
|
|
|
|
__version__ = "3.7.0.30.19.2"
|
2022-10-19 18:46:27 +08:00
|
|
|
|
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
2023-03-02 21:25:50 +08:00
|
|
|
|
def _retry():
|
|
|
|
|
def decorator(func):
|
|
|
|
|
""" Retry the function """
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
try:
|
|
|
|
|
ret = func(*args, **kwargs)
|
|
|
|
|
except Exception as _:
|
|
|
|
|
try:
|
|
|
|
|
ret = func(*args, **kwargs)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
func_name = re.findall(r"func: (.*?)\n", str(args[1]))[-1]
|
|
|
|
|
logging.getLogger("WCF").error(f"Call {func_name} failed: {e}")
|
2023-03-04 22:51:08 +08:00
|
|
|
|
ret = wcf_pb2.Response()
|
2023-03-02 21:25:50 +08:00
|
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
return wrapper
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
2022-10-16 16:50:22 +08:00
|
|
|
|
class Wcf():
|
2022-10-16 21:47:13 +08:00
|
|
|
|
"""WeChatFerry, a tool to play WeChat."""
|
|
|
|
|
class WxMsg():
|
|
|
|
|
"""微信消息"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, msg: wcf_pb2.WxMsg) -> None:
|
|
|
|
|
self._is_self = msg.is_self
|
|
|
|
|
self._is_group = msg.is_group
|
|
|
|
|
self.type = msg.type
|
|
|
|
|
self.id = msg.id
|
|
|
|
|
self.xml = msg.xml
|
|
|
|
|
self.sender = msg.sender
|
|
|
|
|
self.roomid = msg.roomid
|
|
|
|
|
self.content = msg.content
|
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2023-03-08 19:56:28 +08:00
|
|
|
|
s = f"{'自己发的:' if self._is_self else ''}"
|
|
|
|
|
s += f"{self.sender}[{self.roomid}]:{self.id}:{self.type}:{self.xml.replace(chr(10), '').replace(chr(9),'')}\n"
|
2022-10-16 21:47:13 +08:00
|
|
|
|
s += self.content
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
def from_self(self) -> bool:
|
|
|
|
|
"""是否自己发的消息"""
|
|
|
|
|
return self._is_self == 1
|
|
|
|
|
|
|
|
|
|
def from_group(self) -> bool:
|
|
|
|
|
"""是否群聊消息"""
|
|
|
|
|
return self._is_group
|
|
|
|
|
|
|
|
|
|
def is_at(self, wxid) -> bool:
|
|
|
|
|
"""是否被@:群消息,在@名单里,并且不是@所有人"""
|
|
|
|
|
return self.from_group() and re.findall(
|
|
|
|
|
f"<atuserlist>.*({wxid}).*</atuserlist>", self.xml) and not re.findall(r"@(?:所有人|all)", self.xml)
|
|
|
|
|
|
|
|
|
|
def is_text(self) -> bool:
|
|
|
|
|
"""是否文本消息"""
|
|
|
|
|
return self.type == 1
|
|
|
|
|
|
2023-02-25 21:58:00 +08:00
|
|
|
|
def __init__(self, host_port: str = None, debug: bool = False) -> None:
|
2023-01-17 22:56:58 +08:00
|
|
|
|
self._local_host = False
|
|
|
|
|
self._is_running = False
|
2023-02-25 21:58:00 +08:00
|
|
|
|
self._is_receiving_msg = False
|
2022-10-16 16:50:22 +08:00
|
|
|
|
self.LOG = logging.getLogger("WCF")
|
2023-03-05 01:53:20 +08:00
|
|
|
|
self.LOG.info(f"wcferry version: {__version__}")
|
2023-01-17 22:56:58 +08:00
|
|
|
|
if host_port is None:
|
|
|
|
|
self._local_host = True
|
2023-02-25 21:58:00 +08:00
|
|
|
|
host_port = "tcp://127.0.0.1:10086"
|
|
|
|
|
cmd = f"{WCF_ROOT}/wcf.exe start {'debug' if debug else ''}"
|
|
|
|
|
if os.system(cmd) != 0:
|
2023-01-17 22:56:58 +08:00
|
|
|
|
self.LOG.error("初始化失败!")
|
2023-02-28 20:14:22 +08:00
|
|
|
|
exit(-1)
|
2023-02-25 21:58:00 +08:00
|
|
|
|
|
|
|
|
|
# 连接 RPC
|
|
|
|
|
self.cmd_socket = pynng.Pair1() # Client --> Server,发送消息
|
|
|
|
|
self.cmd_socket.send_timeout = 2000 # 发送 2 秒超时
|
|
|
|
|
self.cmd_socket.recv_timeout = 2000 # 接收 2 秒超时
|
|
|
|
|
self.cmd_socket.dial(host_port, block=False)
|
|
|
|
|
|
|
|
|
|
self.msg_socket = pynng.Pair1() # Server --> Client,接收消息
|
|
|
|
|
self.msg_socket.send_timeout = 2000 # 发送 2 秒超时
|
|
|
|
|
self.msg_socket.recv_timeout = 2000 # 接收 2 秒超时
|
|
|
|
|
self.msg_url = host_port.replace("10086", "10087")
|
|
|
|
|
|
|
|
|
|
atexit.register(self.cleanup) # 退出的时候停止消息接收,防止内存泄露
|
|
|
|
|
while not self.is_login(): # 等待微信登录成功
|
|
|
|
|
sleep(1)
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
|
|
|
|
self._is_running = True
|
2022-10-16 21:47:13 +08:00
|
|
|
|
self.contacts = []
|
2023-02-27 23:36:17 +08:00
|
|
|
|
self.msgQ = Queue()
|
2022-10-16 21:47:13 +08:00
|
|
|
|
self._SQL_TYPES = {1: int, 2: float, 3: lambda x: x.decode("utf-8"), 4: bytes, 5: lambda x: None}
|
|
|
|
|
self.self_wxid = self.get_self_wxid()
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
|
|
|
|
def __del__(self) -> None:
|
|
|
|
|
self.cleanup()
|
|
|
|
|
|
|
|
|
|
def cleanup(self) -> None:
|
2023-02-25 21:58:00 +08:00
|
|
|
|
"""关闭连接,回收资源"""
|
2022-10-16 16:50:22 +08:00
|
|
|
|
if not self._is_running:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.disable_recv_msg()
|
2023-02-25 21:58:00 +08:00
|
|
|
|
self.cmd_socket.close()
|
|
|
|
|
|
2023-01-17 22:56:58 +08:00
|
|
|
|
if self._local_host:
|
2023-02-25 21:58:00 +08:00
|
|
|
|
cmd = f"{WCF_ROOT}/wcf.exe stop"
|
|
|
|
|
if os.system(cmd) != 0:
|
|
|
|
|
self.LOG.error("退出失败!")
|
|
|
|
|
return
|
2022-10-16 16:50:22 +08:00
|
|
|
|
self._is_running = False
|
|
|
|
|
|
|
|
|
|
def keep_running(self):
|
2022-10-16 21:47:13 +08:00
|
|
|
|
"""阻塞进程,让 RPC 一直维持连接"""
|
2022-10-16 16:50:22 +08:00
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
sleep(1)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.cleanup()
|
|
|
|
|
|
2023-03-02 21:25:50 +08:00
|
|
|
|
@_retry()
|
2023-02-25 21:58:00 +08:00
|
|
|
|
def _send_request(self, req: wcf_pb2.Request) -> wcf_pb2.Response:
|
|
|
|
|
data = req.SerializeToString()
|
|
|
|
|
self.cmd_socket.send(data)
|
|
|
|
|
rsp = wcf_pb2.Response()
|
|
|
|
|
rsp.ParseFromString(self.cmd_socket.recv_msg().bytes)
|
|
|
|
|
return rsp
|
|
|
|
|
|
2023-02-27 23:36:17 +08:00
|
|
|
|
def is_receiving_msg(self) -> bool:
|
|
|
|
|
return self._is_receiving_msg
|
|
|
|
|
|
2023-02-25 21:58:00 +08:00
|
|
|
|
def is_login(self) -> bool:
|
2022-10-16 21:47:13 +08:00
|
|
|
|
"""是否已经登录"""
|
2023-02-25 21:58:00 +08:00
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_IS_LOGIN # FUNC_IS_LOGIN
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
|
|
|
|
|
return rsp.status == 1
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
|
|
|
|
def get_self_wxid(self) -> str:
|
2022-10-16 21:47:13 +08:00
|
|
|
|
"""获取登录账户的 wxid"""
|
2023-02-25 21:58:00 +08:00
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_GET_SELF_WXID # FUNC_GET_SELF_WXID
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
|
2022-10-16 16:50:22 +08:00
|
|
|
|
return rsp.str
|
|
|
|
|
|
2023-02-25 21:58:00 +08:00
|
|
|
|
def get_msg_types(self) -> dict:
|
|
|
|
|
"""获取所有消息类型"""
|
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_GET_MSG_TYPES # FUNC_GET_MSG_TYPES
|
|
|
|
|
rsp = self._send_request(req)
|
2023-03-01 19:38:18 +08:00
|
|
|
|
types = json_format.MessageToDict(rsp.types).get("types", {})
|
2023-02-25 21:58:00 +08:00
|
|
|
|
types = {int(k): v for k, v in types.items()}
|
|
|
|
|
|
|
|
|
|
return dict(sorted(dict(types).items()))
|
|
|
|
|
|
|
|
|
|
def get_contacts(self) -> List[dict]:
|
|
|
|
|
"""获取完整通讯录"""
|
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_GET_CONTACTS # FUNC_GET_CONTACTS
|
|
|
|
|
rsp = self._send_request(req)
|
2023-03-01 19:38:18 +08:00
|
|
|
|
contacts = json_format.MessageToDict(rsp.contacts).get("contacts", [])
|
2023-02-25 21:58:00 +08:00
|
|
|
|
|
|
|
|
|
for cnt in contacts:
|
|
|
|
|
gender = cnt.get("gender", "")
|
|
|
|
|
if gender == 1:
|
|
|
|
|
gender = "男"
|
|
|
|
|
elif gender == 2:
|
|
|
|
|
gender = "女"
|
|
|
|
|
contact = {
|
|
|
|
|
"wxid": cnt.get("wxid", ""),
|
|
|
|
|
"code": cnt.get("code", ""),
|
|
|
|
|
"name": cnt.get("name", ""),
|
|
|
|
|
"country": cnt.get("country", ""),
|
|
|
|
|
"province": cnt.get("province", ""),
|
|
|
|
|
"city": cnt.get("city", ""),
|
|
|
|
|
"gender": gender}
|
|
|
|
|
self.contacts.append(contact)
|
|
|
|
|
return self.contacts
|
|
|
|
|
|
|
|
|
|
def get_dbs(self) -> List[str]:
|
|
|
|
|
"""获取所有数据库"""
|
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_GET_DB_NAMES # FUNC_GET_DB_NAMES
|
|
|
|
|
rsp = self._send_request(req)
|
2023-03-01 19:38:18 +08:00
|
|
|
|
dbs = json_format.MessageToDict(rsp.dbs).get("names", [])
|
2023-02-25 21:58:00 +08:00
|
|
|
|
|
|
|
|
|
return dbs
|
|
|
|
|
|
|
|
|
|
def get_tables(self, db: str) -> List[dict]:
|
|
|
|
|
"""获取 db 中所有表"""
|
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_GET_DB_TABLES # FUNC_GET_DB_TABLES
|
|
|
|
|
req.str = db
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
tables = json_format.MessageToDict(rsp.tables).get("tables", [])
|
|
|
|
|
|
|
|
|
|
return tables
|
|
|
|
|
|
|
|
|
|
def send_text(self, msg: str, receiver: str, aters: Optional[str] = "") -> int:
|
|
|
|
|
"""发送文本消息"""
|
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_SEND_TXT # FUNC_SEND_TXT
|
|
|
|
|
req.txt.msg = msg
|
|
|
|
|
req.txt.receiver = receiver
|
|
|
|
|
if aters:
|
|
|
|
|
req.txt.aters = aters
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
return rsp.status
|
|
|
|
|
|
|
|
|
|
def send_image(self, path: str, receiver: str) -> int:
|
|
|
|
|
"""发送图片"""
|
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_SEND_IMG # FUNC_SEND_IMG
|
|
|
|
|
req.file.path = path
|
|
|
|
|
req.file.receiver = receiver
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
return rsp.status
|
|
|
|
|
|
|
|
|
|
def send_file(self, path: str, receiver: str) -> int:
|
|
|
|
|
"""发送文件"""
|
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_SEND_FILE # FUNC_SEND_FILE
|
|
|
|
|
req.file.path = path
|
|
|
|
|
req.file.receiver = receiver
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
return rsp.status
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
2023-03-01 04:03:53 +08:00
|
|
|
|
def send_xml(self, receiver: str, xml: str, type: int, path: str = None) -> int:
|
|
|
|
|
"""发送文件"""
|
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_SEND_XML # FUNC_SEND_XML
|
|
|
|
|
req.xml.receiver = receiver
|
|
|
|
|
req.xml.content = xml
|
|
|
|
|
req.xml.type = type
|
|
|
|
|
if path:
|
|
|
|
|
req.xml.path = path
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
return rsp.status
|
|
|
|
|
|
2023-02-27 23:36:17 +08:00
|
|
|
|
def get_msg(self, block=True) -> WxMsg:
|
|
|
|
|
return self.msgQ.get(block, timeout=1)
|
|
|
|
|
|
|
|
|
|
def enable_receiving_msg(self) -> bool:
|
|
|
|
|
"""允许接收消息"""
|
|
|
|
|
def listening_msg():
|
|
|
|
|
rsp = wcf_pb2.Response()
|
|
|
|
|
self.msg_socket.dial(self.msg_url, block=True)
|
|
|
|
|
while self._is_receiving_msg:
|
|
|
|
|
try:
|
|
|
|
|
rsp.ParseFromString(self.msg_socket.recv_msg().bytes)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
self.msgQ.put(self.WxMsg(rsp.wxmsg))
|
|
|
|
|
|
|
|
|
|
# 退出前关闭通信通道
|
|
|
|
|
self.msg_socket.close()
|
|
|
|
|
|
|
|
|
|
if self._is_receiving_msg:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_ENABLE_RECV_TXT # FUNC_ENABLE_RECV_TXT
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
if rsp.status != 0:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
self._is_receiving_msg = True
|
|
|
|
|
# 阻塞,把控制权交给用户
|
|
|
|
|
# self._rpc_get_message(callback)
|
|
|
|
|
|
|
|
|
|
# 不阻塞,启动一个新的线程来接收消息
|
|
|
|
|
Thread(target=listening_msg, name="GetMessage", daemon=True).start()
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
2022-10-16 21:47:13 +08:00
|
|
|
|
def enable_recv_msg(self, callback: Callable[[WxMsg], None] = None) -> bool:
|
|
|
|
|
"""设置接收消息回调"""
|
2023-02-25 21:58:00 +08:00
|
|
|
|
def listening_msg():
|
|
|
|
|
rsp = wcf_pb2.Response()
|
|
|
|
|
self.msg_socket.dial(self.msg_url, block=True)
|
|
|
|
|
while self._is_receiving_msg:
|
|
|
|
|
try:
|
|
|
|
|
rsp.ParseFromString(self.msg_socket.recv_msg().bytes)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
callback(self.WxMsg(rsp.wxmsg))
|
|
|
|
|
# 退出前关闭通信通道
|
|
|
|
|
self.msg_socket.close()
|
|
|
|
|
|
|
|
|
|
if self._is_receiving_msg:
|
2022-10-16 16:50:22 +08:00
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
if callback is None:
|
|
|
|
|
return False
|
|
|
|
|
|
2023-02-25 21:58:00 +08:00
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_ENABLE_RECV_TXT # FUNC_ENABLE_RECV_TXT
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
if rsp.status != 0:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
self._is_receiving_msg = True
|
2022-10-16 16:50:22 +08:00
|
|
|
|
# 阻塞,把控制权交给用户
|
2023-02-27 23:36:17 +08:00
|
|
|
|
# listening_msg()
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
|
|
|
|
# 不阻塞,启动一个新的线程来接收消息
|
2023-02-25 21:58:00 +08:00
|
|
|
|
Thread(target=listening_msg, name="GetMessage", daemon=True).start()
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def disable_recv_msg(self) -> int:
|
2022-10-16 21:47:13 +08:00
|
|
|
|
"""停止接收消息"""
|
2023-02-25 21:58:00 +08:00
|
|
|
|
if not self._is_receiving_msg:
|
|
|
|
|
return 0
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
2023-02-25 21:58:00 +08:00
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_DISABLE_RECV_TXT # FUNC_DISABLE_RECV_TXT
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
self._is_receiving_msg = False
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
|
|
|
|
return rsp.status
|
|
|
|
|
|
2022-10-16 21:47:13 +08:00
|
|
|
|
def query_sql(self, db: str, sql: str) -> List[dict]:
|
|
|
|
|
"""执行 SQL"""
|
|
|
|
|
result = []
|
2023-02-25 21:58:00 +08:00
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_EXEC_DB_QUERY # FUNC_EXEC_DB_QUERY
|
|
|
|
|
req.query.db = db
|
|
|
|
|
req.query.sql = sql
|
|
|
|
|
rsp = self._send_request(req)
|
2023-03-01 19:38:18 +08:00
|
|
|
|
rows = json_format.MessageToDict(rsp.rows).get("rows", [])
|
2023-02-25 21:58:00 +08:00
|
|
|
|
for r in rows:
|
2022-10-16 21:47:13 +08:00
|
|
|
|
row = {}
|
2023-02-25 21:58:00 +08:00
|
|
|
|
for f in r["fields"]:
|
|
|
|
|
c = base64.b64decode(f.get("content", ""))
|
|
|
|
|
row[f["column"]] = self._SQL_TYPES[f["type"]](c)
|
2022-10-16 21:47:13 +08:00
|
|
|
|
result.append(row)
|
|
|
|
|
return result
|
2022-10-16 16:50:22 +08:00
|
|
|
|
|
|
|
|
|
def accept_new_friend(self, v3: str, v4: str) -> int:
|
2023-02-28 20:15:20 +08:00
|
|
|
|
"""添加好友"""
|
2023-02-25 21:58:00 +08:00
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_ACCEPT_FRIEND # FUNC_ACCEPT_FRIEND
|
|
|
|
|
req.v.v3 = v3
|
|
|
|
|
req.v.v4 = v4
|
|
|
|
|
rsp = self._send_request(req)
|
2022-10-16 16:50:22 +08:00
|
|
|
|
return rsp.status
|
2023-01-17 23:01:53 +08:00
|
|
|
|
|
|
|
|
|
def get_friends(self) -> List[dict]:
|
|
|
|
|
"""获取好友列表"""
|
|
|
|
|
not_friends = {
|
|
|
|
|
"fmessage": "朋友推荐消息",
|
|
|
|
|
"medianote": "语音记事本",
|
|
|
|
|
"floatbottle": "漂流瓶",
|
|
|
|
|
"filehelper": "文件传输助手",
|
|
|
|
|
"newsapp": "新闻",
|
|
|
|
|
}
|
|
|
|
|
friends = []
|
2023-02-25 21:58:00 +08:00
|
|
|
|
for cnt in self.get_contacts():
|
2023-03-09 20:03:17 +08:00
|
|
|
|
if (cnt["wxid"].endswith("@chatroom") # 群聊
|
|
|
|
|
or cnt["wxid"].startswith("gh_") # 公众号
|
|
|
|
|
or cnt["wxid"] in not_friends.keys() # 其他杂号
|
2023-01-17 23:01:53 +08:00
|
|
|
|
):
|
|
|
|
|
continue
|
2023-02-25 21:58:00 +08:00
|
|
|
|
friends.append(cnt)
|
|
|
|
|
|
2023-01-17 23:01:53 +08:00
|
|
|
|
return friends
|
2023-02-28 20:14:22 +08:00
|
|
|
|
|
|
|
|
|
def add_chatroom_members(self, roomid: str, wxids: str) -> int:
|
|
|
|
|
"""添加群成员"""
|
|
|
|
|
req = wcf_pb2.Request()
|
|
|
|
|
req.func = wcf_pb2.FUNC_ADD_ROOM_MEMBERS # FUNC_ADD_ROOM_MEMBERS
|
|
|
|
|
req.m.roomid = roomid
|
|
|
|
|
req.m.wxids = wxids
|
|
|
|
|
rsp = self._send_request(req)
|
|
|
|
|
return rsp.status
|