全局geweclient
This commit is contained in:
41
main.py
41
main.py
@@ -9,6 +9,7 @@ import urllib.parse
|
|||||||
from configuration import Config
|
from configuration import Config
|
||||||
from constants import ChatType
|
from constants import ChatType
|
||||||
from gewechat.api.start_server import start_fastapi_server
|
from gewechat.api.start_server import start_fastapi_server
|
||||||
|
from gewechat.client import gewe_client
|
||||||
from robot import Robot
|
from robot import Robot
|
||||||
|
|
||||||
# 配置日志
|
# 配置日志
|
||||||
@@ -17,50 +18,20 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
def main(chat_type: int):
|
def main(chat_type: int):
|
||||||
config = Config()
|
config = Config()
|
||||||
base_url = config.BASE_URL
|
|
||||||
token = config.GEWECHAT_TOKEN
|
|
||||||
app_id = config.APP_ID
|
|
||||||
callback_url = config.CALLBACK_URL
|
|
||||||
send_msg_wxid = "filehelper" # 要发送消息的好友昵称
|
send_msg_wxid = "filehelper" # 要发送消息的好友昵称
|
||||||
|
|
||||||
parsed_url = urllib.parse.urlparse(callback_url)
|
start_fastapi_server(host="0.0.0.0", port=8999)
|
||||||
host = parsed_url.hostname or "0.0.0.0"
|
|
||||||
port = parsed_url.port or 8999
|
|
||||||
|
|
||||||
start_fastapi_server(host, port)
|
|
||||||
|
|
||||||
# 创建 GewechatClient 实例
|
|
||||||
client = GewechatClient(base_url, token)
|
|
||||||
|
|
||||||
# 登录, 自动创建二维码,扫码后自动登录
|
|
||||||
app_id, error_msg = client.login(app_id=app_id)
|
|
||||||
|
|
||||||
if error_msg:
|
|
||||||
logger.error("登录失败")
|
|
||||||
return
|
|
||||||
# 休眠等待server启动,防止回调设置失败
|
|
||||||
time.sleep(5)
|
|
||||||
resp = client.set_callback(token, callback_url)
|
|
||||||
print(f"set_callback:{resp}")
|
|
||||||
|
|
||||||
# 如果启动时,配置文件中的app_id为空,那么将app_id写入配置文件
|
|
||||||
if not config.APP_ID:
|
|
||||||
# 更新配置文件中的APP_ID
|
|
||||||
config.update_config('gewechat', 'app_id', app_id)
|
|
||||||
logger.info(f"已将新的APP_ID: {app_id} 写入配置文件")
|
|
||||||
# 同时更新当前配置对象中的APP_ID
|
|
||||||
config.APP_ID = app_id
|
|
||||||
|
|
||||||
# 创建机器人实例
|
# 创建机器人实例
|
||||||
robot = Robot(config, app_id, client, chat_type)
|
robot = Robot(config)
|
||||||
robot.LOG.info(f"WeChatRobot gewechat 成功启动···")
|
robot.LOG.info(f"WeChatRobot gewechat 成功启动···")
|
||||||
|
|
||||||
# # 注册Robot实例到callback模块
|
# # 注册Robot实例到callback模块
|
||||||
from gewechat.api.callback import register_robot
|
from gewechat.api.callback import register_robot
|
||||||
register_robot(app_id, robot)
|
register_robot(gewe_client.app_id, robot)
|
||||||
|
|
||||||
# 机器人启动发送测试消息
|
# 机器人启动发送测试消息
|
||||||
client.post_text(app_id, send_msg_wxid, "client 启动成功!")
|
gewe_client.post_text(gewe_client.app_id, send_msg_wxid, "client 启动成功!")
|
||||||
#
|
#
|
||||||
# # 每天 8:30 发送新闻
|
# # 每天 8:30 发送新闻
|
||||||
# robot.onEveryTime("08:30", robot.news_baidu_report_auto)
|
# robot.onEveryTime("08:30", robot.news_baidu_report_auto)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from typing import Optional
|
|||||||
|
|
||||||
from gewechat_client import GewechatClient
|
from gewechat_client import GewechatClient
|
||||||
|
|
||||||
|
from gewechat.client import gewe_client
|
||||||
from utils.wechat.contact_manager import ContactManager
|
from utils.wechat.contact_manager import ContactManager
|
||||||
|
|
||||||
|
|
||||||
@@ -16,9 +17,9 @@ class MessageUtil:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# 修改 MessageUtil 类的初始化方法,接受联系人管理器而不是联系人字典
|
# 修改 MessageUtil 类的初始化方法,接受联系人管理器而不是联系人字典
|
||||||
def __init__(self, app_id: str, client: GewechatClient):
|
def __init__(self):
|
||||||
self.app_id = app_id
|
self.app_id = gewe_client.app_id
|
||||||
self.client = client
|
self.client = gewe_client
|
||||||
self.contact_manager = ContactManager.get_instance()
|
self.contact_manager = ContactManager.get_instance()
|
||||||
self.LOG = logging.getLogger("MessageUtil")
|
self.LOG = logging.getLogger("MessageUtil")
|
||||||
|
|
||||||
|
|||||||
20
robot.py
20
robot.py
@@ -10,6 +10,7 @@ from base.func_epic import is_friday, get_free
|
|||||||
from base.func_news import News
|
from base.func_news import News
|
||||||
from configuration import Config
|
from configuration import Config
|
||||||
from gewechat.call_back_message.message import WxMessage, MessageType
|
from gewechat.call_back_message.message import WxMessage, MessageType
|
||||||
|
from gewechat.client import gewe_client
|
||||||
from utils.json_converter import json_to_object
|
from utils.json_converter import json_to_object
|
||||||
from utils.wechat.message_to_db import MessageStorage
|
from utils.wechat.message_to_db import MessageStorage
|
||||||
from plugin_common.event_system import EventType, EventSystem
|
from plugin_common.event_system import EventType, EventSystem
|
||||||
@@ -37,9 +38,8 @@ class Robot(Job):
|
|||||||
"""个性化自己的机器人
|
"""个性化自己的机器人
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, config: Config, app_id: str, client: GewechatClient, chat_type: int) -> None:
|
def __init__(self, config: Config) -> None:
|
||||||
self.app_id = app_id
|
self.client = gewe_client
|
||||||
self.client = client
|
|
||||||
self.config = config
|
self.config = config
|
||||||
self.LOG = logging.getLogger("Robot")
|
self.LOG = logging.getLogger("Robot")
|
||||||
|
|
||||||
@@ -62,14 +62,14 @@ class Robot(Job):
|
|||||||
self.contact_manager.set_contacts(self.allContacts)
|
self.contact_manager.set_contacts(self.allContacts)
|
||||||
|
|
||||||
# 获取个人信息
|
# 获取个人信息
|
||||||
obj = json_to_object(self.client.get_profile(self.app_id))
|
obj = json_to_object(self.client.get_profile(self.client.app_id))
|
||||||
if obj.data.wxid is None:
|
if obj.data.wxid is None:
|
||||||
self.LOG.info(f"获取个人信息失败,退出程序!")
|
self.LOG.info(f"获取个人信息失败,退出程序!")
|
||||||
return
|
return
|
||||||
self.wxid = obj.data.wxid
|
self.wxid = obj.data.wxid
|
||||||
|
|
||||||
# 初始化消息工具类 - 使用联系人管理器
|
# 初始化消息工具类 - 使用联系人管理器
|
||||||
self.message_util = MessageUtil(app_id, client)
|
self.message_util = MessageUtil()
|
||||||
self.groups = {} # 存储按group_id分组的消息列表,每个group_id最多保留10条消息
|
self.groups = {} # 存储按group_id分组的消息列表,每个group_id最多保留10条消息
|
||||||
GroupBotManager.load_local_cache()
|
GroupBotManager.load_local_cache()
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ class Robot(Job):
|
|||||||
# 设置插件系统上下文
|
# 设置插件系统上下文
|
||||||
self.system_context = {
|
self.system_context = {
|
||||||
"config": config,
|
"config": config,
|
||||||
"client": client,
|
"client": gewe_client,
|
||||||
"event_system": self.event_system,
|
"event_system": self.event_system,
|
||||||
"plugin_registry": self.plugin_registry,
|
"plugin_registry": self.plugin_registry,
|
||||||
"db_pool": self.db_pool,
|
"db_pool": self.db_pool,
|
||||||
@@ -151,8 +151,6 @@ class Robot(Job):
|
|||||||
# 设置ROBOT功能为启用状态
|
# 设置ROBOT功能为启用状态
|
||||||
GroupBotManager.set_group_permission(msg.roomid, Feature.ROBOT, PermissionStatus.ENABLED)
|
GroupBotManager.set_group_permission(msg.roomid, Feature.ROBOT, PermissionStatus.ENABLED)
|
||||||
# 更新联系人信息
|
# 更新联系人信息
|
||||||
# 如果是加入新群,则拉取群用户信息,并且将群加入联系人
|
|
||||||
self.client.get_chatroom_info(self.app_id, msg.roomid)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.LOG.error(f"加入新群,自动添加并开启机器人功能 error: {e}")
|
self.LOG.error(f"加入新群,自动添加并开启机器人功能 error: {e}")
|
||||||
|
|
||||||
@@ -468,7 +466,7 @@ class Robot(Job):
|
|||||||
batch_wxids = wxids[i:i + batch_size]
|
batch_wxids = wxids[i:i + batch_size]
|
||||||
|
|
||||||
# 批量获取联系人详细信息
|
# 批量获取联系人详细信息
|
||||||
contact_info = self.client.get_detail_info(self.app_id, batch_wxids)
|
contact_info = self.client.get_detail_info(self.client.app_id, batch_wxids)
|
||||||
self.LOG.info(f"获取联系人详细信息响应: {contact_info}")
|
self.LOG.info(f"获取联系人详细信息响应: {contact_info}")
|
||||||
# 处理返回的数据
|
# 处理返回的数据
|
||||||
if contact_info and contact_info.get("ret") == 200 and "data" in contact_info:
|
if contact_info and contact_info.get("ret") == 200 and "data" in contact_info:
|
||||||
@@ -507,7 +505,7 @@ class Robot(Job):
|
|||||||
"""更新群成员详细信息"""
|
"""更新群成员详细信息"""
|
||||||
try:
|
try:
|
||||||
# 首先获取群成员列表
|
# 首先获取群成员列表
|
||||||
members_response = self.client.get_chatroom_member_list(self.app_id, chatroom_id)
|
members_response = self.client.get_chatroom_member_list(self.client.app_id, chatroom_id)
|
||||||
if members_response and members_response.get('ret') == 200:
|
if members_response and members_response.get('ret') == 200:
|
||||||
member_list = members_response.get('data', {}).get('memberList', [])
|
member_list = members_response.get('data', {}).get('memberList', [])
|
||||||
|
|
||||||
@@ -517,7 +515,7 @@ class Robot(Job):
|
|||||||
if member_wxids:
|
if member_wxids:
|
||||||
# 按照官方接口格式传递参数
|
# 按照官方接口格式传递参数
|
||||||
details_response = self.client.get_chatroom_member_detail(
|
details_response = self.client.get_chatroom_member_detail(
|
||||||
self.app_id,
|
self.client.app_id,
|
||||||
chatroom_id,
|
chatroom_id,
|
||||||
member_wxids # 直接传递列表,不需要转换为集合
|
member_wxids # 直接传递列表,不需要转换为集合
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user