@@ -22,6 +22,7 @@ const ERR_CONV_ID_REQ = '`conversationId` is required and must be valid'
22
22
const ERR_BAD_LANGUAGE = '`language` is required and must be valid'
23
23
const ERR_BAD_CONV_ID = "The conversation ID doesn't belong to that user"
24
24
const ERR_BAD_USER_SESSION_ID = 'session id is invalid'
25
+ const MODULE_NAME = 'channel-web'
25
26
26
27
const USER_ID_MAX_LENGTH = 40
27
28
const MAX_MESSAGE_HISTORY = 100
@@ -257,20 +258,58 @@ export default async (bp: typeof sdk, db: Database) => {
257
258
bp . http . extractExternalToken ,
258
259
assertUserInfo ( { convoIdRequired : true } ) ,
259
260
asyncMiddleware ( async ( req : ChatRequest & any , res : Response ) => {
260
- const { botId, userId } = req
261
+ const { botId, userId, conversationId } = req
261
262
const payloadValue = req . body . payload || { }
263
+ const config : Config = await bp . config . getModuleConfigForBot ( MODULE_NAME , botId )
262
264
263
265
await bp . users . getOrCreateUser ( 'web' , userId , botId ) // Just to create the user if it doesn't exist
264
266
265
- const payload = {
266
- text : `Uploaded a file ** ${ req . file . originalname } **` ,
267
- type : 'file' ,
267
+ let text = `Uploaded a file ** ${ req . file . originalname } **`
268
+
269
+ const variables = {
268
270
storage : req . file . location ? 's3' : 'local' ,
269
271
url : req . file . location || req . file . path || undefined ,
270
272
name : req . file . filename ,
271
273
originalName : req . file . originalname ,
272
274
mime : req . file . contentType || req . file . mimetype ,
273
- size : req . file . size ,
275
+ size : req . file . size
276
+ }
277
+
278
+ if ( config . uploadsFileUploadedTextContentElement ?. length ) {
279
+ try {
280
+ if ( ! config . uploadsFileUploadedTextContentElement . startsWith ( '#!builtin_text' ) ) {
281
+ throw new Error ( 'Only builtin_text elements are supported, use #!builtin_text-<elementId>' )
282
+ }
283
+
284
+ const userState = await bp . users . getAttributes ( 'web' , userId )
285
+
286
+ const rendered = ( await bp . cms . renderElement (
287
+ config . uploadsFileUploadedTextContentElement ,
288
+ {
289
+ ...variables ,
290
+ event : {
291
+ state : {
292
+ user : userState
293
+ }
294
+ }
295
+ } ,
296
+ {
297
+ botId,
298
+ channel : 'web' ,
299
+ target : userId ,
300
+ threadId : conversationId
301
+ }
302
+ ) ) as { text : string } [ ]
303
+ text = rendered [ 0 ] . text
304
+ } catch ( err ) {
305
+ bp . logger . forBot ( botId ) . error ( `Error while rendering uploadsFileUploadedTextContentElement: ${ err . message } ` )
306
+ }
307
+ }
308
+
309
+ const payload = {
310
+ text,
311
+ type : 'file' ,
312
+ ...variables ,
274
313
payload : payloadValue
275
314
}
276
315
0 commit comments