Skip to content

Commit

Permalink
⚡ (chat) Improve chat API compatibility with preview mode
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jan 16, 2023
1 parent dbe5c3c commit 7311988
Show file tree
Hide file tree
Showing 55 changed files with 4,131 additions and 463 deletions.
3,123 changes: 3,119 additions & 4 deletions apps/docs/openapi/chat/_spec_.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions apps/docs/openapi/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ The Chat API allows you to execute (chat) with a typebot.

### How to find my `typebotId`

If you'd like to execute the typebot in preview mode, you will need to provide the ID of the building typebot available in the editor URL:

<img
src="/img/api/typebotId.png"
width="900"
alt="Get typebot ID"
/>

For published typebot execution, you need to provide the public typebot ID available here:

<img
src="/img/api/publicId.png"
width="900"
alt="Get typebot ID"
/>
Binary file added apps/docs/static/img/api/publicId.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 9 additions & 64 deletions apps/viewer/src/components/TypebotPageV2.tsx
Original file line number Diff line number Diff line change
@@ -1,78 +1,31 @@
import { getInitialChatReplyQuery } from '@/queries/getInitialChatReplyQuery'
import {
getExistingResultFromSession,
setResultInSession,
} from '@/utils/sessionStorage'
import { Standard } from '@typebot.io/react'
import { BackgroundType, InitialChatReply, Typebot } from 'models'
import { BackgroundType, Typebot } from 'models'
import { useRouter } from 'next/router'
import { useCallback, useEffect, useState } from 'react'
import { ErrorPage } from './ErrorPage'
import { SEO } from './Seo'

export type TypebotPageV2Props = {
url: string
typebot: Pick<
Typebot,
'settings' | 'theme' | 'id' | 'name' | 'isClosed' | 'isArchived'
'settings' | 'theme' | 'name' | 'isClosed' | 'isArchived' | 'publicId'
>
}

let hasInitializedChat = false

