11const mime = require ( 'mime-types' )
2+ const _ = require ( 'lodash' )
23const { DirectLine, ConnectionStatus } = require ( 'botframework-directlinejs' )
34const debug = require ( 'debug' ) ( 'botium-connector-directline3' )
45
@@ -60,13 +61,13 @@ class BotiumConnectorDirectline3 {
6061
6162 const mapButton = ( b ) => ( {
6263 text : b . title ,
63- payload : b . value ,
64- imageUri : b . image
64+ payload : b . value || b . url || b . data ,
65+ imageUri : b . image || b . iconUrl
6566 } )
6667 const mapImage = ( i ) => ( {
6768 mediaUri : i . url ,
6869 mimeType : mime . lookup ( i . url ) || 'application/unknown' ,
69- altText : i . alt
70+ altText : i . alt || i . altText
7071 } )
7172 const mapMedia = ( m ) => ( {
7273 mediaUri : m . url ,
@@ -78,11 +79,18 @@ class BotiumConnectorDirectline3 {
7879 if ( a . contentType === 'application/vnd.microsoft.card.hero' ) {
7980 botMsg . cards . push ( {
8081 text : a . content . title ,
81- image : a . content . images && a . content . images . length > 0 && [ mapImage ( a . content . images [ 0 ] ) ] ,
82+ image : a . content . images && a . content . images . length > 0 && mapImage ( a . content . images [ 0 ] ) ,
8283 buttons : a . content . buttons && a . content . buttons . map ( mapButton )
8384 } )
8485 } else if ( a . contentType === 'application/vnd.microsoft.card.adaptive' ) {
85- // TODO
86+ const textBlocks = this . _deepFilter ( a . content . body , ( t ) => t . type , ( t ) => t . type === 'TextBlock' )
87+ const imageBlocks = this . _deepFilter ( a . content . body , ( t ) => t . type , ( t ) => t . type === 'Image' )
88+
89+ botMsg . cards . push ( {
90+ text : textBlocks && textBlocks . map ( t => t . text ) ,
91+ image : imageBlocks && imageBlocks . length > 0 && mapImage ( imageBlocks [ 0 ] ) ,
92+ buttons : a . content . actions && a . content . actions . map ( mapButton )
93+ } )
8694 } else if ( a . contentType === 'application/vnd.microsoft.card.animation' ||
8795 a . contentType === 'application/vnd.microsoft.card.audio' ||
8896 a . contentType === 'application/vnd.microsoft.card.video' ) {
@@ -175,6 +183,24 @@ class BotiumConnectorDirectline3 {
175183 this . connSubscription = null
176184 }
177185 }
186+
187+ _deepFilter ( item , selectFn , filterFn ) {
188+ let result = [ ]
189+ if ( _ . isArray ( item ) ) {
190+ item . filter ( selectFn ) . forEach ( subItem => {
191+ result = result . concat ( this . _deepFilter ( subItem , selectFn , filterFn ) )
192+ } )
193+ } else if ( selectFn ( item ) ) {
194+ if ( filterFn ( item ) ) {
195+ result . push ( item )
196+ } else {
197+ Object . getOwnPropertyNames ( item ) . forEach ( key => {
198+ result = result . concat ( this . _deepFilter ( item [ key ] , selectFn , filterFn ) )
199+ } )
200+ }
201+ }
202+ return result
203+ }
178204}
179205
180206module . exports = {
0 commit comments