重大版本调整:gewechat兼容。
This commit is contained in:
+37
-28
@@ -1,10 +1,13 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import logging
|
||||
import os.path
|
||||
import random
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
from wcferry import Wcf
|
||||
from gewechat_client import GewechatClient
|
||||
|
||||
from utils.wechat.contact_manager import ContactManager
|
||||
|
||||
|
||||
class MessageUtil:
|
||||
@@ -13,15 +16,16 @@ class MessageUtil:
|
||||
"""
|
||||
|
||||
# 修改 MessageUtil 类的初始化方法,接受联系人管理器而不是联系人字典
|
||||
def __init__(self, wcf, contact_manager):
|
||||
self.wcf = wcf
|
||||
def __init__(self, app_id: str, base_url: str, client: GewechatClient, contact_manager: ContactManager):
|
||||
self.app_id = app_id
|
||||
self.client = client
|
||||
self.contact_manager = contact_manager
|
||||
self.LOG = logging.getLogger("MessageUtil")
|
||||
|
||||
def send_text(self, msg: str, receiver: str, at_list: str = "") -> None:
|
||||
"""
|
||||
发送文本消息
|
||||
|
||||
|
||||
:param msg: 消息字符串
|
||||
:param receiver: 接收人wxid或者群id
|
||||
:param at_list: 要@的wxid, @所有人的wxid为:notify@all
|
||||
@@ -35,22 +39,21 @@ class MessageUtil:
|
||||
ats = " @所有人"
|
||||
else:
|
||||
wxids = at_list.split(",")
|
||||
for wxid in wxids:
|
||||
# 根据 wxid 查找群昵称
|
||||
ats += f" @{self.wcf.get_alias_in_chatroom(wxid, receiver)}"
|
||||
if len(wxids) > 0:
|
||||
ats += self.get_user_chatroom_nickname(receiver, wxids)
|
||||
|
||||
# {msg}{ats} 表示要发送的消息内容后面紧跟@,例如 北京天气情况为:xxx @张三
|
||||
if ats == "":
|
||||
self.LOG.info(f"To {receiver}: {msg}")
|
||||
self.wcf.send_text(f"{msg}", receiver, at_list)
|
||||
self.client.post_text(self.app_id, receiver, "{msg}", "")
|
||||
else:
|
||||
self.LOG.info(f"To {receiver}: {ats}\r{msg}")
|
||||
self.wcf.send_text(f"{ats}\n{msg}", receiver, at_list)
|
||||
self.client.post_text(self.app_id, receiver, f"{ats}\n{msg}", at_list)
|
||||
|
||||
def send_file(self, file_path: str, receiver: str) -> None:
|
||||
def send_file(self, file_path: str, receiver: str) -> str:
|
||||
"""
|
||||
发送文件消息
|
||||
|
||||
|
||||
:param file_path: 文件路径
|
||||
:param receiver: 接收人wxid或者群id
|
||||
"""
|
||||
@@ -58,9 +61,11 @@ class MessageUtil:
|
||||
time.sleep(random.uniform(0.5, 1.5))
|
||||
|
||||
self.LOG.info(f"Sending file to {receiver}: {file_path}")
|
||||
self.wcf.send_file(file_path, receiver)
|
||||
(path, filename) = os.path.split(file_path)
|
||||
self.LOG.info(f"Sending file to {path}: {filename}")
|
||||
return self.client.post_file(self.app_id, receiver, file_path, filename)
|
||||
|
||||
def send_image(self, image_path: str, receiver: str) -> None:
|
||||
def send_image(self, image_path: str, receiver: str) -> str:
|
||||
"""
|
||||
发送文件消息
|
||||
|
||||
@@ -71,13 +76,13 @@ class MessageUtil:
|
||||
time.sleep(random.uniform(0.5, 1.5))
|
||||
|
||||
self.LOG.info(f"Sending file to {receiver}: {image_path}")
|
||||
self.wcf.send_image(image_path, receiver)
|
||||
return self.client.post_image(self.app_id, receiver, image_path)
|
||||
|
||||
def send_rich_text(self, name: str, account: str, title: str, digest: str, url: str, thumburl: str,
|
||||
receiver: str) -> int:
|
||||
"""
|
||||
发送富文本消息
|
||||
|
||||
|
||||
卡片样式:
|
||||
|-------------------------------------|
|
||||
|title, 最长两行 |
|
||||
@@ -87,7 +92,7 @@ class MessageUtil:
|
||||
|digest, 最多三行,会占位 |--------|
|
||||
|(account logo) name |
|
||||
|-------------------------------------|
|
||||
|
||||
|
||||
:param name: 左下显示的名字
|
||||
:param account: 填公众号 id 可以显示对应的头像(gh_ 开头的)
|
||||
:param title: 标题,最多两行
|
||||
@@ -101,17 +106,21 @@ class MessageUtil:
|
||||
time.sleep(random.uniform(0.5, 1.5))
|
||||
|
||||
self.LOG.info(f"Sending rich text to {receiver}: {title}")
|
||||
return self.wcf.send_rich_text(name, account, title, digest, url, thumburl, receiver)
|
||||
return self.client.post_link(self.app_id, receiver, title, digest, url, thumburl)
|
||||
|
||||
def update_contacts(self, contacts: dict) -> None:
|
||||
"""
|
||||
更新联系人字典
|
||||
|
||||
:param contacts: 联系人字典,格式为 {"wxid": "NickName"}
|
||||
"""
|
||||
self.contacts.update(contacts)
|
||||
def get_user_chatroom_nickname(self, chatroom_id: str, member_wxids: list[str]) -> str:
|
||||
data = self.client.get_chatroom_member_detail(self.app_id, chatroom_id, member_wxids)
|
||||
nicknames_with_at = [" @" + member["nickName"] for member in data["data"] if member.get("nickName")]
|
||||
return " ".join(nicknames_with_at)
|
||||
|
||||
# 修改使用 allContacts 的地方,改为使用 contact_manager
|
||||
# 例如:
|
||||
# 原来的代码: nickname = self.allContacts.get(wxid, wxid)
|
||||
# 修改为: nickname = self.contact_manager.get_nickname(wxid)
|
||||
def invite_member(self, group_id, sender):
|
||||
return self.client.invite_member(self.app_id, sender, group_id, "自动加群邀请")
|
||||
|
||||
def get_chatroom_members(self, group_id) -> dict:
|
||||
data = self.client.get_chatroom_member_list(self.app_id, group_id)
|
||||
members = {member["wxid"]: member["nickName"] for member in data["data"]["memberList"]}
|
||||
return members
|
||||
|
||||
def download_file_from_url(self, url: str, target_dir: str) -> str:
|
||||
# 根据获取的文件地址,从server 下载 :http://{服务ip}:2532/download/{接口返回的文件路径}
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user