2
0

(bot) Use ky for queries in bot to improve reliability

This commit is contained in:
Baptiste Arnaud
2023-12-20 08:31:22 +01:00
parent f2cccbd33f
commit a6536461e5
11 changed files with 167 additions and 135 deletions

View File

@@ -1,6 +1,6 @@
import { BotContext, InitialChatReply } from '@/types'
import { guessApiHost } from '@/utils/guessApiHost'
import { isNotDefined, isNotEmpty, sendRequest } from '@typebot.io/lib'
import { isNotDefined, isNotEmpty } from '@typebot.io/lib'
import {
getPaymentInProgressInStorage,
removePaymentInProgressFromStorage,
@@ -10,6 +10,18 @@ import {
StartFrom,
StartPreviewChatInput,
} from '@typebot.io/schemas'
import ky from 'ky'
type Props = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
typebot: string | any
stripeRedirectStatus?: string
apiHost?: string
startFrom?: StartFrom
isPreview: boolean
prefilledVariables?: Record<string, unknown>
resultId?: string
}
export async function startChatQuery({
typebot,
@@ -19,16 +31,7 @@ export async function startChatQuery({
resultId,
stripeRedirectStatus,
startFrom,
}: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
typebot: string | any
stripeRedirectStatus?: string
apiHost?: string
startFrom?: StartFrom
isPreview: boolean
prefilledVariables?: Record<string, unknown>
resultId?: string
}) {
}: Props) {
if (isNotDefined(typebot))
throw new Error('Typebot ID is required to get initial messages')
@@ -41,67 +44,75 @@ export async function startChatQuery({
: undefined
if (paymentInProgressState) {
removePaymentInProgressFromStorage()
const { data, error, response } = await sendRequest<InitialChatReply>({
method: 'POST',
url: `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v1/sessions/${
paymentInProgressState.sessionId
}/continueChat`,
body: {
message: paymentInProgressState
? stripeRedirectStatus === 'failed'
? 'fail'
: 'Success'
: undefined,
},
})
return {
data: data
? {
...data,
...(paymentInProgressState
? { typebot: paymentInProgressState.typebot }
: {}),
try {
const data = await ky
.post(
`${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v1/sessions/${
paymentInProgressState.sessionId
}/continueChat`,
{
json: {
message: paymentInProgressState
? stripeRedirectStatus === 'failed'
? 'fail'
: 'Success'
: undefined,
},
timeout: false,
}
: undefined,
error,
response,
)
.json<InitialChatReply>()
return { data }
} catch (error) {
return { error }
}
}
const typebotId = typeof typebot === 'string' ? typebot : typebot.id
if (isPreview) {
const { data, error, response } = await sendRequest<InitialChatReply>({
method: 'POST',
url: `${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/typebots/${typebotId}/preview/startChat`,
body: {
isStreamEnabled: true,
startFrom,
typebot,
} satisfies Omit<StartPreviewChatInput, 'typebotId'>,
})
return {
data,
error,
response,
try {
const data = await ky
.post(
`${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/typebots/${typebotId}/preview/startChat`,
{
json: {
isStreamEnabled: true,
startFrom,
typebot,
} satisfies Omit<StartPreviewChatInput, 'typebotId'>,
timeout: false,
}
)
.json<InitialChatReply>()
return { data }
} catch (error) {
return { error }
}
}
const { data, error, response } = await sendRequest<InitialChatReply>({
method: 'POST',
url: `${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/typebots/${typebotId}/startChat`,
body: {
isStreamEnabled: true,
prefilledVariables,
resultId,
} satisfies Omit<StartChatInput, 'publicId'>,
})
try {
const data = await ky
.post(
`${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/typebots/${typebotId}/startChat`,
{
json: {
isStreamEnabled: true,
prefilledVariables,
resultId,
} satisfies Omit<StartChatInput, 'publicId'>,
timeout: false,
}
)
.json<InitialChatReply>()
return {
data,
error,
response,
return { data }
} catch (error) {
return { error }
}
}