Skip to content

Commit cb7bb26

Browse files
author
treml
committed
Directline3 API integration
1 parent fb02949 commit cb7bb26

File tree

6 files changed

+3351
-4235
lines changed

6 files changed

+3351
-4235
lines changed

index.js

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,78 @@
1+
const DirectLine = require('botframework-directlinejs').DirectLine
12
const debug = require('debug')('botium-connector-directline3')
23

3-
global.XMLHttpRequest = require('xhr2');
4+
global.XMLHttpRequest = require('xhr2')
45

56
class BotiumConnectorDirectline3 {
67
constructor ({ queueBotSays, caps }) {
7-
this.queueBotSays = queueBotSays
8-
this.caps = caps
8+
this.queueBotSays = queueBotSays
9+
this.caps = caps
910
}
1011

1112
Validate () {
12-
debug('Validate called')
13-
return Promise.resolve()
13+
debug('Validate called')
14+
if (!this.caps['DIRECTLINE3_SECRET']) throw new Error('DIRECTLINE3_SECRET capability required')
15+
16+
return Promise.resolve()
1417
}
1518

1619
Build () {
17-
debug('Build called')
18-
return Promise.resolve()
20+
debug('Build called')
21+
this.directLine = new DirectLine({
22+
secret: this.caps['DIRECTLINE3_SECRET'],
23+
webSocket: this.caps['DIRECTLINE3_WEBSOCKET'],
24+
pollingInterval: this.caps['DIRECTLINE3_POLLINGINTERVAL']
25+
})
26+
return Promise.resolve()
1927
}
2028

2129
Start () {
22-
debug('Start called')
23-
return Promise.resolve()
30+
debug('Start called')
31+
this.subscription = this.directLine.activity$
32+
.filter(activity => activity.type === 'message' && activity.from.id !== 'me')
33+
.subscribe(
34+
message => {
35+
debug('received message ', message)
36+
const botMsg = { sender: 'bot', sourceData: message, messageText: message.text }
37+
this.queueBotSays(botMsg)
38+
}
39+
)
40+
return Promise.resolve()
2441
}
2542

2643
UserSays (msg) {
27-
debug('UserSays called, echo back')
28-
const botMsg = { sender: 'bot', sourceData: msg.sourceData, messageText: msg.messageText }
29-
this.queueBotSays(msg)
44+
debug('UserSays called')
45+
return new Promise((resolve, reject) => {
46+
this.directLine.postActivity({
47+
from: { id: msg.sender },
48+
type: 'message',
49+
text: msg.messageText
50+
}).subscribe(
51+
id => {
52+
debug('Posted activity, assigned ID ', id)
53+
resolve()
54+
},
55+
err => {
56+
debug('Error posting activity', err)
57+
reject(err)
58+
}
59+
)
60+
})
3061
}
3162

3263
Stop () {
33-
debug('Stop called')
34-
return Promise.resolve()
64+
debug('Stop called')
65+
if (this.subscription) {
66+
this.subscription.unsubscribe()
67+
this.subscription = null
68+
}
69+
70+
return Promise.resolve()
3571
}
3672

3773
Clean () {
38-
debug('Clean called')
39-
return Promise.resolve()
74+
debug('Clean called')
75+
return Promise.resolve()
4076
}
4177
}
4278

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"module": "dist/botium-connector-directline3-es.js",
77
"scripts": {
88
"build": "npm run eslint && rollup -c",
9-
"eslint": "eslint \"./src/**/*.js\" \"./test/**/*.js\""
9+
"eslint": "eslint index.js"
1010
},
1111
"repository": {
1212
"type": "git",

samples/botium.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"botium": {
33
"Capabilities": {
44
"PROJECTNAME": "Directline3 Plugin Sample",
5-
"CONTAINERMODE": "directline3"
5+
"CONTAINERMODE": "directline3",
6+
"DIRECTLINE3_SECRET": "7O48RCUWWTI.cwA.A9w.XyviXjUVJooJwI6I6zabVQaEAoRK_TIPx50dUazzeE8",
7+
"DIRECTLINE3_WEBSOCKET": true,
8+
"DIRECTLINE3_POLLINGINTERVAL": 1000
69
}
710
}
811
}

samples/botiumFluent.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const driver = new BotDriver()
44

55
driver.BuildFluent()
66
.Start()
7-
.UserSaysText('hello')
7+
.WaitBotSaysText(console.log)
8+
.UserSaysText('Show me a hero card')
89
.WaitBotSaysText(console.log)
910
.UserSaysText('book a room')
1011
.WaitBotSaysText(console.log)

0 commit comments

Comments
 (0)