|
1 | 1 | import WebSocket from 'ws'; |
2 | 2 | import axios from 'axios'; |
3 | 3 | import EventEmitter from 'events'; |
4 | | -import { GET_TOKEN_URL, GATEWAY_URL,GraphAPIResponse } from './constants'; |
| 4 | +import { TOPIC_ROBOT,GET_TOKEN_URL, GATEWAY_URL,GraphAPIResponse } from './constants.js'; |
5 | 5 |
|
6 | 6 | export enum EventAck { |
7 | 7 | SUCCESS = "SUCCESS", |
@@ -149,49 +149,52 @@ export class DWClient extends EventEmitter { |
149 | 149 | return this; |
150 | 150 | } |
151 | 151 |
|
152 | | - async getEndpoint() { |
153 | | - this.printDebug('get connect endpoint by config'); |
154 | | - this.printDebug(this.config); |
| 152 | + async getAccessToken() { |
155 | 153 | const result = await axios.get( |
156 | 154 | `${GET_TOKEN_URL}?appkey=${this.config.clientId}&appsecret=${this.config.clientSecret}` |
157 | 155 | ); |
158 | 156 | if (result.status === 200 && result.data.access_token) { |
159 | 157 | this.config.access_token = result.data.access_token; |
160 | | - const res = await axios({ |
161 | | - url: GATEWAY_URL, |
162 | | - method: 'POST', |
163 | | - responseType: 'json', |
164 | | - data: { |
165 | | - clientId: this.config.clientId, |
166 | | - clientSecret: this.config.clientSecret, |
167 | | - ua: this.config.ua, |
168 | | - subscriptions: this.config.subscriptions, |
169 | | - }, |
170 | | - headers: { |
171 | | - // 这个接口得加个,否则默认返回的会是xml |
172 | | - Accept: 'application/json', |
173 | | - 'access-token': result.data.access_token, // 'd136e657-5998-4cc4-a055-2b7ceab0f212' |
174 | | - }, |
175 | | - }); |
| 158 | + return result.data.access_token; |
| 159 | + } else { |
| 160 | + throw new Error('getAccessToken: get access_token failed'); |
| 161 | + } |
| 162 | + } |
176 | 163 |
|
177 | | - this.printDebug('res.data ' + JSON.stringify(res.data)); |
178 | | - if (res.data) { |
179 | | - this.config.endpoint = res.data; |
180 | | - const { endpoint, ticket } = res.data; |
181 | | - if (!endpoint || !ticket) { |
182 | | - this.printDebug('endpoint or ticket is null'); |
183 | | - throw new Error('endpoint or ticket is null'); |
184 | | - } |
185 | | - this.dw_url = `${endpoint}?ticket=${ticket}`; |
186 | | - return this; |
187 | | - } else { |
188 | | - throw new Error('build: get endpoint failed'); |
| 164 | + async getEndpoint() { |
| 165 | + this.printDebug('get connect endpoint by config'); |
| 166 | + this.printDebug(this.config); |
| 167 | + const res = await axios({ |
| 168 | + url: GATEWAY_URL, |
| 169 | + method: 'POST', |
| 170 | + responseType: 'json', |
| 171 | + data: { |
| 172 | + clientId: this.config.clientId, |
| 173 | + clientSecret: this.config.clientSecret, |
| 174 | + ua: this.config.ua, |
| 175 | + subscriptions: this.config.subscriptions, |
| 176 | + }, |
| 177 | + headers: { |
| 178 | + // 这个接口得加个,否则默认返回的会是xml |
| 179 | + Accept: 'application/json' |
| 180 | + }, |
| 181 | + }); |
| 182 | + |
| 183 | + this.printDebug('res.data ' + JSON.stringify(res.data)); |
| 184 | + if (res.data) { |
| 185 | + this.config.endpoint = res.data; |
| 186 | + const { endpoint, ticket } = res.data; |
| 187 | + if (!endpoint || !ticket) { |
| 188 | + this.printDebug('endpoint or ticket is null'); |
| 189 | + throw new Error('endpoint or ticket is null'); |
189 | 190 | } |
| 191 | + this.dw_url = `${endpoint}?ticket=${ticket}`; |
| 192 | + return this; |
190 | 193 | } else { |
191 | | - throw new Error('build: get access_token failed'); |
| 194 | + throw new Error('build: get endpoint failed'); |
192 | 195 | } |
193 | 196 | } |
194 | | - |
| 197 | + |
195 | 198 | _connect() { |
196 | 199 | return new Promise<void>((resolve, reject) => { |
197 | 200 | this.userDisconnect = false; |
@@ -279,10 +282,9 @@ export class DWClient extends EventEmitter { |
279 | 282 |
|
280 | 283 | onDownStream(data: string) { |
281 | 284 | this.printDebug('Received message from dingtalk websocket server'); |
282 | | - this.printDebug(data); |
283 | | - // {"specVersion":"1.0","type":"SYSTEM","headers":{"contentType":"application/json","messageId":"c0a800e9168327945646012d43","time":"1683279456460","topic":"disconnect"},"data":"{\"reason\":\"persistent connection is timeout\"}"} |
284 | | - // {"specVersion":"1.0","type":"CALLBACK","headers":{"appId":"9256b875-17e5-46a8-890a-bf4246dc5349","connectionId":"c3faec14-ebe9-11ed-8943-0ec429f1b9a1","contentType":"application/json","messageId":"213f1d00_853_187b7df781f_225d","time":"1683362492940","topic":"bot_got_msg"},"data":"{\"conversationId\":\"cidFbEwwavwcAsXDZbYqSBLnA==\",\"atUsers\":[{\"dingtalkId\":\"$:LWCP_v1:$25jBd/IW606RTMrGnMs9AuLMeuAztDrv\"}],\"chatbotCorpId\":\"ding9f50b15bccd16741\",\"chatbotUserId\":\"$:LWCP_v1:$25jBd/IW606RTMrGnMs9AuLMeuAztDrv\",\"msgId\":\"msgqEufncv9gVqy7ia60LYs3w==\",\"senderNick\":\"骏隆(主用钉)\",\"isAdmin\":true,\"senderStaffId\":\"01426861-1254332033\",\"sessionWebhookExpiredTime\":1683363692884,\"createAt\":1683362491872,\"senderCorpId\":\"ding9f50b15bccd16741\",\"conversationType\":\"2\",\"senderId\":\"$:LWCP_v1:$+PxJZVhRkpC139mPH6L7aw==\",\"conversationTitle\":\"机器人长链接事件测试群\",\"isInAtList\":true,\"sessionWebhook\":\"https://oapi.dingtalk.com/robot/sendBySession?session=2664f1467475bd90fcba36234c735997\",\"text\":{\"content\":\" ss\"},\"robotCode\":\"dingphtembyvlbeq2y4d\",\"msgtype\":\"text\"}"} |
| 285 | + |
285 | 286 | const msg = JSON.parse(data) as DWClientDownStream; |
| 287 | + this.printDebug(msg); |
286 | 288 | switch (msg.type) { |
287 | 289 | case 'SYSTEM': |
288 | 290 | this.onSystem(msg); |
@@ -369,6 +371,22 @@ export class DWClient extends EventEmitter { |
369 | 371 | this.socket?.send(JSON.stringify(msg)); |
370 | 372 | } |
371 | 373 |
|
| 374 | + /** |
| 375 | + * 消息响应,避免服务端重试. |
| 376 | + * stream模式下,服务端推送消息到client后,会监听client响应,如果消息长时间未响应会在一定时间内(60s)重试推消息,可以通过此方法返回消息响应,避免多次接收服务端消息。 |
| 377 | + * @param messageId |
| 378 | + * @param result |
| 379 | + * @returns |
| 380 | + * @memberof DWClient |
| 381 | + * @example |
| 382 | + * ```javascript |
| 383 | + * client.socketResponse(res.headers.messageId, result.data); |
| 384 | + * ``` |
| 385 | + */ |
| 386 | + socketCallBackResponse(messageId: string, result: any) { |
| 387 | + this.send(messageId, {response : result}); |
| 388 | + } |
| 389 | + |
372 | 390 | sendGraphAPIResponse(messageId: string, value: GraphAPIResponse) { |
373 | 391 | if (!messageId) { |
374 | 392 | console.error('send: messageId must be defined'); |
|
0 commit comments