2023-11-13 15:27:36 +01:00
|
|
|
import { ContinueChatResponse } from '@typebot.io/schemas'
|
2023-08-29 10:01:28 +02:00
|
|
|
import { WhatsAppSendingMessage } from '@typebot.io/schemas/features/whatsapp'
|
|
|
|
import { convertRichTextToWhatsAppText } from './convertRichTextToWhatsAppText'
|
2023-09-20 15:26:52 +02:00
|
|
|
import { isSvgSrc } from '@typebot.io/lib/utils'
|
2023-11-08 15:34:16 +01:00
|
|
|
import { BubbleBlockType } from '@typebot.io/schemas/features/blocks/bubbles/constants'
|
|
|
|
import { VideoBubbleContentType } from '@typebot.io/schemas/features/blocks/bubbles/video/constants'
|
2023-08-29 10:01:28 +02:00
|
|
|
|
|
|
|
const mp4HttpsUrlRegex = /^https:\/\/.*\.mp4$/
|
|
|
|
|
|
|
|
export const convertMessageToWhatsAppMessage = (
|
2023-11-13 15:27:36 +01:00
|
|
|
message: ContinueChatResponse['messages'][number]
|
2023-08-29 10:01:28 +02:00
|
|
|
): WhatsAppSendingMessage | undefined => {
|
|
|
|
switch (message.type) {
|
|
|
|
case BubbleBlockType.TEXT: {
|
|
|
|
if (!message.content.richText || message.content.richText.length === 0)
|
|
|
|
return
|
|
|
|
return {
|
|
|
|
type: 'text',
|
|
|
|
text: {
|
|
|
|
body: convertRichTextToWhatsAppText(message.content.richText),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case BubbleBlockType.IMAGE: {
|
|
|
|
if (!message.content.url || isImageUrlNotCompatible(message.content.url))
|
|
|
|
return
|
|
|
|
return {
|
|
|
|
type: 'image',
|
|
|
|
image: {
|
|
|
|
link: message.content.url,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case BubbleBlockType.AUDIO: {
|
|
|
|
if (!message.content.url) return
|
|
|
|
return {
|
|
|
|
type: 'audio',
|
|
|
|
audio: {
|
|
|
|
link: message.content.url,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case BubbleBlockType.VIDEO: {
|
|
|
|
if (
|
|
|
|
!message.content.url ||
|
|
|
|
(message.content.type !== VideoBubbleContentType.URL &&
|
|
|
|
isVideoUrlNotCompatible(message.content.url))
|
|
|
|
)
|
|
|
|
return
|
|
|
|
return {
|
|
|
|
type: 'video',
|
|
|
|
video: {
|
|
|
|
link: message.content.url,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case BubbleBlockType.EMBED: {
|
|
|
|
if (!message.content.url) return
|
|
|
|
return {
|
|
|
|
type: 'text',
|
|
|
|
text: {
|
|
|
|
body: message.content.url,
|
|
|
|
},
|
|
|
|
preview_url: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isImageUrlNotCompatible = (url: string) =>
|
|
|
|
!isHttpUrl(url) || isGifFileUrl(url) || isSvgSrc(url)
|
|
|
|
|
|
|
|
export const isVideoUrlNotCompatible = (url: string) =>
|
|
|
|
!mp4HttpsUrlRegex.test(url)
|
|
|
|
|
|
|
|
export const isHttpUrl = (text: string) =>
|
|
|
|
text.startsWith('http://') || text.startsWith('https://')
|
|
|
|
|
|
|
|
export const isGifFileUrl = (url: string) => {
|
|
|
|
const urlWithoutQueryParams = url.split('?')[0]
|
|
|
|
return urlWithoutQueryParams.endsWith('.gif')
|
|
|
|
}
|