Skip to content

Commit 7613cf2

Browse files
author
Botium
authored
Merge pull request #48 from codeforequity-at/develop
Version 0.0.25
2 parents e73ae90 + 37e1960 commit 7613cf2

File tree

3 files changed

+84
-50
lines changed

3 files changed

+84
-50
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ If not using the default Direct Line endpoint, e.g. if you are using a region-sp
128128
_true or false_
129129
Wether to use Websocket connection or HTTP polling. Usually, using Websockets is prefered, but sometimes this is not possible (maybe with blocking proxies).
130130

131-
_Currently, only HTTP polling supported (Bot Framework restriction)_
132-
133131
### DIRECTLINE3_POLLINGINTERVAL
134132
_in milliseconds_
135133
HTTP Polling interval
@@ -201,8 +199,3 @@ Send some activity upfront to start a session. Can be used to simulate the webch
201199
}
202200
},
203201
...
204-
205-
# Current Restrictions
206-
207-
* Only HTTP Polling supported (WebSocket not available in Node.js)
208-
* Typing indicator is not transmitted over HTTP Polling (Bot Framework restriction)

index.js

Lines changed: 83 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const fetch = require('node-fetch')
88
const fs = require('fs')
99
const path = require('path')
1010
const xhr2 = require('xhr2')
11+
const ws = require('ws')
1112

