Skip to main content

一个命令就可启用的微信机器人

Project description

WhoChat

 ___       __   ___  ___  ________  ________  ___  ___  ________  _________
|\  \     |\  \|\  \|\  \|\   __  \|\   ____\|\  \|\  \|\   __  \|\___   ___\
\ \  \    \ \  \ \  \\\  \ \  \|\  \ \  \___|\ \  \\\  \ \  \|\  \|___ \  \_|
 \ \  \  __\ \  \ \   __  \ \  \\\  \ \  \    \ \   __  \ \   __  \   \ \  \
  \ \  \|\__\_\  \ \  \ \  \ \  \\\  \ \  \____\ \  \ \  \ \  \ \  \   \ \  \
   \ \____________\ \__\ \__\ \_______\ \_______\ \__\ \__\ \__\ \__\   \ \__\
    \|____________|\|__|\|__|\|_______|\|_______|\|__|\|__|\|__|\|__|    \|__|

一个依赖于 ComWeChatRobot提供的Com接口的微信机器人,在此之上提供了:

  1. 发布至PyPI,可以一键安装
  2. 命令行支持,可以方便通过命令操作(见下面使用说明)
  3. WebSocket消息推送
  4. JSON-RPC2.0方法调用,支持WebSocket和HTTP
  5. 简单的定时任务支持
  6. 其他

当前支持微信版本为3.7.0.30, Python版本3.8及以上

安装:

pip install whochat

若需要HTTP RPC支持,则是

pip install whochat[httprpc]

安装完成之后尝试使用whochat命令,理应看到以下输出:

D:\
> whochat --help
Usage: whochat [OPTIONS] COMMAND [ARGS]...

  微信机器人

  使用<子命令> --help查看使用说明

Options:
  --help  Show this message and exit.

Commands:
  list-wechat       列出当前运行的微信进程
  regserver         注册COM
  serve-message-ws  运行接收微信消息的Websocket服务
  serve-rpc-http    运行微信机器人RPC服务(JSON-RPC2.0), 使用HTTP接口
  serve-rpc-ws      运行微信机器人RPC服务(JSON-RPC2.0), 使用Websocket
  show-rpc-docs     列出RPC接口
  version           显示程序和支持微信的版本信息

使用

  1. 列出当前运行的微信进程:
> whochat list-wechat
PID: 102852
启动时间: 2022-08-27T22:22:02.290700
运行状态: running
用户名: wxid_hjkafa123a
---
  1. 注册COM服务:
> whochat regserver  # 注册
> whochat regserver --unreg  # 取消注册

注册一次就可以使用服务了。

  1. 开启微信消息转发WebSocket服务
> whochat serve-message-ws --help
Usage: whochat serve-message-ws [OPTIONS] [WX_PIDS]...

  运行接收微信消息的Websocket服务

  WX_PIDS: 微信进程PID

Options:
  -h, --host TEXT     Server host.  [default: localhost]
  -p, --port INTEGER  Server port  [default: 9001]
  --help              Show this message and exit.

该子命令接受一或多个微信PID作为位置参数,可以指定地址

> whochat serve-message-ws 102852
注册SIGINT信号处理程序: WechatWebsocketServer.shutdown
开始运行微信消息接收服务
开始向客户端广播接收到的微信消息
开始运行微信Websocket服务,地址为:<localhost:9001>
{'wxId': 'wxid_hjkafa123a', 'wxNumber': 'wxid_hjkafa123a', 'wxNickName': 'Cider', 'Sex': '男', 'wxSignature': 'null', 'wxBigAvatar': 'http://wx.qlogo.cn/mmhead/ver_1/R50J6cxxTRzE28sY32DVJibeRUZPiaPotzPVjuReXZsONBdNZXQChSfrK0rDWh8RKS5ibt7VJdK0p22YJrOGjRA051lY9mwkt6ONruLmYTyBAA/0', 'wxSmallAvatar': 'http://wx.qlogo.cn/mmhead/ver_1/R50J6cxxTRzE28sY32DVJibeRUZPiaPotzPVjuReXZsONBdNZXQChSfrK0rDWh8RKS5ibt7VJdK0p22YJrOGjRA051lY9mwkt6ONruLmYTyBAA/132', 'wxNation': 'CN', 'wxProvince': 'Anhui', 'wxCity': 'Hefei', 'PhoneNumber': 'null'}
开启Robot消息推送

