|
| 1 | +const DirectLine = require('botframework-directlinejs').DirectLine |
1 | 2 | const debug = require('debug')('botium-connector-directline3') |
2 | 3 |
|
3 | | -global.XMLHttpRequest = require('xhr2'); |
| 4 | +global.XMLHttpRequest = require('xhr2') |
4 | 5 |
|
5 | 6 | class BotiumConnectorDirectline3 { |
6 | 7 | constructor ({ queueBotSays, caps }) { |
7 | | - this.queueBotSays = queueBotSays |
8 | | - this.caps = caps |
| 8 | + this.queueBotSays = queueBotSays |
| 9 | + this.caps = caps |
9 | 10 | } |
10 | 11 |
|
11 | 12 | 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() |
14 | 17 | } |
15 | 18 |
|
16 | 19 | 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() |
19 | 27 | } |
20 | 28 |
|
21 | 29 | 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() |
24 | 41 | } |
25 | 42 |
|
26 | 43 | 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 | + }) |
30 | 61 | } |
31 | 62 |
|
32 | 63 | 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() |
35 | 71 | } |
36 | 72 |
|
37 | 73 | Clean () { |
38 | | - debug('Clean called') |
39 | | - return Promise.resolve() |
| 74 | + debug('Clean called') |
| 75 | + return Promise.resolve() |
40 | 76 | } |
41 | 77 | } |
42 | 78 |
|
|
0 commit comments