Skip to content

LAVARONG/wechat-automation-api

wechat-automation

Verified

微信发信能力:通过 Windows 桌面微信发送文本、图片URL或本地文件。适合各类 Agent 在本机直接发送一次性消息。触发词:给[某人]发微信、通知[某人]、把这个用微信发给[某人]、用微信发个图片、把本地文件发给[某人]

FileKindSize
SKILL.mdskill md9.9 KB
examples/README.mdmarkdown819 B
examples/monitor_wechat_messages.pyscript11.9 KB
examples/wx.pyscript2.1 KB
scripts/app.pyscript8.4 KB
scripts/message_queue.pyscript10.6 KB
scripts/monitor.pyscript4.4 KB
scripts/skill_cli.pyscript6.2 KB
scripts/test_wpush.pyscript2.5 KB
scripts/wechat_controller.pyscript57.6 KB

scripts/app.py

View on GitHub
"""
微信自动化 HTTP 服务
提供 RESTful API 接口,接收消息发送请求并加入队列处理
"""
from flask import Flask, request, jsonify
import json
import logging
import os
import sys
import subprocess
import atexit
from message_queue import MessageQueue

def get_project_root():
    """获取项目根目录绝对路径"""
    return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# 创建 Flask 应用
app = Flask(__name__)

# 全局变量
message_queue = None
config = None
monitor_process = None

# 配置日志
def setup_logging():
    """配置日志系统"""
    log_level = config.get('log_level', 'INFO')
    log_file_name = config.get('log_file', 'wechat_automation.log')
    log_file = os.path.join(get_project_root(), log_file_name)
    
    logging.basicConfig(
        level=getattr(logging, log_level),
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.FileHandler(log_file, encoding='utf-8'),
            logging.StreamHandler()
        ]
    )
    
    logger = logging.getLogger(__name__)
    logger.info("日志系统已初始化")
    return logger

# 加载配置
def load_config():
    """从 config.json 加载配置"""
    config_file = os.path.join(get_project_root(), 'config.json')
    if not os.path.exists(config_file):
        raise FileNotFoundError(f"配置文件 {config_file} 不存在")
    
    with open(config_file, 'r', encoding='utf-8') as f:
        return json.load(f)

# 验证 token
def verify_token(token):
    """
    验证请求的 token 是否正确
    
    Args:
        token: 请求中的 token
        
    Returns:
        bool: token 是否有效
    """
    return token == config.get('token')

# 验证请求参数
def validate_request_data(data):
    """
    验证请求数据的完整性
    
    Args:
        data: 请求的 JSON 数据
        
    Returns:
        tuple: (是否有效, 错误信息)
    """
    # 检查必需字段
    required_fields = ['token', 'action', 'to', 'content']
    for field in required_fields:
        if field not in data:
            return False, f"缺少必需字段: {field}"
    
    # 验证 action
    if data['action'] not in ['sendtext', 'sendpic', 'sendfile']:
        return False, f"不支持的操作: {data['action']}"
    
    # 验证 to 字段是否为列表
    if not isinstance(data['to'], list):
        return False, "'to' 字段必须是数组"
    
    if len(data['to']) == 0:
        return False, "'to' 字段不能为空"
    
    # 验证 content 字段
    if not isinstance(data['content'], str) or len(data['content']) == 0:
        return False, "'content' 字段必须是非空字符串"
    
    return True, None