1213
const Capabilities = {
1314
DIRECTLINE3_SECRET: 'DIRECTLINE3_SECRET',
@@ -74,6 +75,7 @@ class BotiumConnectorDirectline3 {
7475
async Start () {
7576
debug('Start called')
7677
global.XMLHttpRequest = xhr2
78+
global.WebSocket = ws
7779
if (debug.enabled) {
7880
global.window = Object.assign(global.window || {}, { botchatDebug: true })
7981
}
@@ -263,47 +265,77 @@ class BotiumConnectorDirectline3 {
263265
// give userSays some time
264266
setTimeout(() => this.queueBotSays(botMsg), 200)
265267
}
268+
},
269+
err => {
270+
debug(`Error in waiting for activities: ${err.message}`)
266271
}
267272
)
273+
274+
let resultResolve = null
275+
let resultReject = null
276+
const resultPromise = new Promise((resolve, reject) => {
277+
resultResolve = resolve
278+
resultReject = reject
279+
})
280+
268281
this.connSubscription = this.directLine.connectionStatus$
269-
.subscribe(connectionStatus => {
270-
switch (connectionStatus) {
271-
case ConnectionStatus.Uninitialized:
272-
debug(`Directline Connection Status: ${connectionStatus} / Uninitialized`)
273-
break
274-
case ConnectionStatus.Connecting:
275-
debug(`Directline Connection Status: ${connectionStatus} / Connecting`)
276-
break
277-
case ConnectionStatus.Online:
278-
debug(`Directline Connection Status: ${connectionStatus} / Online`)
279-
if (this.caps[Capabilities.DIRECTLINE3_WELCOME_ACTIVITY]) {
280-
if (_.isString(this.caps[Capabilities.DIRECTLINE3_WELCOME_ACTIVITY])) {
281-
let initActivity = null
282-
try {
283-
initActivity = JSON.parse(this.caps[Capabilities.DIRECTLINE3_WELCOME_ACTIVITY])
284-
} catch (err) {
285-
}
286-
if (initActivity) {
287-
this.UserSays({ sourceData: initActivity }).catch(err => debug(`Failed to send DIRECTLINE3_WELCOME_ACTIVITY: ${err.message}`))
288-
} else {
289-
this.UserSays({ messageText: this.caps[Capabilities.DIRECTLINE3_WELCOME_ACTIVITY] }).catch(err => debug(`Failed to send DIRECTLINE3_WELCOME_ACTIVITY: ${err.message}`))
290-
}
291-
} else {
292-
this.UserSays({ sourceData: this.caps[Capabilities.DIRECTLINE3_WELCOME_ACTIVITY] }).catch(err => debug(`Failed to send DIRECTLINE3_WELCOME_ACTIVITY: ${err.message}`))
293-
}
294-
}
295-
break
296-
case ConnectionStatus.ExpiredToken:
297-
debug(`Directline Connection Status: ${connectionStatus} / ExpiredToken`)
298-
break
299-
case ConnectionStatus.FailedToConnect:
300-
debug(`Directline Connection Status: ${connectionStatus} / FailedToConnect`)
301-
break
302-
case ConnectionStatus.Ended:
303-
debug(`Directline Connection Status: ${connectionStatus} / Ended`)
304-
break
282+
.subscribe(
283+
connectionStatus => {
284+
switch (connectionStatus) {
285+
case ConnectionStatus.Uninitialized:
286+
debug(`Directline Connection Status: ${connectionStatus} / Uninitialized`)
287+
break
288+
case ConnectionStatus.Connecting:
289+
debug(`Directline Connection Status: ${connectionStatus} / Connecting`)
290+
break
291+
case ConnectionStatus.Online:
292+
debug(`Directline Connection Status: ${connectionStatus} / Online`)
293+
resultResolve && resultResolve()
294+
resultResolve = null
295+
break
296+
case ConnectionStatus.ExpiredToken:
297+
debug(`Directline Connection Status: ${connectionStatus} / ExpiredToken`)
298+
resultReject && resultReject(new Error('Directline token expired'))
299+
resultReject = null
300+
break
301+
case ConnectionStatus.FailedToConnect:
302+
debug(`Directline Connection Status: ${connectionStatus} / FailedToConnect`)
303+
resultReject && resultReject(new Error('Directline failed to connect - check the Directline Secret'))
304+
resultReject = null
305+
break
306+
case ConnectionStatus.Ended:
307+
debug(`Directline Connection Status: ${connectionStatus} / Ended`)
308+
break
309+
}
310+
},
311+
err => {
312+
debug(`Error in waiting for connectionStatus: ${err.message}`)
305313
}
306-
})
314+
)
315+
316+
if (this.caps[Capabilities.DIRECTLINE3_WELCOME_ACTIVITY]) {
317+
if (_.isString(this.caps[Capabilities.DIRECTLINE3_WELCOME_ACTIVITY])) {
318+
let initActivity = null
319+
try {
320+
initActivity = JSON.parse(this.caps[Capabilities.DIRECTLINE3_WELCOME_ACTIVITY])
321+
} catch (err) {
322+
}
323+
if (initActivity) {
324+
this.UserSays({ sourceData: initActivity }).catch(err => debug(`Failed to send DIRECTLINE3_WELCOME_ACTIVITY: ${err.message}`))
325+
} else {
326+
this.UserSays({ messageText: this.caps[Capabilities.DIRECTLINE3_WELCOME_ACTIVITY] }).catch(err => debug(`Failed to send DIRECTLINE3_WELCOME_ACTIVITY: ${err.message}`))
327+
}
328+
} else {
329+
this.UserSays({ sourceData: this.caps[Capabilities.DIRECTLINE3_WELCOME_ACTIVITY] }).catch(err => debug(`Failed to send DIRECTLINE3_WELCOME_ACTIVITY: ${err.message}`))
330+
}
331+
} else {
332+
this.directLine.getSessionId().subscribe(
333+
() => {},
334+
() => {}
335+
)
336+
}
337+
338+
return resultPromise
307339
}
308340

309341
UserSays (msg) {
@@ -459,14 +491,22 @@ class BotiumConnectorDirectline3 {
459491

460492
_stopSubscription () {
461493
if (this.subscription) {
462-
debug('unsubscribing from directline activity subscription')
463-
this.subscription.unsubscribe()
464-
this.subscription = null
494+
try {
495+
this.subscription.unsubscribe()
496+
this.subscription = null
497+
debug('unsubscribed from directline activity subscription')
498+
} catch (err) {
499+
debug(`unsubscribing from directline activity subscription failed: ${err.message}`)
500+
}
465501
}
466502
if (this.connSubscription) {
467-
debug('unsubscribing from directline connectionstatus subscription')
468-
this.connSubscription.unsubscribe()
469-
this.connSubscription = null
503+
try {
504+
this.connSubscription.unsubscribe()
505+
this.connSubscription = null
506+
debug('unsubscribing from directline connectionstatus subscription')
507+
} catch (err) {
508+
debug(`unsubscribing from directline connectionstatus subscription failed: ${err.message}`)
509+
}
470510
}
471511
if (this.directLine) {
472512
debug('ending directline connection')

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"mime-types": "^2.1.26",
4646
"node-fetch": "^2.6.0",
4747
"uuid": "^7.0.2",
48+
"ws": "^7.3.1",
4849
"xhr2": "^0.2.0"
4950
},
5051
"peerDependencies": {

0 commit comments

Comments
 (0)