@meet-im/meet-bot-jssdk 0.0.7 → 1.1.0

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.
package/README.md CHANGED
@@ -1,229 +1 @@
1
1
  # @meet-im/meet-bot-jssdk
2
-
3
- MeetIM Chatbot JavaScript SDK - 支持 Long Polling 消息获取和消息发送
4
-
5
- ## 安装
6
-
7
- ```bash
8
- pnpm add @meet-im/meet-bot-jssdk
9
- ```
10
-
11
- ## 快速开始
12
-
13
- ### 类实例风格
14
-
15
- ```typescript
16
- import { MeetBot } from '@meet-im/meet-bot-jssdk'
17
-
18
- const bot = new MeetBot({
19
- token: 'bot_id:secret',
20
- // baseUrl: 'https://staging-meet-api.miyachat.com', // 可选,默认值
21
- })
22
-
23
- bot.on('message', (msg) => {
24
- console.log('收到消息:', msg.content)
25
-
26
- if (msg.sessionInfo) {
27
- bot.sendMessage(msg.sessionInfo, { content: `收到: ${msg.content}` })
28
- }
29
- })
30
-
31
- bot.startPolling()
32
- ```
33
-
34
- ### 函数式风格
35
-
36
- ```typescript
37
- import { getUpdates, sendMessage } from '@meet-im/meet-bot-jssdk'
38
-
39
- const token = 'bot_id:secret'
40
- const baseUrl = 'https://staging-meet-api.miyachat.com'
41
-
42
- const updates = await getUpdates({ token, baseUrl })
43
-
44
- for (const update of updates) {
45
- if (update.message?.sessionInfo) {
46
- console.log('收到消息:', update.message.content)
47
-
48
- await sendMessage({
49
- token,
50
- baseUrl,
51
- sessionInfo: update.message.sessionInfo,
52
- msgContent: { content: '收到!' },
53
- })
54
- }
55
- }
56
- ```
57
-
58
- ## API 文档
59
-
60
- ### MeetBot 类
61
-
62
- #### 构造函数
63
-
64
- ```typescript
65
- new MeetBot(config: MeetBotConfig)
66
- ```
67
-
68
- | 参数 | 类型 | 必填 | 说明 |
69
- | ------------------ | ---------------- | ---- | ------------------------------------------------------ |
70
- | token | string | 是\* | Bot Token,格式:`bot_id:secret` |
71
- | botId | string \| number | 是\* | Bot ID(与 botToken 配合使用) |
72
- | botToken | string | 是\* | Bot Token(与 botId 配合使用) |
73
- | baseUrl | string | 否 | API 地址,默认 `https://staging-meet-api.miyachat.com` |
74
- | pollingLimit | number | 否 | 每次拉取消息条数,默认 100 |
75
- | longPollingTimeout | number | 否 | 长轮询超时时间(秒),默认 30 |
76
-
77
- \*token 或 (botId + botToken) 二选一
78
-
79
- #### 方法
80
-
81
- | 方法 | 说明 |
82
- | -------------------------------------- | ------------ |
83
- | `on(event, handler)` | 监听事件 |
84
- | `off(event, handler)` | 移除事件监听 |
85
- | `startPolling(options?)` | 启动长轮询 |
86
- | `stopPolling()` | 停止轮询 |
87
- | `isPolling()` | 获取轮询状态 |
88
- | `getUpdates(options?)` | 手动获取消息 |
89
- | `sendMessage(sessionInfo, msgContent)` | 发送消息 |
90
-
91
- #### startPolling options
92
-
93
- | 参数 | 类型 | 默认值 | 说明 |
94
- | -------------- | ------------------------ | ------ | ------------------------------ |
95
- | timeout | number | 30 | 长轮询超时时间(秒) |
96
- | limit | number | 100 | 每次拉取消息条数 |
97
- | retryDelay | number | 1000 | 重试延迟基础时间(毫秒) |
98
- | maxRetries | number | 0 | 最大重试次数(0 表示无限重试) |
99
- | onOffsetUpdate | (offset: number) => void | - | offset 更新回调 |
100
-
101
- #### 事件
102
-
103
- | 事件 | 参数 | 说明 |
104
- | --------------- | ------------ | ---------- |
105
- | `message` | `MsgContent` | 收到新消息 |
106
- | `error` | `Error` | 发生错误 |
107
- | `polling_start` | `void` | 轮询开始 |
108
- | `polling_stop` | `void` | 轮询停止 |
109
-
110
- ### 函数式 API
111
-
112
- #### getUpdates
113
-
114
- ```typescript
115
- getUpdates(params: GetUpdatesParams): Promise<BotUpdate[]>
116
- ```
117
-
118
- | 参数 | 类型 | 必填 | 默认值 | 说明 |
119
- | ------- | ------ | ---- | ------ | ----------------------- |
120
- | token | string | 是 | - | Bot Token |
121
- | baseUrl | string | 否 | - | API 地址 |
122
- | timeout | number | 否 | 0 | Long Polling 超时(秒) |
123
- | offset | number | 否 | 0 | 从该 seqId 之后获取 |
124
- | limit | number | 否 | 100 | 最大返回条数 |
125
-
126
- #### sendMessage
127
-
128
- ```typescript
129
- sendMessage(params: SendMessageParams): Promise<SendMessageResult>
130
- ```
131
-
132
- | 参数 | 类型 | 必填 | 说明 |
133
- | ----------- | ----------- | ---- | --------- |
134
- | token | string | 是 | Bot Token |
135
- | baseUrl | string | 否 | API 地址 |
136
- | sessionInfo | SessionInfo | 是 | 会话信息 |
137
- | msgContent | MsgContent | 是 | 消息内容 |
138
-
139
- ## 类型定义
140
-
141
- ```typescript
142
- type SessionType = 1 | 3 // 1: 私聊, 3: 群聊
143
-
144
- interface SessionInfo {
145
- firstID: number // 会话第一方 ID
146
- secondID: number // 会话第二方 ID(私聊为用户ID,群聊为群ID)
147
- sessionType: SessionType
148
- companyID?: number
149
- }
150
-
151
- type MsgType = 'NORMAL' | 'RECALL' | 'QUOTE'
152
-
153
- interface ExtraInfo {
154
- msgType?: MsgType
155
- [key: string]: unknown
156
- }
157
-
158
- interface MsgContent {
159
- content: string // 消息文本内容
160
- seqId?: number // 消息序列号
161
- timestamp?: number // 时间戳(秒)
162
- fromUid?: number // 发送者 UID
163
- atIds?: number[] // @用户 ID 列表
164
- extraInfo?: ExtraInfo // 额外信息
165
- sessionInfo?: SessionInfo
166
- }
167
-
168
- interface BotUpdate {
169
- message?: MsgContent
170
- }
171
-
172
- interface SendMessageResult {
173
- msgContent: MsgContent
174
- quoteMsg: unknown
175
- userProfileMap: Record<number, unknown>
176
- }
177
- ```
178
-
179
- ## 错误处理
180
-
181
- SDK 提供以下错误类型:
182
-
183
- | 错误类 | 说明 |
184
- | ----------------- | -------------- |
185
- | `MeetBotError` | 基础错误类 |
186
- | `ApiError` | API 返回的错误 |
187
- | `NetworkError` | 网络连接错误 |
188
- | `TimeoutError` | 请求超时错误 |
189
- | `ValidationError` | 参数验证错误 |
190
-
191
- ```typescript
192
- import { ApiError, NetworkError } from '@meet-im/meet-bot-jssdk'
193
-
194
- try {
195
- await bot.sendMessage(sessionInfo, { content: 'Hello' })
196
- } catch (error) {
197
- if (error instanceof ApiError) {
198
- console.error('API 错误:', error.code, error.statusCode)
199
- } else if (error instanceof NetworkError) {
200
- console.error('网络错误:', error.message)
201
- }
202
- }
203
- ```
204
-
205
- ## 开发
206
-
207
- ```bash
208
- # 安装依赖
209
- pnpm install
210
-
211
- # 构建
212
- pnpm build
213
-
214
- # 测试
215
- pnpm test
216
-
217
- # 类型检查
218
- pnpm typecheck
219
-
220
- # 代码格式化
221
- pnpm format
222
-
223
- # 代码检查
224
- pnpm lint
225
- ```
226
-
227
- ## License
228
-
229
- MIT