wecom-aibot-python-sdk 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aibot/__init__.py +50 -0
- aibot/api.py +74 -0
- aibot/client.py +362 -0
- aibot/crypto_utils.py +73 -0
- aibot/logger.py +47 -0
- aibot/message_handler.py +89 -0
- aibot/types.py +170 -0
- aibot/utils.py +32 -0
- aibot/ws.py +574 -0
- wecom_aibot_python_sdk-1.0.0.dist-info/METADATA +365 -0
- wecom_aibot_python_sdk-1.0.0.dist-info/RECORD +14 -0
- wecom_aibot_python_sdk-1.0.0.dist-info/WHEEL +5 -0
- wecom_aibot_python_sdk-1.0.0.dist-info/licenses/LICENSE +21 -0
- wecom_aibot_python_sdk-1.0.0.dist-info/top_level.txt +1 -0
aibot/types.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"""
|
|
2
|
+
企业微信智能机器人 SDK 类型定义
|
|
3
|
+
|
|
4
|
+
对标 Node.js SDK src/types/ 目录下的全部类型:
|
|
5
|
+
- common.ts → Logger Protocol
|
|
6
|
+
- config.ts → WSClientOptions dataclass
|
|
7
|
+
- message.ts → MessageType 枚举, BaseMessage 等消息类型
|
|
8
|
+
- api.ts → WsCmd 常量, WsFrame, 各种回复/发送消息体, TemplateCard 等
|
|
9
|
+
- event.ts → EventType 枚举, EventMessage 等事件类型
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
from enum import Enum
|
|
14
|
+
from typing import Any, Dict, List, Optional, Protocol, runtime_checkable
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# ========== 通用基础类型 (common.ts) ==========
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@runtime_checkable
|
|
21
|
+
class Logger(Protocol):
|
|
22
|
+
"""日志接口"""
|
|
23
|
+
|
|
24
|
+
def debug(self, message: str, *args: Any) -> None: ...
|
|
25
|
+
def info(self, message: str, *args: Any) -> None: ...
|
|
26
|
+
def warn(self, message: str, *args: Any) -> None: ...
|
|
27
|
+
def error(self, message: str, *args: Any) -> None: ...
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# ========== 配置类型 (config.ts) ==========
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass
|
|
34
|
+
class WSClientOptions:
|
|
35
|
+
"""WSClient 配置选项"""
|
|
36
|
+
|
|
37
|
+
bot_id: str
|
|
38
|
+
"""机器人 ID(在企业微信后台获取)"""
|
|
39
|
+
|
|
40
|
+
secret: str
|
|
41
|
+
"""机器人 Secret(在企业微信后台获取)"""
|
|
42
|
+
|
|
43
|
+
reconnect_interval: int = 1000
|
|
44
|
+
"""WebSocket 重连基础延迟(毫秒),实际延迟按指数退避递增,默认 1000"""
|
|
45
|
+
|
|
46
|
+
max_reconnect_attempts: int = 10
|
|
47
|
+
"""最大重连次数,默认 10,设为 -1 表示无限重连"""
|
|
48
|
+
|
|
49
|
+
heartbeat_interval: int = 30000
|
|
50
|
+
"""心跳间隔(毫秒),默认 30000"""
|
|
51
|
+
|
|
52
|
+
request_timeout: int = 10000
|
|
53
|
+
"""请求超时时间(毫秒),默认 10000"""
|
|
54
|
+
|
|
55
|
+
ws_url: str = ""
|
|
56
|
+
"""自定义 WebSocket 连接地址,默认 wss://openws.work.weixin.qq.com"""
|
|
57
|
+
|
|
58
|
+
logger: Optional[Any] = None
|
|
59
|
+
"""自定义日志函数"""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# ========== WebSocket 命令常量 (api.ts) ==========
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class WsCmd:
|
|
66
|
+
"""WebSocket 命令类型常量"""
|
|
67
|
+
|
|
68
|
+
# ========== 开发者 → 企业微信 ==========
|
|
69
|
+
SUBSCRIBE = "aibot_subscribe"
|
|
70
|
+
"""认证订阅"""
|
|
71
|
+
|
|
72
|
+
HEARTBEAT = "ping"
|
|
73
|
+
"""心跳"""
|
|
74
|
+
|
|
75
|
+
RESPONSE = "aibot_respond_msg"
|
|
76
|
+
"""回复消息"""
|
|
77
|
+
|
|
78
|
+
RESPONSE_WELCOME = "aibot_respond_welcome_msg"
|
|
79
|
+
"""回复欢迎语"""
|
|
80
|
+
|
|
81
|
+
RESPONSE_UPDATE = "aibot_respond_update_msg"
|
|
82
|
+
"""更新模板卡片"""
|
|
83
|
+
|
|
84
|
+
SEND_MSG = "aibot_send_msg"
|
|
85
|
+
"""主动发送消息"""
|
|
86
|
+
|
|
87
|
+
# ========== 企业微信 → 开发者 ==========
|
|
88
|
+
CALLBACK = "aibot_msg_callback"
|
|
89
|
+
"""消息推送回调"""
|
|
90
|
+
|
|
91
|
+
EVENT_CALLBACK = "aibot_event_callback"
|
|
92
|
+
"""事件推送回调"""
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# ========== 消息类型枚举 (message.ts) ==========
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class MessageType(str, Enum):
|
|
99
|
+
"""消息类型枚举"""
|
|
100
|
+
|
|
101
|
+
Text = "text"
|
|
102
|
+
"""文本消息"""
|
|
103
|
+
|
|
104
|
+
Image = "image"
|
|
105
|
+
"""图片消息"""
|
|
106
|
+
|
|
107
|
+
Mixed = "mixed"
|
|
108
|
+
"""图文混排消息"""
|
|
109
|
+
|
|
110
|
+
Voice = "voice"
|
|
111
|
+
"""语音消息"""
|
|
112
|
+
|
|
113
|
+
File = "file"
|
|
114
|
+
"""文件消息"""
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# ========== 事件类型枚举 (event.ts) ==========
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class EventType(str, Enum):
|
|
121
|
+
"""事件类型枚举"""
|
|
122
|
+
|
|
123
|
+
EnterChat = "enter_chat"
|
|
124
|
+
"""进入会话事件:用户当天首次进入机器人单聊会话"""
|
|
125
|
+
|
|
126
|
+
TemplateCardEvent = "template_card_event"
|
|
127
|
+
"""模板卡片事件:用户点击模板卡片按钮"""
|
|
128
|
+
|
|
129
|
+
FeedbackEvent = "feedback_event"
|
|
130
|
+
"""用户反馈事件:用户对机器人回复进行反馈"""
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# ========== 模板卡片类型枚举 (api.ts) ==========
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
class TemplateCardType(str, Enum):
|
|
137
|
+
"""卡片类型枚举"""
|
|
138
|
+
|
|
139
|
+
TextNotice = "text_notice"
|
|
140
|
+
"""文本通知模版卡片"""
|
|
141
|
+
|
|
142
|
+
NewsNotice = "news_notice"
|
|
143
|
+
"""图文展示模版卡片"""
|
|
144
|
+
|
|
145
|
+
ButtonInteraction = "button_interaction"
|
|
146
|
+
"""按钮交互模版卡片"""
|
|
147
|
+
|
|
148
|
+
VoteInteraction = "vote_interaction"
|
|
149
|
+
"""投票选择模版卡片"""
|
|
150
|
+
|
|
151
|
+
MultipleInteraction = "multiple_interaction"
|
|
152
|
+
"""多项选择模版卡片"""
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
# ========== WebSocket 帧结构 (api.ts) ==========
|
|
156
|
+
#
|
|
157
|
+
# Python 中使用 dict 表示 JSON 帧,以下提供类型别名和工厂函数辅助使用。
|
|
158
|
+
# WsFrame 在 Python 中直接使用 Dict[str, Any],字段:
|
|
159
|
+
# cmd?: str — 命令类型
|
|
160
|
+
# headers: dict — 请求头 { req_id: str, ... }
|
|
161
|
+
# body?: Any — 消息体
|
|
162
|
+
# errcode?: int — 响应错误码
|
|
163
|
+
# errmsg?: str — 响应错误信息
|
|
164
|
+
|
|
165
|
+
# 类型别名(用于类型提示)
|
|
166
|
+
WsFrame = Dict[str, Any]
|
|
167
|
+
"""WebSocket 帧结构,等价于 { cmd?, headers: { req_id, ... }, body?, errcode?, errmsg? }"""
|
|
168
|
+
|
|
169
|
+
WsFrameHeaders = Dict[str, Any]
|
|
170
|
+
"""仅包含 headers 的 WsFrame 子集,用于 reply 等方法的参数类型"""
|
aibot/utils.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""
|
|
2
|
+
通用工具方法
|
|
3
|
+
|
|
4
|
+
对标 Node.js SDK src/utils.ts
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import time
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def generate_random_string(length: int = 8) -> str:
|
|
12
|
+
"""
|
|
13
|
+
生成随机字符串
|
|
14
|
+
|
|
15
|
+
:param length: 随机字符串长度,默认 8
|
|
16
|
+
:return: 随机十六进制字符串
|
|
17
|
+
"""
|
|
18
|
+
return os.urandom((length + 1) // 2).hex()[:length]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def generate_req_id(prefix: str) -> str:
|
|
22
|
+
"""
|
|
23
|
+
生成唯一请求 ID
|
|
24
|
+
|
|
25
|
+
格式:{prefix}_{timestamp}_{random}
|
|
26
|
+
|
|
27
|
+
:param prefix: 前缀,通常为 cmd 名称
|
|
28
|
+
:return: 唯一请求 ID
|
|
29
|
+
"""
|
|
30
|
+
timestamp = int(time.time() * 1000)
|
|
31
|
+
random_str = generate_random_string()
|
|
32
|
+
return f"{prefix}_{timestamp}_{random_str}"
|