|
1 | | -# dingtalk-stream |
2 | | -Nodejs SDK for DingTalk Stream Mode API, Compared with the webhook mode, it is easier to access the DingTalk chatbot |
3 | | -钉钉支持 Stream 模式接入事件推送、机器人收消息以及卡片回调,该 SDK 实现了 Stream 模式。相比 Webhook 模式,Stream 模式可以更简单的接入各类事件和回调。 |
4 | | - |
5 | | -## 快速开始 |
6 | | -npm ii |
7 | | -npm start |
8 | | - |
9 | | -### 准备工作 |
10 | | - |
11 | | -* Nodejs 开发环境,https://midwayjs.org/docs/how_to_install_nodejs |
12 | | -* 需要Node version >= 18.17.1 |
13 | | -* 钉钉开发者账号,具备创建企业内部应用的权限,详见[成为钉钉开发者](https://open.dingtalk.com/document/orgapp/become-a-dingtalk-developer) |
14 | | - |
15 | | -### 快速开始指南 |
16 | | - |
17 | | -1、创建企业内部应用 |
18 | | - |
19 | | -进入[钉钉开发者后台](https://open-dev.dingtalk.com/),创建企业内部应用,获取ClientID(即 AppKey)和ClientSecret( 即AppSecret)。 |
20 | | - |
21 | | -发布应用:在开发者后台左侧导航中,点击“版本管理与发布”,点击“确认发布”,并在接下来的可见范围设置中,选择“全部员工”,或者按需选择部分员工。 |
22 | | - |
23 | | - |
24 | | -2、Stream 模式的机器人(可选) |
25 | | - |
26 | | -如果不需要使用机器人功能的话,可以不用创建。 |
27 | | - |
28 | | -在应用管理的左侧导航中,选择“消息推送”,打开机器人能力,设置机器人基本信息。 |
29 | | - |
30 | | -注意:消息接收模式中,选择 “Stream 模式” |
| 1 | +# DingTalk Stream Mode 介绍 |
31 | 2 |
|
32 | | - |
| 3 | +Node.js SDK for DingTalk Stream Mode API, Compared with the webhook mode, it is easier to access the DingTalk chatbot |
33 | 4 |
|
34 | | -点击“点击调试”按钮,可以创建测试群进行测试。 |
35 | | - |
36 | | -3、使用demo项目测试,启动服务: |
37 | | - |
38 | | -a、获取demo项目 |
39 | | -```Shell |
40 | | - git clone [email protected]:open-dingtalk/dingtalk-stream-sdk-nodejs.git |
41 | | -``` |
42 | | -b、在example/config.json里配置应用信息。 |
43 | | - |
44 | | -c、启动测试case |
45 | | -```Shell |
46 | | -cd dingtalk-stream |
47 | | -yarn |
48 | | -npm run build |
49 | | -npm start |
50 | | -``` |
51 | | - |
52 | | -4、在项目中引用sdk,安装 dingtalk-stream |
53 | | - |
54 | | -```Shell |
55 | | -npm i dingtalk-stream |
56 | | -``` |
57 | | - |
58 | | -代码中使用 |
59 | | -```javascript |
60 | | -const DWClient = require("dingtalk-stream"); |
61 | | -const config = require("./config.json"); |
62 | | - |
63 | | -const client = new DWClient({ |
64 | | - clientId: config.clientId, |
65 | | - clientSecret: config.clientSecret, |
66 | | - debug: true // 开启调试信息,默认关闭 |
67 | | -}); |
68 | | -client.registerCallbackListener('/v1.0/im/bot/messages/get', async (res) => { |
69 | | - // 注册机器人回调事件 |
70 | | - console.log("收到消息"); |
71 | | - const {messageId} = res.headers; |
72 | | - const { text, senderStaffId, sessionWebhook } = JSON.parse(res.data); |
73 | | - }) |
74 | | - .registerCallbackListener( |
75 | | - '/v1.0/graph/api/invoke', |
76 | | - async (res: DWClientDownStream) => { |
77 | | - // 注册AI插件回调事件 |
78 | | - console.log("收到ai消息"); |
79 | | - const { messageId } = res.headers; |
80 | | - |
81 | | - // 添加业务逻辑 |
82 | | - console.log(res); |
83 | | - console.log(JSON.parse(res.data)); |
84 | | - |
85 | | - // 通过Stream返回数据 |
86 | | - client.sendGraphAPIResponse(messageId, { |
87 | | - response: { |
88 | | - statusLine: { |
89 | | - code: 200, |
90 | | - reasonPhrase: "OK", |
91 | | - }, |
92 | | - headers: {}, |
93 | | - body: JSON.stringify({ |
94 | | - text: "你好", |
95 | | - }), |
96 | | - }, |
97 | | - }); |
98 | | - } |
99 | | - ) |
100 | | - .connect(); |
101 | | -``` |
102 | | - |
103 | | -### 事件订阅切换到 Stream 模式(可选) |
104 | | - |
105 | | -进入钉钉开发者后台,选择企业内部应用,在应用管理的左侧导航中,选择“事件与回调”。 |
106 | | -“订阅管理”中,“推送方式”选项中,选择 “Stream 模式”,并保存 |
| 5 | +钉钉支持 Stream 模式接入事件推送、机器人收消息以及卡片回调,该 SDK 实现了 Stream 模式。相比 Webhook 模式,Stream 模式可以更简单的接入各类事件和回调。 |
107 | 6 |
|
108 | | -### AI 应用 - 技能插件,Graph API 类型插件使用 (可选) |
| 7 | +## 开发教程 |
109 | 8 |
|
110 | | -配置好魔法棒应用,并在技能中使用了Graph API类型插件。 |
| 9 | +在 [教程文档](https://opensource.dingtalk.com/developerpedia/docs/explore/tutorials/stream/overview) 中,你可以找到钉钉 Stream 模式的教程文档和示例代码。 |
111 | 10 |
|
112 | | -### 技术支持 |
| 11 | +### 参考资料 |
113 | 12 |
|
114 | | -[点击链接,加入Stream模式共创群交流](https://open-dingtalk.github.io/developerpedia/docs/explore/support/?via=moon-group) |
| 13 | +* [Stream 模式说明](https://opensource.dingtalk.com/developerpedia/docs/learn/stream/overview) |
| 14 | +* [教程文档](https://opensource.dingtalk.com/developerpedia/docs/explore/tutorials/stream/overview) |
| 15 | +* [常见问题](https://opensource.dingtalk.com/developerpedia/docs/learn/stream/faq) |
| 16 | +* [Stream 模式共创群](https://opensource.dingtalk.com/developerpedia/docs/explore/support/?via=moon-group) |
0 commit comments