@app.route('/', methods=['POST'])
def send_message():
    """
    处理消息发送请求
    
    请求格式 (发送文本):
    {
        "token": "123123",
        "action": "sendtext",
        "to": ["联系人1", "联系人2"],
        "content": "消息内容"
    }
    
    请求格式 (发送图片URL或本地文件):
    {
        "token": "123123",
        "action": "sendpic",
        "to": ["联系人1", "联系人2"],
        "content": "图片的URL"
    }

    请求格式 (发送本地文件):
    {
        "token": "123123",
        "action": "sendfile",
        "to": ["联系人1", "联系人2"],
        "content": "C:\\path\\to\\file.zip"
    }
    
    响应格式:
    {
        "success": true,
        "message": "消息已加入队列",
        "queued_count": 2
    }
    """
    logger = logging.getLogger(__name__)
    
    try:
        # 获取 JSON 数据
        data = request.get_json()
        
        if not data:
            logger.warning("收到空请求")
            return jsonify({
                'success': False,
                'error': '请求体不能为空'
            }), 400
        
        logger.info(f"收到消息发送请求: to={data.get('to', [])}, content_length={len(data.get('content', ''))}")
        
        # 验证请求参数
        is_valid, error_msg = validate_request_data(data)
        if not is_valid:
            logger.warning(f"请求参数验证失败: {error_msg}")
            return jsonify({
                'success': False,
                'error': error_msg
            }), 400
        
        # 验证 token
        if not verify_token(data['token']):
            logger.warning(f"Token 验证失败")
            return jsonify({
                'success': False,
                'error': '无效的 token'
            }), 401
        
        # 将消息加入队列
        to_list = data['to']
        content = data['content']
        action = data['action']
        queued_count = message_queue.add_message(to_list, content, action)
        
        logger.info(f"消息已加入队列: 接收者数量={queued_count}, 队列大小={message_queue.get_queue_size()}")
        
        # 返回成功响应
        return jsonify({
            'success': True,
            'message': '消息已加入队列',
            'queued_count': queued_count,
            'queue_size': message_queue.get_queue_size()
        }), 200
        
    except Exception as e:
        logger.error(f"处理请求时发生错误: {str(e)}", exc_info=True)
        return jsonify({
            'success': False,
            'error': f'服务器内部错误: {str(e)}'
        }), 500


@app.route('/status', methods=['GET'])
def get_status():
    """
    获取服务状态
    
    响应格式:
    {
        "status": "running",
        "queue_size": 5
    }
    """
    return jsonify({
        'status': 'running',
        'queue_size': message_queue.get_queue_size()
    }), 200


@app.route('/health', methods=['GET'])
def health_check():
    """健康检查端点"""
    return jsonify({
        'status': 'healthy'
    }), 200


def main():
    """主函数,初始化并启动服务"""
    global message_queue, config
    
    # 加载配置
    try:
        config = load_config()
        print("配置加载成功")
    except Exception as e:
        print(f"加载配置失败: {str(e)}")
        return
    
    # 配置日志
    logger = setup_logging()
    
    # 创建并启动消息队列
    message_interval = config.get('message_interval', 1)
    lock_mouse = config.get('lock_mouse_during_send', False)
    message_queue = MessageQueue(message_interval=message_interval, lock_mouse_during_send=lock_mouse)
    message_queue.start()
    logger.info("消息队列已启动")
    
    # 获取服务器配置
    host = config.get('host', '127.0.0.1')
    port = config.get('port', 8808)
    
    # 启动监控进程 (monitor.py)
    global monitor_process
    monitor_interval = config.get('monitor_interval', 0)
    if monitor_interval > 0:
        logger.info(f"准备启动微信监控进程 (间隔: {monitor_interval}s)...")
        monitor_script = os.path.join(get_project_root(), 'scripts', 'monitor.py')
        try:
            # 使用 subprocess.Popen 启动独立进程
            # args 用 list 保证路径带空格时的兼容性
            monitor_process = subprocess.Popen([sys.executable, monitor_script])
            logger.info(f"监控进程已成功启动,PID: {monitor_process.pid}")
            
            # 注册退出时的清理函数
            def cleanup_monitor():
                if monitor_process and monitor_process.poll() is None:
                    logger.info("正在停止监控进程...")
                    monitor_process.terminate()
            atexit.register(cleanup_monitor)
        except Exception as e:
            logger.error(f"启动监控进程失败: {e}", exc_info=True)
    else:
        logger.info("监控间隔为 0,不启动独立监控进程。")
    
    # 启动 Flask 服务
    logger.info(f"微信自动化服务启动在 http://{host}:{port}")
    print(f"\n========================================")
    print(f"微信自动化服务已启动")
    print(f"监听地址: http://{host}:{port}")
    print(f"API 端点: POST http://{host}:{port}/")
    print(f"状态查询: GET http://{host}:{port}/status")
    print(f"健康检查: GET http://{host}:{port}/health")
    print(f"========================================\n")
    
    try:
        app.run(host=host, port=port, debug=False)
    except KeyboardInterrupt:
        logger.info("收到退出信号")
    finally:
        # 停止消息队列
        logger.info("正在停止消息队列...")
        message_queue.stop()
        logger.info("服务已停止")


if __name__ == '__main__':
    main()

reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall