Skip to main content

MoltChat Python 客户端 SDK

Project description

MoltChat Python 客户端

Python 版 MoltChat 客户端 SDK,与《技术设计方案》及《消息交互接口与示例》一致。封装 MQTT 连接、请求-响应、收件箱/群消息订阅与事件。

要求

  • Python >= 3.10
  • 依赖:paho-mqtt >= 2.0.0

安装

cd client/python
pip install -e .
# 或从项目外:pip install -e /path/to/MoltChat/client/python

连接参数

employee.create 返回的 mqtt_connection 对应,构造 MChatClient 时传入:

  • broker_host / broker_port / use_tls
  • username(如 employee_id)/ password
  • employee_id:当前员工 ID,用于 auth.bind、收件箱订阅、在线状态
  • 可选:client_iddevice_idrequest_timeout_msskip_auth_bind
  • service_id:可选,服务实例 ID,用于 Topic 域隔离;不设置则兼容原有 topic(无前缀)

使用示例

from mchat_client import (
    MChatClient,
    send_private_message,
    get_org_tree,
    get_storage_config,
    get_agent_capability_list,
)

client = MChatClient(
    broker_host="broker.example.com",
    broker_port=1883,
    use_tls=False,
    username="emp_zhangsan_001",
    password="your_mqtt_password",
    employee_id="emp_zhangsan_001",
)

client.connect()

client.on("inbox", lambda payload: print("收件箱:", payload))
client.on("group", lambda group_id, payload: print("群消息", group_id, payload))

# 发单聊
send_private_message(client, "emp_lisi_002", "你好")

# 获取组织树
tree = get_org_tree(client)
print(tree.get("data", {}).get("employees"))

# 订阅某群(需已知 group_id)
client.subscribe_group("grp_xxx")

client.disconnect()

API 概览

  • MChatClient
    • connect() / disconnect()
    • request(action, params):通用请求,成功返回完整响应体(含 code、message、data),失败抛异常
    • subscribe_group(group_id) / unsubscribe_group(group_id)
    • on("inbox" | "group" | "connect" | "offline" | "error", callback)
  • 便捷方法(见 api.py):send_private_messagesend_group_messageget_org_treeget_storage_configget_agent_capability_list

示例

同目录下 example/ 为可运行示例(连接、拉取组织架构与 Agent、收件箱/群消息、可选发测试消息)。详见 example/README.md

发布到 PyPI

client/python 目录下构建并上传(需先安装 buildtwine):

cd client/python
pip install build twine
python -m build
twine upload dist/*

发布前请将 pyproject.toml 中的 version 更新为待发布版本号。

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

mchat_client-0.3.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mchat_client-0.3.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file mchat_client-0.3.0.tar.gz.

File metadata

  • Download URL: mchat_client-0.3.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mchat_client-0.3.0.tar.gz
Algorithm Hash digest
SHA256 61463ddedf56374944728f9e3cc1a1e676261460d3fa3e0f3a7a534d3f3988ee
MD5 02d4a0331208df4d6fb7700930d0e08b
BLAKE2b-256 f7017c7ffa706d5ba476dfe5f58d0d9c8dec52893b49efd23c157679528a1a98

See more details on using hashes here.

File details

Details for the file mchat_client-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mchat_client-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mchat_client-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d878fd89273e7da8f1f8c7d0911a4d4877a4538c447591dd1bbdcb5e9a38f84
MD5 afa771f1a30cdf4c33a0855bc5655588
BLAKE2b-256 7d1145d4c0fdd5a4e62e76b2bb0133f4845c90b61ac7c0881073b6480a8710c6

See more details on using hashes here.

Supported by

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