Skip to main content

gewechat-python

gewechat python client

Usage

安装 gewechat client

pip install gewechat-client

使用login函数可以直接在控制台打印二维码,扫码后自动执行登录确认,详见下方使用demo代码

使用demo

from gewechat_client import GewechatClient
import os

def main():
    # 配置参数
    base_url = os.environ.get("BASE_URL", "http://127.0.0.1:2531/v2/api")
    token = os.environ.get("GEWECHAT_TOKE", "xxx")
    app_id = os.environ.get("APP_ID", "xxx")
    send_msg_nickname = "张伟" # 要发送消息的好友昵称

    # 创建 GewechatClient 实例
    client = GewechatClient(base_url, token)

    # 登录, 自动创建二维码,扫码后自动登录
    app_id, error_msg = client.login(app_id=app_id)
    if error_msg:
        print("登录失败")
        return
    try:

        # 获取好友列表
        fetch_contacts_list_result = client.fetch_contacts_list(app_id)
        if fetch_contacts_list_result.get('ret') != 200 or not fetch_contacts_list_result.get('data'):
            print("获取通讯录列表失败:", fetch_contacts_list_result)
            return
        # {'ret': 200, 'msg': '操作成功', 'data': {'friends': ['weixin', 'fmessage', 'medianote', 'floatbottle', 'wxid_abcxx'], 'chatrooms': ['1234xx@chatroom'], 'ghs': ['gh_xx']}}
        friends = fetch_contacts_list_result['data'].get('friends', [])
        if not friends:
            print("获取到的好友列表为空")
            return
        print("获取到的好友列表:", friends)

        # 获取好友的简要信息
        friends_info = client.get_brief_info(app_id, friends)
        if friends_info.get('ret') != 200 or not friends_info.get('data'):
            print("获取好友简要信息失败:", friends_info)
            return
        # {
        #     "ret": 200,
        #     "msg": "获取联系人信息成功",
        #     "data": [
        #         {
        #             "userName": "weixin",
        #             "nickName": "微信团队",
        #             "pyInitial": "WXTD",
        #             "quanPin": "weixintuandui",
        #             "sex": 0,
        #             "remark": "",
        #             "remarkPyInitial": "",
        #             "remarkQuanPin": "",
        #             "signature": null,
        #             "alias": "",
        #             "snsBgImg": null,
        #             "country": "",
        #             "bigHeadImgUrl": "https: //wx.qlogo.cn/mmhead/Q3auHgzwzM6H8bJKHKyGY2mk0ljLfodkWnrRbXLn3P11f68cg0ePxA/0",
        #             "smallHeadImgUrl": "https://wx.qlogo.cn/mmhead/Q3auHgzwzM6H8bJKHKyGY2mk0ljLfodkWnrRbXLn3P11f68cg0ePxA/132",
        #             "description": null,
        #             "cardImgUrl": null,
        #             "labelList": null,
        #             "province": "",
        #             "city": "",
        #             "phoneNumList": null
        #         }
        #     ]
        # }
        
        # 找对目标好友的wxid
        friends_info_list = friends_info['data']
        if not friends_info_list:
            print("获取到的好友简要信息列表为空")
            return
        wxid = None
        for friend_info in friends_info_list:
            if friend_info.get('nickName') == send_msg_nickname:
                print("找到好友:", friend_info)
                wxid = friend_info.get('userName')
                break
        if not wxid:
            print(f"没有找到好友: {send_msg_nickname} 的wxid")
            return
        print("找到好友:", wxid)

        # 发送消息
        send_msg_result = client.post_text(app_id, wxid, "你好啊")
        if send_msg_result.get('ret') != 200:
            print("发送消息失败:", send_msg_result)
            return
        print("发送消息成功:", send_msg_result)
    except Exception as e:
        print("Failed to fetch contacts list:", str(e))

if __name__ == "__main__":
    main()

Metadata

Release files for gewechat-client 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gewechat-client 0.2.2
File Size Uploaded
gewechat_client-0.2.2.tar.gz 17.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gewechat-client 0.2.2
File Interpreter ABI Platform
gewechat_client-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 37.7 kB

Release files / gewechat_client-0.2.2.tar.gz

Download URL gewechat_client-0.2.2.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
42a1ed176e2a806b97b76ba2a89d24b7898ee6868db62a6c9c8141cc62989702
BLAKE2b-256 checksum
How to use checksums
69977e5ba34551559de1da12ba8f33661d9a4597f79db956250cb787751bdbb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / gewechat_client-0.2.2-py3-none-any.whl

Download URL gewechat_client-0.2.2-py3-none-any.whl
Size 20.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d1c39608d97d03490efb4b0482af55431a449bcda6a6440c285cb7347676a6f
BLAKE2b-256 checksum
How to use checksums
4e46f2667017945c02a8fa36eaf57cd36490a2a344f456227d3e70fede7ea678
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page