Skip to content

Commit

Permalink
🧑‍💻 (typebot-js) Add closeChatBubble event data
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Nov 10, 2022
1 parent ea84039 commit 54a1dc0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/typebot-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typebot-js",
"version": "2.2.12",
"version": "2.2.13",
"main": "dist/index.js",
"unpkg": "dist/index.global.js",
"license": "AGPL-3.0-or-later",
Expand Down
11 changes: 11 additions & 0 deletions packages/typebot-js/src/iframe/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { closeIframe } from '../embedTypes/chat/iframe'
import { TypebotPostMessageData, IframeCallbacks, IframeParams } from '../types'
import './style.css'

Expand Down Expand Up @@ -55,6 +56,15 @@ export const listenForTypebotMessages = (callbacks: IframeCallbacks) => {
})
}

const closeChatBubbleIfExisting = () => {
const bubble = document.querySelector('#typebot-bubble') as
| HTMLDivElement
| undefined
if (!bubble) return
const iframe = bubble.querySelector('.typebot-iframe') as HTMLIFrameElement
closeIframe(bubble, iframe)
}

const processMessage = (
data: TypebotPostMessageData,
callbacks: IframeCallbacks
Expand All @@ -63,4 +73,5 @@ const processMessage = (
if (data.newVariableValue && callbacks.onNewVariableValue)
callbacks.onNewVariableValue(data.newVariableValue)
if (data.codeToExecute) Function(data.codeToExecute)()
if (data.closeChatBubble) closeChatBubbleIfExisting()
}
1 change: 1 addition & 0 deletions packages/typebot-js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type TypebotPostMessageData = {
redirectUrl?: string
newVariableValue?: Variable
codeToExecute?: string
closeChatBubble?: boolean
}

export const localStorageKeys = {
Expand Down
25 changes: 25 additions & 0 deletions packages/typebot-js/tests/iframe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createIframe } from '../src/iframe'
import * as Typebot from '../src'
import { TypebotPostMessageData } from '../src'

describe('createIframe', () => {
it('should create a valid iframe element', () => {
Expand Down Expand Up @@ -111,4 +113,27 @@ describe('createIframe', () => {
expect(n).toBeUndefined()
expect(v).toBeUndefined()
})

it('should close chat when receive close command', async () => {
expect.assertions(2)
const { open } = Typebot.initBubble({
url: 'https://typebot.io/typebot-id2',
})
const bubble = document.getElementById('typebot-bubble')
open()
await new Promise((resolve) => setTimeout(resolve, 50))
expect(bubble?.classList.contains('iframe-opened')).toBe(true)
const messageData: TypebotPostMessageData = {
closeChatBubble: true,
}
window.postMessage(
{
from: 'typebot',
...messageData,
},
'*'
)
await new Promise((resolve) => setTimeout(resolve, 50))
expect(bubble?.classList.contains('iframe-opened')).toBe(false)
})
})

5 comments on commit 54a1dc0

@vercel
Copy link

@vercel vercel bot commented on 54a1dc0 Nov 10, 2022

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-alpha – ./apps/viewer

ns8.vn
yobot.me
247987.com
8jours.top
bot.aws.bj
bot.bbc.bj
finplex.be
sat.cr8.ai
bot.aipr.kr
minipost.uk
bt.id8rs.com
bot.krdfy.com
vhpage.cr8.ai
am.nigerias.io
an.nigerias.io
ar.nigerias.io
bot.enreso.org
bot.lalmon.com
ticketfute.com
apo.nigerias.io
apr.nigerias.io
aso.nigerias.io
bot.ageenda.com
bot.artiweb.app
bot.tc-mail.com
chat.sureb4.com
eventhub.com.au
games.klujo.com
sakuranembro.it
typebot.aloe.do
bot.piccinato.co
bot.upfunnel.art
botc.ceox.com.br
clo.closeer.work
faqs.nigerias.io
feedback.ofx.one
form.syncwin.com
kw.wpwakanda.com
myrentalhost.com
stan.vselise.com
start.taxtree.io
typebot.aloe.bot
voicehelp.cr8.ai
app.chatforms.net
bot.agfunnel.tech
bot.hostnation.de
sellmycarglasgow.com
talkbot.agfunnel.com
tenorioadvogados.com
uppity.wpwakanda.com
abutton.wpwakanda.com
aidigitalmarketing.kr
bbutton.wpwakanda.com
bot.incusservices.com
bot.meuesocial.com.br
bot.ramonmatos.com.br
cdd.searchcube.com.sg
chat.missarkansas.org
sbutton.wpwakanda.com
815639944.21000000.one
aplicacao.bmind.com.br
apply.ansuraniphone.my
bbutton.wpwwakanda.com
bot.louismarcondes.com
bot.t20worldcup.com.au
c23111azqw.nigerias.io
felipewelington.com.br
form.searchcube.com.sg
gcase.barrettamario.it
help.giversforgood.com
info.clickasuransi.com
kodawariab736.skeep.it
premium.kandabrand.com
report.gratirabbit.com
resume.gratirabbit.com
view.onlinebotdemo.xyz
83242573.actualizar.xyz
bot.blackboxtips.com.br
bot.upgradesolutions.eu
help.comebackreward.com
mainmenu.diddancing.com
register.kandabrand.com
signup.hypemarketing.in
subfooter.wpwakanda.com
survey.hypemarketing.in
testbot.afterorigin.com
91181264.your-access.one
ai.chromebookstoreph.com
form.sergiolimajr.com.br
hunterbot.saleshunter.ai
link.cascadigital.com.br
onboarding.growthside.io
reward.onlinebotdemo.xyz
type.opaulovieira.com.br
aibot.angrybranding.co.uk
bot.aidigitalmarketing.kr
bot.arraesecenteno.com.br
bot.blackboxsports.com.br
bot.cabinrentalagency.com
boyfriend-breakup.riku.ai

@vercel
Copy link

@vercel vercel bot commented on 54a1dc0 Nov 10, 2022

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 54a1dc0 Nov 10, 2022

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-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app
app.typebot.io

@vercel
Copy link

@vercel vercel bot commented on 54a1dc0 Nov 10, 2022

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.vercel.app
docs.typebot.io
docs-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 54a1dc0 Nov 10, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.