export const TypebotPageV2 = ({ url, typebot }: TypebotPageV2Props) => {
const { asPath, push } = useRouter()
const [initialChatReply, setInitialChatReply] = useState<InitialChatReply>()
const [error, setError] = useState<Error | undefined>(undefined)

const background = typebot.theme.general.background

const clearQueryParamsIfNecessary = useCallback(() => {
const clearQueryParamsIfNecessary = () => {
const hasQueryParams = asPath.includes('?')
if (
!hasQueryParams ||
!(typebot.settings.general.isHideQueryParamsEnabled ?? true)
)
return
push(asPath.split('?')[0], undefined, { shallow: true })
}, [asPath, push, typebot.settings.general.isHideQueryParamsEnabled])

useEffect(() => {
console.log(open)
clearQueryParamsIfNecessary()
}, [clearQueryParamsIfNecessary])

useEffect(() => {
if (hasInitializedChat) return
hasInitializedChat = true
const prefilledVariables = extractPrefilledVariables()
const existingResultId = getExistingResultFromSession() ?? undefined

getInitialChatReplyQuery({
typebotId: typebot.id,
resultId:
typebot.settings.general.isNewResultOnRefreshEnabled ?? false
? undefined
: existingResultId,
prefilledVariables,
}).then(({ data, error }) => {
if (error && 'code' in error && error.code === 'FORBIDDEN') {
setError(new Error('This bot is now closed.'))
return
}
if (!data) return setError(new Error("Couldn't initiate the chat"))
setInitialChatReply(data)
setResultInSession(data.resultId)
})
}, [
initialChatReply,
typebot.id,
typebot.settings.general.isNewResultOnRefreshEnabled,
])

if (error) {
return <ErrorPage error={error} />
}

return (
<div
style={{
Expand All @@ -89,20 +42,12 @@ export const TypebotPageV2 = ({ url, typebot }: TypebotPageV2Props) => {
typebotName={typebot.name}
metadata={typebot.settings.metadata}
/>
{initialChatReply && (
<Standard typebotId={typebot.id} initialChatReply={initialChatReply} />
{typebot.publicId && (
<Standard
typebot={typebot.publicId}
onInit={clearQueryParamsIfNecessary}
/>
)}
</div>
)
}

const extractPrefilledVariables = () => {
const urlParams = new URLSearchParams(location.search)

const prefilledVariables: { [key: string]: string } = {}
urlParams.forEach((value, key) => {
prefilledVariables[key] = value
})

return prefilledVariables
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ import { ChoiceInputBlock } from 'models'
export const validateButtonInput = (
buttonBlock: ChoiceInputBlock,
input: string
) => buttonBlock.items.some((item) => item.content === input)
) =>
input
.split(',')
.every((value) =>
buttonBlock.items.some((item) => item.content === value.trim())
)
Loading

4 comments on commit 7311988

@vercel
Copy link

@vercel vercel bot commented on 7311988 Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

ns8.vn
1stop.au
yobot.me
247987.com
8jours.top
aginap.com
bee.cr8.ai
bot.aws.bj
bot.bbc.bj
cat.cr8.ai
finplex.be
nepkit.com
pig.cr8.ai
sat.cr8.ai
bot.aipr.kr
bot.joof.it
bull.cr8.ai
docs.cr8.ai
minipost.uk
mole.cr8.ai
team.cr8.ai
wolf.cr8.ai
cinecorn.com
kusamint.com
rhino.cr8.ai
sheep.cr8.ai
snake.cr8.ai
svhm.mprs.in
tiger.cr8.ai
video.cr8.ai
yoda.riku.ai
zebra.cr8.ai
bergamo.store
bot.krdfy.com
bot.tvbeat.it
cgcassets.com
filmylogy.com
goldorayo.com
rabbit.cr8.ai
signup.cr8.ai
turkey.cr8.ai
vhpage.cr8.ai
am.nigerias.io
an.nigerias.io
app.yvon.earth
ar.nigerias.io
bot.enreso.org
bot.rslabs.pro
bots.bridge.ai
chat.hayuri.id
michaeljackson.riku.ai
report.gratirabbit.com
casestudyemb.wpwakanda.com
chat.atlasoutfittersk9.com
configurator.bouclidom.com
help.atlasoutfittersk9.com
herbalife.barrettamario.it
homepageonly.wpwakanda.com
liveconvert.kandalearn.com
mainmenu1one.wpwakanda.com
tarian.theiofoundation.org
ted.meujalecobrasil.com.br
type.dericsoncalari.com.br
bot.pinpointinteractive.com
bot.polychromes-project.com
bot.seidinembroseanchetu.it
chatbot.berbelanjabiz.trade
designguide.techyscouts.com
liveconvert2.kandalearn.com
presente.empresarias.com.mx
sell.sellthemotorhome.co.uk
anamnese.odontopavani.com.br
austin.channelautomation.com
bot.marketingplusmindset.com
bot.seidibergamoseanchetu.it
desabafe.sergiolimajr.com.br
piazzatorre.barrettamario.it
requests.swamprecordsgnv.com
type.cookieacademyonline.com
bot.brigadeirosemdrama.com.br
forms.escoladeautomacao.com.br
onboarding.libertydreamcare.ie
type.talitasouzamarques.com.br
agendamento.sergiolimajr.com.br
anamnese.clinicamegasjdr.com.br
bookings.littlepartymonkeys.com
bot.comercializadoraomicron.com
elevateyourmind.groovepages.com
viewer-v2-typebot-io.vercel.app
yourfeedback.comebackreward.com
gerador.verificadordehospedes.com
personal-trainer.barrettamario.it
preagendamento.sergiolimajr.com.br
studiotecnicoimmobiliaremerelli.it
download.thailandmicespecialist.com
register.thailandmicespecialist.com
bot.studiotecnicoimmobiliaremerelli.it
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
viewer-v2-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 7311988 Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 7311988 Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
builder-v2-typebot-io.vercel.app
app.typebot.io

@vercel
Copy link

@vercel vercel bot commented on 7311988 Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs.typebot.io
docs-typebot-io.vercel.app
docs-git-main-typebot-io.vercel.app

Please sign in to comment.