默认地址为localhost:9001,连接测试: WebSocket测试

当前接收消息格式示例:

{
    "extrainfo": {
        "is_at_msg": true,
        "at_user_list": [
            "wx_user_id1",
            "wx_user_id2"
        ],
        "member_count": 23
    },
    "filepath": "",
    "isSendMsg": 0,
    "message": "@wx_user1\u2005@wx_user2\u2005Hello",
    "msgid": 7495392442139043211,
    "pid": 17900,
    "sender": "20813132945@chatroom",
    "time": "2022-09-03 22: 10: 33",
    "type": 1,
    "wxid": "wx_user_id10"
}
  1. 开启WebSocket RPC服务进行方法调用:
> whochat serve-rpc-ws
PID: 28824
注册SIGINT信号处理程序: run.<locals>.shutdown
运行微信机器人RPC websocket服务, 地址为<localhost:9002>

默认地址为localhost:9002,测试发送消息给文件传输助手,记得先调用start_robot_service注入dll,现在调用方法时会自动注入dll 发送消息 RPC所有方法和参数可通过whochat show-rpc-docs命令查看或者whochat show-rpc-docs --json > docs.json生成JSON文档(rpc-api.json):

> whochat show-rpc-docs --help
Usage: whochat show-rpc-docs [OPTIONS]

  列出RPC接口

  whochat show-rpc-docs
  or
  whochat show-rpc-docs --json > docs.json

Options:
  --json  JSON文档
  --help  Show this message and exit.
  1. 定时任务:

在每天上午6点整喊基友起床,同样使用RPC调用schedule_a_job(获取接口文档见4),

{
    "jsonrpc": "2.0",
    "method": "schedule_a_job",
    "params": {
        "name": "GETUP",
        "unit": "days",
        "every": 1,
        "at": "08:00:00",
        "do": {
            "func": "send_text",
            "args": [
                102852,
                "jiyou",
                "GET UP!"
            ]
        },
        "description": "",
        "tags": [
            "jiyou"
        ]
    },
    "id": 4
}

CHANGELOG

Tags

v1.3.1

  • 修改日志级别,增加日志文件记录

v1.3.0

  • 增加RPC Websocket客户端
  • 消息转发命令行增加--welcome参数决定是否在客户端连接是发送"hello"
  • hook_方法返回路径
  • 增加prevent_revoke阻止文件消息被撤回时被删除

v1.2.1

  • 更新适配 Robot DLL
  • 添加_comtypes.py方便在非Windows平台开发

v.1.1.0

  • 更新 Robot DLL
  • 接收消息增加extrainfo字段,当消息为群消息时可获取群成员数量和被@的人的微信ID
  • 命令行增加log-level选项控制日志级别
  • 调用bot方法时自动注入dll
  • 添加 docs/rpc/api.json

v1.0.1

  • 添加Python版本依赖说明

欢迎学习交流,点个star⭐️

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

whochat-1.3.2.tar.gz (219.3 kB view details)

Uploaded Source

Built Distribution

whochat-1.3.2-py3-none-any.whl (221.1 kB view details)

Uploaded Python 3

File details

Details for the file whochat-1.3.2.tar.gz.

File metadata

  • Download URL: whochat-1.3.2.tar.gz
  • Upload date:
  • Size: 219.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for whochat-1.3.2.tar.gz
Algorithm Hash digest
SHA256 46d9bb31ac3aa888ed176cfa9dbb7591df2778f97f47665e90c43a99a1fdbe56
MD5 d11a6b914760d146718afe3ecac67028
BLAKE2b-256 fe41afac2a54c0f830631e4ac48c938d8cf2dcdedb7955503164d47f7e0c0126

See more details on using hashes here.

Provenance

File details

Details for the file whochat-1.3.2-py3-none-any.whl.

File metadata

  • Download URL: whochat-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 221.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for whochat-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 96dcc0acb2ddc92d01a34a502fa29909778954d2aa287d74fcb64df4c6e6bbe6
MD5 990d58aa98cb0059ee296299dfa42773
BLAKE2b-256 eb17dcaa0c10d8e559e6e47303a65157c585873f975df69cd885735ae1f5b8ef

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page