2
0

(settings) Add delay between bubbles option and typing disabling on first message

This commit is contained in:
Baptiste Arnaud
2024-01-24 12:03:41 +01:00
parent e80f297e74
commit f052b4c805
16 changed files with 159 additions and 37 deletions

View File

@ -16,12 +16,14 @@ import { isNotDefined } from '@typebot.io/lib/utils'
import { computeTypingDuration } from '../computeTypingDuration'
import { continueBotFlow } from '../continueBotFlow'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { defaultSettings } from '@typebot.io/schemas/features/typebot/settings/constants'
// Media can take some time to be delivered. This make sure we don't send a message before the media is delivered.
const messageAfterMediaTimeout = 5000
type Props = {
to: string
isFirstChatChunk: boolean
typingEmulation: SessionState['typingEmulation']
credentials: WhatsAppCredentials['data']
state: SessionState
@ -30,6 +32,7 @@ type Props = {
export const sendChatReplyToWhatsApp = async ({
to,
typingEmulation,
isFirstChatChunk,
messages,
input,
clientSideActions,
@ -57,6 +60,7 @@ export const sendChatReplyToWhatsApp = async ({
to,
messages,
input,
isFirstChatChunk: false,
typingEmulation: newSessionState.typingEmulation,
clientSideActions,
credentials,
@ -64,19 +68,40 @@ export const sendChatReplyToWhatsApp = async ({
})
}
let i = -1
for (const message of messagesBeforeInput) {
i += 1
if (
i > 0 &&
(typingEmulation?.delayBetweenBubbles ??
defaultSettings.typingEmulation.delayBetweenBubbles) > 0
) {
await new Promise((resolve) =>
setTimeout(
resolve,
(typingEmulation?.delayBetweenBubbles ??
defaultSettings.typingEmulation.delayBetweenBubbles) * 1000
)
)
}
const whatsAppMessage = convertMessageToWhatsAppMessage(message)
if (isNotDefined(whatsAppMessage)) continue
const lastSentMessageIsMedia = ['audio', 'video', 'image'].includes(
sentMessages.at(-1)?.type ?? ''
)
const typingDuration = lastSentMessageIsMedia
? messageAfterMediaTimeout
: isFirstChatChunk &&
i === 0 &&
(typingEmulation?.isDisabledOnFirstMessage ??
defaultSettings.typingEmulation.isDisabledOnFirstMessage)
? 0
: getTypingDuration({
message: whatsAppMessage,
typingEmulation,
})
if (typingDuration)
if ((typingDuration ?? 0) > 0)
await new Promise((resolve) => setTimeout(resolve, typingDuration))
try {
await sendWhatsAppMessage({
@ -101,6 +126,7 @@ export const sendChatReplyToWhatsApp = async ({
to,
messages,
input,
isFirstChatChunk: false,
typingEmulation: newSessionState.typingEmulation,
clientSideActions,
credentials,