1
packages/bot-engine/src/features/chatwoot/index.ts
Normal file
1
packages/bot-engine/src/features/chatwoot/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export { openChatwootWidget } from './utils/openChatwootWidget'
|
@ -0,0 +1,83 @@
|
||||
import { ChatwootBlock, ChatwootOptions } from 'models/features/chatwoot'
|
||||
import { sendEventToParent } from 'services/chat'
|
||||
import { IntegrationContext } from 'services/integration'
|
||||
import { isEmbedded } from 'services/utils'
|
||||
import { parseCorrectValueType, parseVariables } from 'services/variable'
|
||||
|
||||
const parseSetUserCode = (user: ChatwootOptions['user']) => `
|
||||
window.$chatwoot.setUser("${user?.id ?? user?.email}", {
|
||||
email: ${user?.email ? `"${user.email}"` : 'undefined'},
|
||||
name: ${user?.name ? `"${user.name}"` : 'undefined'},
|
||||
avatar_url: ${user?.avatarUrl ? `"${user.avatarUrl}"` : 'undefined'},
|
||||
phone_number: ${user?.phoneNumber ? `"${user.phoneNumber}"` : 'undefined'},
|
||||
});
|
||||
|
||||
`
|
||||
const parseChatwootOpenCode = ({
|
||||
baseUrl,
|
||||
websiteToken,
|
||||
user,
|
||||
}: ChatwootOptions) => `
|
||||
if (window.$chatwoot) {
|
||||
if(${Boolean(user?.id || user?.email)}) {
|
||||
${parseSetUserCode(user)}
|
||||
}
|
||||
if (typeof Typebot !== 'undefined') Typebot.getBubbleActions?.().close()
|
||||
window.$chatwoot.toggle("open");
|
||||
} else {
|
||||
(function (d, t) {
|
||||
var BASE_URL = "${baseUrl}";
|
||||
var g = d.createElement(t),
|
||||
s = d.getElementsByTagName(t)[0];
|
||||
g.src = BASE_URL + "/packs/js/sdk.js";
|
||||
g.defer = true;
|
||||
g.async = true;
|
||||
s.parentNode.insertBefore(g, s);
|
||||
g.onload = function () {
|
||||
window.chatwootSDK.run({
|
||||
websiteToken: "${websiteToken}",
|
||||
baseUrl: BASE_URL,
|
||||
});
|
||||
window.addEventListener("chatwoot:ready", function () {
|
||||
if(${Boolean(user?.id || user?.email)}) {
|
||||
${parseSetUserCode(user)}
|
||||
}
|
||||
if (typeof Typebot !== 'undefined') Typebot.getBubbleActions?.().close()
|
||||
window.$chatwoot.toggle("open");
|
||||
});
|
||||
};
|
||||
})(document, "script");
|
||||
}`
|
||||
|
||||
export const openChatwootWidget = async (
|
||||
block: ChatwootBlock,
|
||||
{ variables, isPreview, onNewLog }: IntegrationContext
|
||||
) => {
|
||||
if (isPreview) {
|
||||
onNewLog({
|
||||
status: 'info',
|
||||
description: "Chatwoot won't open in preview mode",
|
||||
details: null,
|
||||
})
|
||||
} else if (isEmbedded) {
|
||||
sendEventToParent({
|
||||
codeToExecute: parseVariables(variables)(
|
||||
parseChatwootOpenCode(block.options)
|
||||
),
|
||||
})
|
||||
} else {
|
||||
const func = Function(
|
||||
...variables.map((v) => v.id),
|
||||
parseVariables(variables, { fieldToParse: 'id' })(
|
||||
parseChatwootOpenCode(block.options)
|
||||
)
|
||||
)
|
||||
try {
|
||||
await func(...variables.map((v) => parseCorrectValueType(v.value)))
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
return block.outgoingEdgeId
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
import { Log } from 'db'
|
||||
import { openChatwootWidget } from 'features/chatwoot'
|
||||
import {
|
||||
IntegrationBlock,
|
||||
IntegrationBlockType,
|
||||
@ -25,7 +26,7 @@ import { byId, sendRequest } from 'utils'
|
||||
import { sendGaEvent } from '../../lib/gtag'
|
||||
import { parseVariables, parseVariablesInObject } from './variable'
|
||||
|
||||
type IntegrationContext = {
|
||||
export type IntegrationContext = {
|
||||
apiHost: string
|
||||
typebotId: string
|
||||
groupId: string
|
||||
@ -59,6 +60,8 @@ export const executeIntegration = ({
|
||||
return executeWebhook(block, context)
|
||||
case IntegrationBlockType.EMAIL:
|
||||
return sendEmail(block, context)
|
||||
case IntegrationBlockType.CHATWOOT:
|
||||
return openChatwootWidget(block, context)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -167,7 +167,6 @@ const executeCode = async (
|
||||
{ typebot: { variables } }: LogicContext
|
||||
) => {
|
||||
if (!block.options.content) return
|
||||
console.log('isEmbedded', isEmbedded)
|
||||
if (block.options.shouldExecuteInParentContext && isEmbedded) {
|
||||
sendEventToParent({
|
||||
codeToExecute: parseVariables(variables)(block.options.content),
|
||||
|
Reference in New Issue
Block a user