全局geweclient
This commit is contained in:
41
main.py
41
main.py
@@ -9,6 +9,7 @@ import urllib.parse
|
||||
from configuration import Config
|
||||
from constants import ChatType
|
||||
from gewechat.api.start_server import start_fastapi_server
|
||||
from gewechat.client import gewe_client
|
||||
from robot import Robot
|
||||
|
||||
# 配置日志
|
||||
@@ -17,50 +18,20 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
def main(chat_type: int):
|
||||
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" # 要发送消息的好友昵称
|
||||
|
||||
parsed_url = urllib.parse.urlparse(callback_url)
|
||||
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
|
||||
|
||||
start_fastapi_server(host="0.0.0.0", port=8999)
|
||||
# 创建机器人实例
|
||||
robot = Robot(config, app_id, client, chat_type)
|
||||
robot = Robot(config)
|
||||
robot.LOG.info(f"WeChatRobot gewechat 成功启动···")
|
||||
|
||||
# # 注册Robot实例到callback模块
|
||||
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 发送新闻
|
||||
# robot.onEveryTime("08:30", robot.news_baidu_report_auto)
|
||||
|
||||
Reference in New Issue
Block a user