⚡ (chatwoot) Add result URL custom attribute
This commit is contained in:
@@ -2,32 +2,47 @@ import { ExecuteIntegrationResponse } from '@/features/chat/types'
|
||||
import { extractVariablesFromText } from '@/features/variables/extractVariablesFromText'
|
||||
import { parseGuessedValueType } from '@/features/variables/parseGuessedValueType'
|
||||
import { parseVariables } from '@/features/variables/parseVariables'
|
||||
import { isDefined } from '@typebot.io/lib'
|
||||
import {
|
||||
ChatwootBlock,
|
||||
ChatwootOptions,
|
||||
SessionState,
|
||||
} from '@typebot.io/schemas'
|
||||
|
||||
const parseSetUserCode = (user: ChatwootOptions['user']) => `
|
||||
window.$chatwoot.setUser("${user?.id ?? ''}", {
|
||||
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 parseSetUserCode = (user: ChatwootOptions['user'], resultId: string) =>
|
||||
user?.email || user?.id
|
||||
? `
|
||||
window.$chatwoot.setUser(${user?.id ?? `"${resultId}"`}, {
|
||||
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)}) {
|
||||
${parseSetUserCode(user)}
|
||||
}
|
||||
resultId,
|
||||
typebotId,
|
||||
}: ChatwootOptions & { typebotId: string; resultId: string }) => {
|
||||
const openChatwoot = `${parseSetUserCode(user, resultId)}
|
||||
window.$chatwoot.setCustomAttributes({
|
||||
typebot_result_url: "${
|
||||
process.env.NEXTAUTH_URL
|
||||
}/typebots/${typebotId}/results?id=${resultId}",
|
||||
});
|
||||
window.$chatwoot.toggle("open");
|
||||
} else {
|
||||
`
|
||||
|
||||
return `
|
||||
window.addEventListener("chatwoot:error", function (error) {
|
||||
console.log(error);
|
||||
});
|
||||
|
||||
if (window.$chatwoot) {${openChatwoot}}
|
||||
else {
|
||||
(function (d, t) {
|
||||
var BASE_URL = "${baseUrl}";
|
||||
var g = d.createElement(t),
|
||||
@@ -41,15 +56,11 @@ if (window.$chatwoot) {
|
||||
websiteToken: "${websiteToken}",
|
||||
baseUrl: BASE_URL,
|
||||
});
|
||||
window.addEventListener("chatwoot:ready", function () {
|
||||
if(${Boolean(user?.id || user?.email)}) {
|
||||
${parseSetUserCode(user)}
|
||||
}
|
||||
window.$chatwoot.toggle("open");
|
||||
});
|
||||
window.addEventListener("chatwoot:ready", function () {${openChatwoot}});
|
||||
};
|
||||
})(document, "script");
|
||||
}`
|
||||
}
|
||||
|
||||
const chatwootCloseCode = `
|
||||
if (window.$chatwoot) {
|
||||
@@ -59,17 +70,20 @@ if (window.$chatwoot) {
|
||||
`
|
||||
|
||||
export const executeChatwootBlock = (
|
||||
{ typebot: { variables }, result }: SessionState,
|
||||
{ typebot, result }: SessionState,
|
||||
block: ChatwootBlock,
|
||||
lastBubbleBlockId?: string
|
||||
): ExecuteIntegrationResponse => {
|
||||
const isPreview = !result.id
|
||||
const chatwootCode =
|
||||
block.options.task === 'Close widget'
|
||||
? chatwootCloseCode
|
||||
: isPreview
|
||||
? ''
|
||||
: parseChatwootOpenCode(block.options)
|
||||
: isDefined(result.id)
|
||||
? parseChatwootOpenCode({
|
||||
...block.options,
|
||||
typebotId: typebot.id,
|
||||
resultId: result.id,
|
||||
})
|
||||
: ''
|
||||
return {
|
||||
outgoingEdgeId: block.outgoingEdgeId,
|
||||
clientSideActions: [
|
||||
@@ -77,10 +91,10 @@ export const executeChatwootBlock = (
|
||||
lastBubbleBlockId,
|
||||
chatwoot: {
|
||||
scriptToExecute: {
|
||||
content: parseVariables(variables, { fieldToParse: 'id' })(
|
||||
content: parseVariables(typebot.variables, { fieldToParse: 'id' })(
|
||||
chatwootCode
|
||||
),
|
||||
args: extractVariablesFromText(variables)(chatwootCode).map(
|
||||
args: extractVariablesFromText(typebot.variables)(chatwootCode).map(
|
||||
(variable) => ({
|
||||
id: variable.id,
|
||||
value: parseGuessedValueType(variable.value),
|
||||
|
||||
@@ -9,7 +9,11 @@ export const extractVariablesFromText =
|
||||
const variable = variables.find(
|
||||
(variable) => variable.name === variableName
|
||||
)
|
||||
if (!variable) return acc
|
||||
if (
|
||||
!variable ||
|
||||
acc.find((accVariable) => accVariable.id === variable.id)
|
||||
)
|
||||
return acc
|
||||
return [...acc, variable]
|
||||
}, [])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user