2
0
Files
bot/packages/lib/computeTypingDuration.ts
Baptiste Arnaud b852b4af0b Add WhatsApp integration beta test (#722)
Related to #401
2023-08-29 10:05:24 +02:00

26 lines
738 B
TypeScript

import {
TypingEmulation,
defaultSettings,
} from '@typebot.io/schemas/features/typebot/settings'
type Props = {
bubbleContent: string
typingSettings?: TypingEmulation
}
export const computeTypingDuration = ({
bubbleContent,
typingSettings = defaultSettings({ isBrandingEnabled: false })
.typingEmulation,
}: Props) => {
let wordCount = bubbleContent.match(/(\w+)/g)?.length ?? 0
if (wordCount === 0) wordCount = bubbleContent.length
const typedWordsPerMinute = typingSettings.speed
let typingTimeout = typingSettings.enabled
? (wordCount / typedWordsPerMinute) * 60000
: 0
if (typingTimeout > typingSettings.maxDelay * 1000)
typingTimeout = typingSettings.maxDelay * 1000
return typingTimeout
}