1+ const uuidv4 = require ( 'uuid/v4' )
12const mime = require ( 'mime-types' )
23const _ = require ( 'lodash' )
34const { DirectLine, ConnectionStatus } = require ( 'botframework-directlinejs' )
@@ -9,13 +10,15 @@ const Capabilities = {
910 DIRECTLINE3_SECRET : 'DIRECTLINE3_SECRET' ,
1011 DIRECTLINE3_WEBSOCKET : 'DIRECTLINE3_WEBSOCKET' ,
1112 DIRECTLINE3_POLLINGINTERVAL : 'DIRECTLINE3_POLLINGINTERVAL' ,
13+ DIRECTLINE3_GENERATE_USERNAME : 'DIRECTLINE3_GENERATE_USERNAME' ,
1214 DIRECTLINE3_BUTTON_TYPE : 'DIRECTLINE3_BUTTON_TYPE' ,
1315 DIRECTLINE3_BUTTON_VALUE_FIELD : 'DIRECTLINE3_BUTTON_VALUE_FIELD'
1416}
1517
1618const Defaults = {
1719 [ Capabilities . DIRECTLINE3_WEBSOCKET ] : true ,
1820 [ Capabilities . DIRECTLINE3_POLLINGINTERVAL ] : 1000 ,
21+ [ Capabilities . DIRECTLINE3_GENERATE_USERNAME ] : false ,
1922 [ Capabilities . DIRECTLINE3_BUTTON_TYPE ] : 'event' ,
2023 [ Capabilities . DIRECTLINE3_BUTTON_VALUE_FIELD ] : 'name'
2124}
@@ -51,9 +54,15 @@ class BotiumConnectorDirectline3 {
5154 pollingInterval : this . caps [ 'DIRECTLINE3_POLLINGINTERVAL' ]
5255 } )
5356
57+ if ( this . caps [ 'DIRECTLINE3_GENERATE_USERNAME' ] ) {
58+ this . me = uuidv4 ( )
59+ } else {
60+ this . me = 'me'
61+ }
62+
5463 this . receivedMessageIds = { }
5564 this . subscription = this . directLine . activity$
56- . filter ( activity => activity . type === 'message' && activity . from . id !== 'me' )
65+ . filter ( activity => activity . type === 'message' && activity . from . id !== this . me )
5766 . subscribe (
5867 message => {
5968 if ( this . receivedMessageIds [ message . id ] ) {
@@ -84,8 +93,11 @@ class BotiumConnectorDirectline3 {
8493 if ( a . contentType === 'application/vnd.microsoft.card.hero' ) {
8594 botMsg . cards . push ( {
8695 text : a . content . title || a . content . text ,
96+ subtext : a . content . subtitle ,
97+ content : a . content . text ,
8798 image : a . content . images && a . content . images . length > 0 && mapImage ( a . content . images [ 0 ] ) ,
88- buttons : a . content . buttons && a . content . buttons . map ( mapButton )
99+ buttons : a . content . buttons && a . content . buttons . map ( mapButton ) ,
100+ media : a . content . images && a . content . images . map ( mapImage )
89101 } )
90102 } else if ( a . contentType === 'application/vnd.microsoft.card.adaptive' ) {
91103 const textBlocks = this . _deepFilter ( a . content . body , ( t ) => t . type , ( t ) => t . type === 'TextBlock' )
@@ -99,11 +111,23 @@ class BotiumConnectorDirectline3 {
99111 } else if ( a . contentType === 'application/vnd.microsoft.card.animation' ||
100112 a . contentType === 'application/vnd.microsoft.card.audio' ||
101113 a . contentType === 'application/vnd.microsoft.card.video' ) {
102- botMsg . media = botMsg . media . concat ( ( a . content . media && a . content . media . map ( mapMedia ) ) || [ ] )
103- botMsg . buttons = botMsg . buttons . concat ( ( a . content . buttons && a . content . buttons . map ( mapButton ) ) || [ ] )
114+ botMsg . cards . push ( {
115+ text : a . content . title || a . content . text ,
116+ subtext : a . content . subtitle ,
117+ content : a . content . text ,
118+ image : a . content . image && mapImage ( a . content . image ) ,
119+ buttons : a . content . buttons && a . content . buttons . map ( mapButton ) ,
120+ media : a . content . media && a . content . media . map ( mapMedia )
121+ } )
104122 } else if ( a . contentType === 'application/vnd.microsoft.card.thumbnail' ) {
105- botMsg . media = botMsg . media . concat ( ( a . content . images && a . content . images . map ( mapImage ) ) || [ ] )
106- botMsg . buttons = botMsg . buttons . concat ( ( a . content . buttons && a . content . buttons . map ( mapButton ) ) || [ ] )
123+ botMsg . cards . push ( {
124+ text : a . content . title || a . content . text ,
125+ subtext : a . content . subtitle ,
126+ content : a . content . text ,
127+ image : a . content . images && a . content . images . length > 0 && mapImage ( a . content . images [ 0 ] ) ,
128+ buttons : a . content . buttons && a . content . buttons . map ( mapButton ) ,
129+ media : a . content . images && a . content . images . map ( mapImage )
130+ } )
107131 } else if ( a . contentType && a . contentUrl ) {
108132 botMsg . media . push ( {
109133 mediaUri : a . contentUrl ,
@@ -167,11 +191,16 @@ class BotiumConnectorDirectline3 {
167191 debug ( 'UserSays called' )
168192 return new Promise ( ( resolve , reject ) => {
169193 const activity = {
170- from : { id : msg . sender }
194+ from : { id : this . me }
171195 }
172- if ( msg . buttons && msg . buttons . length > 0 && msg . buttons [ 0 ] . text ) {
196+ if ( msg . buttons && msg . buttons . length > 0 && ( msg . buttons [ 0 ] . text || msg . buttons [ 0 ] . payload ) ) {
197+ let payload = msg . buttons [ 0 ] . payload || msg . buttons [ 0 ] . text
198+ try {
199+ payload = JSON . parse ( payload )
200+ } catch ( err ) {
201+ }
173202 activity . type = this . caps [ Capabilities . DIRECTLINE3_BUTTON_TYPE ]
174- activity [ this . caps [ Capabilities . DIRECTLINE3_BUTTON_VALUE_FIELD ] ] = msg . buttons [ 0 ] . text
203+ activity [ this . caps [ Capabilities . DIRECTLINE3_BUTTON_VALUE_FIELD ] ] = payload
175204 } else {
176205 activity . type = 'message'
177206 activity . text = msg . messageText
@@ -188,7 +217,7 @@ class BotiumConnectorDirectline3 {
188217 } ,
189218 err => {
190219 debug ( 'Error posting activity' , err )
191- reject ( err )
220+ reject ( new Error ( `Error posting activity: ${ err } ` ) )
192221 }
193222 )
194223 } )
0 commit comments