2
0

🧑‍💻 (chat) Introduce startChat and continueChat endpoints

Closes #1030
This commit is contained in:
Baptiste Arnaud
2023-11-13 15:27:36 +01:00
parent 63233eb7ee
commit 084588a086
74 changed files with 28426 additions and 645 deletions

View File

@@ -1,4 +1,4 @@
import { ButtonItem, ChatReply } from '@typebot.io/schemas'
import { ButtonItem, ContinueChatResponse } from '@typebot.io/schemas'
import { WhatsAppSendingMessage } from '@typebot.io/schemas/features/whatsapp'
import { convertRichTextToWhatsAppText } from './convertRichTextToWhatsAppText'
import { isDefined, isEmpty } from '@typebot.io/lib/utils'
@@ -8,8 +8,8 @@ import { defaultPictureChoiceOptions } from '@typebot.io/schemas/features/blocks
import { defaultChoiceInputOptions } from '@typebot.io/schemas/features/blocks/inputs/choice/constants'
export const convertInputToWhatsAppMessages = (
input: NonNullable<ChatReply['input']>,
lastMessage: ChatReply['messages'][number] | undefined
input: NonNullable<ContinueChatResponse['input']>,
lastMessage: ContinueChatResponse['messages'][number] | undefined
): WhatsAppSendingMessage[] => {
const lastMessageText =
lastMessage?.type === BubbleBlockType.TEXT

View File

@@ -1,4 +1,4 @@
import { ChatReply } from '@typebot.io/schemas'
import { ContinueChatResponse } from '@typebot.io/schemas'
import { WhatsAppSendingMessage } from '@typebot.io/schemas/features/whatsapp'
import { convertRichTextToWhatsAppText } from './convertRichTextToWhatsAppText'
import { isSvgSrc } from '@typebot.io/lib/utils'
@@ -8,7 +8,7 @@ import { VideoBubbleContentType } from '@typebot.io/schemas/features/blocks/bubb
const mp4HttpsUrlRegex = /^https:\/\/.*\.mp4$/
export const convertMessageToWhatsAppMessage = (
message: ChatReply['messages'][number]
message: ContinueChatResponse['messages'][number]
): WhatsAppSendingMessage | undefined => {
switch (message.type) {
case BubbleBlockType.TEXT: {

View File

@@ -1,4 +1,8 @@
import { ChatReply, SessionState, Settings } from '@typebot.io/schemas'
import {
ContinueChatResponse,
SessionState,
Settings,
} from '@typebot.io/schemas'
import {
WhatsAppCredentials,
WhatsAppSendingMessage,
@@ -21,7 +25,7 @@ type Props = {
typingEmulation: SessionState['typingEmulation']
credentials: WhatsAppCredentials['data']
state: SessionState
} & Pick<ChatReply, 'messages' | 'input' | 'clientSideActions'>
} & Pick<ContinueChatResponse, 'messages' | 'input' | 'clientSideActions'>
export const sendChatReplyToWhatsApp = async ({
to,
@@ -171,7 +175,9 @@ const getTypingDuration = ({
}
}
const isLastMessageIncludedInInput = (input: ChatReply['input']): boolean => {
const isLastMessageIncludedInInput = (
input: ContinueChatResponse['input']
): boolean => {
if (isNotDefined(input)) return false
return input.type === InputBlockType.CHOICE
}
@@ -179,7 +185,9 @@ const isLastMessageIncludedInInput = (input: ChatReply['input']): boolean => {
const executeClientSideAction =
(context: { to: string; credentials: WhatsAppCredentials['data'] }) =>
async (
clientSideAction: NonNullable<ChatReply['clientSideActions']>[number]
clientSideAction: NonNullable<
ContinueChatResponse['clientSideActions']
>[number]
): Promise<{ replyToSend: string | undefined } | void> => {
if ('wait' in clientSideAction) {
await new Promise((resolve) =>

View File

@@ -1,6 +1,6 @@
import prisma from '@typebot.io/lib/prisma'
import {
ChatReply,
ContinueChatResponse,
PublicTypebot,
SessionState,
Settings,
@@ -20,7 +20,7 @@ import { VisitedEdge } from '@typebot.io/prisma'
type Props = {
incomingMessage?: string
workspaceId?: string
workspaceId: string
credentials: WhatsAppCredentials['data'] & Pick<WhatsAppCredentials, 'id'>
contact: NonNullable<SessionState['whatsApp']>['contact']
}
@@ -31,7 +31,7 @@ export const startWhatsAppSession = async ({
credentials,
contact,
}: Props): Promise<
| (ChatReply & {
| (ContinueChatResponse & {
newSessionState: SessionState
visitedEdges: VisitedEdge[]
})
@@ -89,9 +89,10 @@ export const startWhatsAppSession = async ({
version: 2,
message: incomingMessage,
startParams: {
typebot: publicTypebot.typebot.publicId as string,
type: 'live',
publicId: publicTypebot.typebot.publicId as string,
isOnlyRegistering: false,
},
userId: undefined,
initialSessionState: {
whatsApp: {
contact,