Skip to content

Commit 902f02d

Browse files
authored
feat(channel-web): allow usage of contentElement for fileUpload message (#1769)
* feat(channel-web): allow usage of contentElement for fileUpload message * check empty array * type
1 parent 58a8ea9 commit 902f02d

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

modules/channel-web/src/backend/api.ts

+44-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const ERR_CONV_ID_REQ = '`conversationId` is required and must be valid'
2222
const ERR_BAD_LANGUAGE = '`language` is required and must be valid'
2323
const ERR_BAD_CONV_ID = "The conversation ID doesn't belong to that user"
2424
const ERR_BAD_USER_SESSION_ID = 'session id is invalid'
25+
const MODULE_NAME = 'channel-web'
2526

2627
const USER_ID_MAX_LENGTH = 40
2728
const MAX_MESSAGE_HISTORY = 100
@@ -257,20 +258,58 @@ export default async (bp: typeof sdk, db: Database) => {
257258
bp.http.extractExternalToken,
258259
assertUserInfo({ convoIdRequired: true }),
259260
asyncMiddleware(async (req: ChatRequest & any, res: Response) => {
260-
const { botId, userId } = req
261+
const { botId, userId, conversationId } = req
261262
const payloadValue = req.body.payload || {}
263+
const config: Config = await bp.config.getModuleConfigForBot(MODULE_NAME, botId)
262264

263265
await bp.users.getOrCreateUser('web', userId, botId) // Just to create the user if it doesn't exist
264266

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 = {
268270
storage: req.file.location ? 's3' : 'local',
269271
url: req.file.location || req.file.path || undefined,
270272
name: req.file.filename,
271273
originalName: req.file.originalname,
272274
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,
274313
payload: payloadValue
275314
}
276315

modules/channel-web/src/config.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
export interface Config {
2+
/**
3+
* Specify a builtin_text contentElement to be sent to the user after a file upload
4+
* example: #!builtin_text-OzpN5X (only builtin_text contentElements are supported)
5+
*
6+
* Variables available:
7+
* storage: 'local' or 's3'
8+
* url: URL where the file has been uploaded
9+
* originalName: Name that the file had on the user's computer
10+
* mime: mime type of the file, example: 'image/gif'
11+
* size: size from the file in bytes
12+
*
13+
* User variables are also available at 'event.state.user' (only user variables are supported)
14+
*/
15+
uploadsFileUploadedTextContentElement?: string
216
/**
317
* @default false
418
*/

0 commit comments

Comments
 (0)