lansenger-cli 1.0.0 → 1.0.2

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 ADDED
@@ -0,0 +1,395 @@
1
+ # Lansenger CLI (TypeScript)
2
+
3
+ 蓝信(Lansenger)命令行工具 — 在终端直接调用蓝信开放平台 API,发送消息、管理群组、查询人员/部门、操作日程与待办等。
4
+
5
+ 命令语法与 Python/Go 版完全一致,安装任一版本即可使用。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ npm install -g lansenger-cli
11
+ ```
12
+
13
+ 或从源码安装:
14
+
15
+ ```bash
16
+ npm install
17
+ npm run build
18
+ npm link
19
+ ```
20
+
21
+ 需要 Node.js ≥ 18.0.0。
22
+
23
+ ## 快速开始
24
+
25
+ ### 1. 配置凭证
26
+
27
+ 通过 `config set` 命令保存凭证(按 profile 隔离存储在 `~/.lansenger/sdk_state.json`,密钥脱敏显示,文件权限 0600):
28
+
29
+ ```bash
30
+ lansenger config set app_id YOUR_APP_ID
31
+ lansenger config set app_secret YOUR_APP_SECRET
32
+ lansenger config set api_gateway_url https://apigw.lx.qianxin.com/open/apigw # 私有部署需修改
33
+ ```
34
+
35
+ **OAuth2 用户认证(需要获取 userToken 时填写)**:
36
+
37
+ ```bash
38
+ lansenger config set passport_url https://passport.lx.qianxin.com
39
+ ```
40
+
41
+ **回调接收(需要解析/验签回调 Webhook 时填写)**:
42
+
43
+ ```bash
44
+ lansenger config set encoding_key YOUR_ENCODING_KEY
45
+ lansenger config set callback_token YOUR_CALLBACK_TOKEN
46
+ ```
47
+
48
+ 也可以通过环境变量配置(适合 CI/CD 或临时使用):
49
+
50
+ ```bash
51
+ export LANSENGER_APP_ID=YOUR_APP_ID
52
+ export LANSENGER_APP_SECRET=YOUR_APP_SECRET
53
+ export LANSENGER_ENCODING_KEY=YOUR_ENCODING_KEY
54
+ export LANSENGER_CALLBACK_TOKEN=YOUR_CALLBACK_TOKEN
55
+ ```
56
+
57
+ ### 2. 查看配置
58
+
59
+ ```bash
60
+ lansenger config show
61
+ ```
62
+
63
+ ### 3. 健康检查
64
+
65
+ 验证凭证是否正确、能否成功获取 app token:
66
+
67
+ ```bash
68
+ lansenger health check
69
+ ```
70
+
71
+ ## 命令总览
72
+
73
+ | 命令组 | 说明 | 子命令 |
74
+ |--------|------|--------|
75
+ | `config` | 管理凭证配置 | `set`, `show`, `clear`, `list-profiles` |
76
+ | `message` | 发送与管理消息 | `send-text`, `send-markdown`, `send-file`, `send-image-url`, `send-link-card`, `send-app-articles`, `send-app-card`, `send-oacard`, `send-bot-message`, `send-group-message`, `send-account-message`, `send-user-message`, `update-dynamic-card`, `revoke`, `query-groups`, `send-reminder` |
77
+ | `group` | 管理群组 | `create`, `info`, `members`, `list`, `check`, `update`, `update-members`, `dismiss` |
78
+ | `staff` | 查询人员信息 | `basic-info`, `detail`, `ancestors`, `id-mapping`, `org-extra-fields`, `search`, `org-info` |
79
+ | `department` | 查询部门信息 | `detail`, `children`, `staffs` |
80
+ | `calendar` | 日程操作 | `primary`, `create-schedule`, `fetch-schedule`, `delete-schedule`, `list-schedules`, `attendees`, `add-attendees`, `delete-attendees`, `update-schedule`, `attendee-meta` |
81
+ | `todo` | 待办任务管理 | `create`, `update`, `update-status`, `delete`, `list`, `fetch-by-id`, `fetch-by-source`, `status-counts`, `executor-status`, `add-executors`, `delete-executors`, `executor-list` |
82
+ | `oauth` | OAuth2 用户认证 | `authorize-url`, `exchange-code`, `refresh-token`, `user-info`, `parse-callback`, `validate-state` |
83
+ | `callback` | 回调事件解析 | `parse-payload`, `decrypt-payload`, `verify-signature`, `event-types` |
84
+ | `media` | 媒体文件操作 | `upload`, `upload-app`, `download`, `download-to-file`, `path` |
85
+ | `streaming` | 流式消息(AI 场景) | `create`, `fetch` |
86
+ | `chat` | 会话与消息记录 | `list`, `messages` |
87
+ | `health` | 连接健康检查 | `check` |
88
+
89
+ ## 常用示例
90
+
91
+ ### 发送消息
92
+
93
+ ```bash
94
+ # 发送文本消息
95
+ lansenger message send-text chat123 "Hello World"
96
+
97
+ # 发送 Markdown 消息
98
+ lansenger message send-markdown chat123 "**Bold** text"
99
+
100
+ # 发送文件
101
+ lansenger message send-file chat123 /path/to/file.pdf
102
+
103
+ # 发送带图片 URL 的消息
104
+ lansenger message send-image-url chat123 https://example.com/photo.jpg
105
+
106
+ # 发送链接卡片
107
+ lansenger message send-link-card chat123 "公告标题" https://example.com --desc "点击查看详情"
108
+
109
+ # 发送应用卡片
110
+ lansenger message send-app-card chat123 "卡片标题" --content "正文内容" --card-link https://example.com
111
+
112
+ # 发送多条图文(appArticles)
113
+ lansenger message send-app-articles chat123 '{"title":"文章1","url":"https://a.com"}' '{"title":"文章2","url":"https://b.com"}'
114
+
115
+ # 发送 OA 审批卡片
116
+ lansenger message send-oacard chat123 "审批标题" --head "审批通知" --field '{"key":"申请人","value":"张三"}' --link https://app.com/approve
117
+
118
+ # 群内发送并 @all
119
+ lansenger message send-text group123 "全员通知" --group --mention-all
120
+
121
+ # 群内 @指定人
122
+ lansenger message send-text group123 "请查看" --group --mention staff001 --mention staff002
123
+
124
+ # 机器人通道发送消息
125
+ lansenger message send-bot-message text '{"content":"通知内容"}' --chat-id user001 --chat-id user002
126
+
127
+ # 群消息通道发送(需要 user_token)
128
+ lansenger message send-group-message group123 text '{"content":"群消息"}' --user-token YOUR_USER_TOKEN --sender-id staff001
129
+
130
+ # 应用账号通道发送
131
+ lansenger message send-account-message text '{"content":"账号消息"}' --chat-id user001 --account-id acct001
132
+
133
+ # 用户通道发送(需要 user_token)
134
+ lansenger message send-user-message user001 text '{"content":"私聊消息"}' --user-token YOUR_USER_TOKEN
135
+
136
+ # 撤回消息
137
+ lansenger message revoke msg001 msg002
138
+
139
+ # 发送提醒
140
+ lansenger message send-reminder msg001 --type 1 --type 2 --user staff001 --user staff002
141
+
142
+ # 查询群 ID 列表
143
+ lansenger message query-groups --page 1 --size 100
144
+ ```
145
+
146
+ ### 群组管理
147
+
148
+ ```bash
149
+ # 创建群组
150
+ lansenger group create "项目群" org001 --staff staff001 --staff staff002
151
+
152
+ # 查看群信息
153
+ lansenger group info group123
154
+
155
+ # 查看群成员
156
+ lansenger group members group123
157
+
158
+ # 查看群列表
159
+ lansenger group list --user-token YOUR_USER_TOKEN
160
+
161
+ # 检查用户是否在群内
162
+ lansenger group check group123 --staff-id staff001
163
+
164
+ # 更新群信息
165
+ lansenger group update group123 --name "新名称" --desc "新描述"
166
+
167
+ # 添加/移除成员
168
+ lansenger group update-members group123 --add staff003 --remove staff001
169
+ ```
170
+
171
+ ### 人员查询
172
+
173
+ ```bash
174
+ # 查看人员基本信息
175
+ lansenger staff basic-info staff001
176
+
177
+ # 查看人员详细信息
178
+ lansenger staff detail staff001
179
+
180
+ # 搜索人员
181
+ lansenger staff search 张三
182
+
183
+ # 手机号/邮箱映射 staff ID
184
+ lansenger staff id-mapping org001 phone 13800138000
185
+
186
+ # 查看组织信息
187
+ lansenger staff org-info org001
188
+ ```
189
+
190
+ ### 部门查询
191
+
192
+ ```bash
193
+ # 查看部门详情
194
+ lansenger department detail dept001
195
+
196
+ # 查看子部门
197
+ lansenger department children dept001
198
+
199
+ # 查看部门内人员
200
+ lansenger department staffs dept001
201
+ ```
202
+
203
+ ### 会话与消息记录
204
+
205
+ ```bash
206
+ # 获取会话列表(需要 user_token)
207
+ lansenger chat list --user-token YOUR_USER_TOKEN
208
+
209
+ # 只看群聊
210
+ lansenger chat list --type 2 --user-token YOUR_USER_TOKEN
211
+
212
+ # 搜索会话(关键词)
213
+ lansenger chat list --keyword 张三 --user-token YOUR_USER_TOKEN
214
+
215
+ # 获取私聊消息记录
216
+ lansenger chat messages --staff-id staff001 --user-token YOUR_USER_TOKEN
217
+
218
+ # 获取群聊消息记录
219
+ lansenger chat messages --group-id group123 --user-token YOUR_USER_TOKEN
220
+ ```
221
+
222
+ ### 日程操作
223
+
224
+ ```bash
225
+ # 获取主日历
226
+ lansenger calendar primary --user-token YOUR_USER_TOKEN
227
+
228
+ # 创建日程(start/end 为秒级时间戳)
229
+ lansenger calendar create-schedule cal001 "周会" 1747539600 1747543200 \
230
+ '[{"staffId":"staff001","attendeeFlag":"yes"}]' \
231
+ --desc "每周例会" --user-token YOUR_USER_TOKEN
232
+
233
+ # 查看日程列表(start/end 为秒级时间戳)
234
+ lansenger calendar list-schedules cal001 1747539600 1747603200 --user-token YOUR_TOKEN
235
+
236
+ # 查看日程详情
237
+ lansenger calendar fetch-schedule cal001 schedule001 --user-token YOUR_TOKEN
238
+
239
+ # 删除日程
240
+ lansenger calendar delete-schedule cal001 schedule001 --user-token YOUR_TOKEN
241
+ ```
242
+
243
+ ### 待办任务
244
+
245
+ ```bash
246
+ # 创建待办
247
+ lansenger todo create "审批文档" https://app.com/doc https://app.com/doc \
248
+ "staff001,staff002" org001 --desc "请审批" --type 2
249
+
250
+ # 更新待办状态(11=待阅, 12=已阅, 21=待办, 22=已办)
251
+ lansenger todo update-status task001 22 org001
252
+
253
+ # 更新执行人状态
254
+ lansenger todo executor-status '[{"executorId":"staff001","status":"22"}]' org001 --task-id task001
255
+
256
+ # 查看待办列表
257
+ lansenger todo list org001 --status 21,22
258
+
259
+ # 删除待办
260
+ lansenger todo delete task001 org001
261
+ ```
262
+
263
+ ### OAuth2 用户认证
264
+
265
+ ```bash
266
+ # 生成授权 URL
267
+ lansenger oauth authorize-url https://yourapp.com/callback --scope basic_userinfor
268
+
269
+ # 交换 code 获取 user token
270
+ lansenger oauth exchange-code AUTH_CODE --redirect-uri https://yourapp.com/callback
271
+
272
+ # 刷新 user token
273
+ lansenger oauth refresh-token YOUR_REFRESH_TOKEN
274
+
275
+ # 获取用户信息
276
+ lansenger oauth user-info YOUR_USER_TOKEN
277
+
278
+ # 解析 OAuth2 回调 URL 参数
279
+ lansenger oauth parse-callback "code=xxx&state=yyy"
280
+
281
+ # 验证回调 state 参数
282
+ lansenger oauth validate-state yyy yyy
283
+ ```
284
+
285
+ ### 回调事件
286
+
287
+ ```bash
288
+ # 查看所有回调事件类型
289
+ lansenger callback event-types
290
+
291
+ # 解析回调数据
292
+ lansenger callback parse-payload ENCRYPTED_DATA --encoding-key YOUR_KEY
293
+
294
+ # 解密回调数据(仅解密不解析)
295
+ lansenger callback decrypt-payload ENCRYPTED_DATA --encoding-key YOUR_KEY
296
+
297
+ # 验证签名
298
+ lansenger callback verify-signature TIMESTAMP NONCE SIGNATURE --encoding-key YOUR_KEY
299
+ ```
300
+
301
+ ### 媒体文件
302
+
303
+ ```bash
304
+ # 上传文件
305
+ lansenger media upload /path/to/file.pdf --media-type 3
306
+
307
+ # 上传应用/机器人媒体文件
308
+ lansenger media upload-app /path/to/file.pdf --media-type file
309
+
310
+ # 下载媒体文件到本地
311
+ lansenger media download-to-file MEDIA_ID --output /path/to/save.pdf
312
+ ```
313
+
314
+ ### 流式消息
315
+
316
+ ```bash
317
+ # 创建流式消息(用于 AI agent 渐进式输出)
318
+ lansenger streaming create user123 single stream-session-001
319
+
320
+ # 获取流式消息状态
321
+ lansenger streaming fetch MSG_ID
322
+ ```
323
+
324
+ ## 全局选项
325
+
326
+ | 选项 | 说明 |
327
+ |------|------|
328
+ | `--json` / `-j` | 输出原始 JSON 格式而非表格 |
329
+ | `--profile` / `-P` | 指定凭证 profile(默认 `default`) |
330
+
331
+ ```bash
332
+ # JSON 格式输出(便于脚本处理)
333
+ lansenger -j staff basic-info staff001
334
+
335
+ # 使用指定 profile
336
+ lansenger -P my-bot message send-text chat123 "Hello"
337
+ ```
338
+
339
+ ## 多应用/多机器人配置(Profile)
340
+
341
+ CLI 支持多 profile,每个 profile 对应一个 appID(一个应用或一个机器人),凭证互相隔离:
342
+
343
+ ```bash
344
+ # 配置第一个应用(个人机器人)
345
+ lansenger config set app_id xxx1 --profile my-bot
346
+ lansenger config set app_secret xxx1 --profile my-bot
347
+
348
+ # 配置第二个应用(蓝信应用)
349
+ lansenger config set app_id xxx2 --profile my-app
350
+ lansenger config set app_secret xxx2 --profile my-app
351
+ lansenger config set encoding_key yyy2 --profile my-app
352
+ lansenger config set callback_token zzz2 --profile my-app
353
+
354
+ # 切换应用执行命令
355
+ lansenger -P my-bot message send-text chat123 "Hello"
356
+ lansenger -P my-app callback parse-payload DATA
357
+
358
+ # 查看所有已配置 profile
359
+ lansenger config list-profiles
360
+
361
+ # 查看某个 profile 详情
362
+ lansenger config show --profile my-app
363
+ ```
364
+
365
+ ## 凭证安全
366
+
367
+ - 凭证按 profile 隔离存储在 `~/.lansenger/sdk_state.json`,文件权限 0600
368
+ - `config show` 对所有密钥类字段脱敏显示(`***`),仅 `api_gateway_url` 和 `passport_url` 明文展示
369
+ - 支持环境变量 `LANSENGER_APP_ID` / `LANSENGER_APP_SECRET` / `LANSENGER_ENCODING_KEY` / `LANSENGER_CALLBACK_TOKEN`,适合 CI/CD 场景
370
+
371
+ ## CLI 兼容性
372
+
373
+ 本 TS 版 CLI 与 Python 版、Go 版命令语法完全一致:
374
+
375
+ ```bash
376
+ # Python CLI
377
+ pip install lansenger-cli
378
+
379
+ # Go CLI
380
+ go install github.com/lansenger-pm/lansenger-sdk-go/cmd/lansenger@latest
381
+
382
+ # TypeScript CLI
383
+ npm install -g lansenger-cli
384
+
385
+ # 三者使用相同的命令语法
386
+ lansenger message send-text chat123 "Hello"
387
+ ```
388
+
389
+ ## 与 SDK 的关系
390
+
391
+ 本 CLI 基于 [lansenger-sdk-ts](https://github.com/lansenger-pm/lansenger-sdk-ts) 的 `LansengerClient` 实现,覆盖 SDK 全部 API,不修改 SDK 代码。
392
+
393
+ ## 许可证
394
+
395
+ MIT License