Skip to content

Commit

Permalink
core & plugin-flow-builder: HandoffBuilder withBotEvent (#2835)
Browse files Browse the repository at this point in the history
## Description

**botonic-core**
- Add `input.message_id`
- Add withBotEvent in HandoffBuilder to indicate to the backend to track
the handoff-success

**botonic-plugin-flow-builder**
- Remove handoff events (the commented code of the handoff-option event
is removed in a newer PR)


## Context

It is necessary to pass the language and the country to the withBotEvent
function
  • Loading branch information
Iru89 authored May 28, 2024
1 parent bcdd177 commit a029f29
Show file tree
Hide file tree
Showing 47 changed files with 560 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
workflow_dispatch:

jobs:
botonic-react-tests:
botonic-hubtype-analytics-tests:
uses: ./.github/workflows/botonic-common-workflow.yml
secrets: inherit #pragma: allowlist secret
with:
Expand Down
101 changes: 13 additions & 88 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/botonic-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botonic/core",
"version": "0.26.1-alpha.0",
"version": "0.27.0-alpha.0",
"license": "MIT",
"description": "Build Chatbots using React",
"main": "./lib/cjs/index.js",
Expand Down
21 changes: 19 additions & 2 deletions packages/botonic-core/src/handoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export type HandoffExtraData = {
location?: string
}

interface BotEventData {
language: string
country: string
}

function contextDefaults(context: any): BackendContext {
return {
timeoutMs: context.timeoutMs || 10000,
Expand Down Expand Up @@ -68,6 +73,7 @@ export class HandOffBuilder {
_autoIdleMessage: string
_shadowing: boolean
_extraData: HandoffExtraData
_bot_event: BotEventData

constructor(session: Session) {
this._session = session
Expand Down Expand Up @@ -133,6 +139,11 @@ export class HandOffBuilder {
return this
}

withBotEvent(botEvent: BotEventData): this {
this._bot_event = botEvent
return this
}

async handOff(): Promise<void> {
return _humanHandOff(
this._session,
Expand All @@ -146,7 +157,8 @@ export class HandOffBuilder {
this._note,
this._autoIdleMessage,
this._shadowing,
this._extraData
this._extraData,
this._bot_event
)
}
}
Expand Down Expand Up @@ -183,6 +195,7 @@ interface HubtypeHandoffParams {
shadowing?: boolean
on_finish?: string
case_extra_data?: HandoffExtraData
bot_event?: BotEventData
}
async function _humanHandOff(
session: Session,
Expand All @@ -196,7 +209,8 @@ async function _humanHandOff(
note = '',
autoIdleMessage = '',
shadowing = false,
extraData: HandoffExtraData | undefined = undefined
extraData: HandoffExtraData | undefined = undefined,
botEvent: BotEventData
) {
const params: HubtypeHandoffParams = {}

Expand Down Expand Up @@ -232,6 +246,9 @@ async function _humanHandOff(
if (extraData) {
params.case_extra_data = extraData
}
if (botEvent) {
params.bot_event = botEvent
}
session._botonic_action = `create_case:${JSON.stringify(params)}`
}

Expand Down
1 change: 1 addition & 0 deletions packages/botonic-core/src/models/legacy-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export interface Input extends Partial<NluResult> {
context?: {
campaign?: Campaign
}
message_id?: string
}

export interface Campaign {
Expand Down
4 changes: 2 additions & 2 deletions packages/botonic-plugin-flow-builder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botonic/plugin-flow-builder",
"version": "0.26.2-alpha.0",
"version": "0.27.0-alpha.0",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"description": "Use Flow Builder to show your contents",
Expand All @@ -14,7 +14,7 @@
"lint_core": "../../node_modules/.bin/eslint_d --cache --quiet 'src/**/*.ts*'"
},
"dependencies": {
"@botonic/react": "^0.26.2-alpha.0",
"@botonic/react": "^0.27.0-alpha.0",
"axios": "^1.6.8",
"uuid": "^9.0.1"
},
Expand Down
30 changes: 21 additions & 9 deletions packages/botonic-plugin-flow-builder/src/action/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FlowContent, FlowHandoff } from '../content-fields'
import { HtNodeWithContent } from '../content-fields/hubtype-fields'
import { getFlowBuilderPlugin } from '../helpers'
import BotonicPluginFlowBuilder from '../index'
import { EventAction, getNodeEventArgs, trackEvent } from '../tracking'
import { EventAction, trackEvent, trackFlowContent } from '../tracking'
import { createNodeFromKnowledgeBase } from './knowledge-bases'

export type FlowBuilderActionProps = {
Expand All @@ -24,7 +24,9 @@ export class FlowBuilderAction extends React.Component<FlowBuilderActionProps> {
const handoffContent = contents.find(
content => content instanceof FlowHandoff
) as FlowHandoff
if (handoffContent) await handoffContent.doHandoff(request)
if (handoffContent) {
await handoffContent.doHandoff(request)
}

return { contents }
}
Expand Down Expand Up @@ -89,10 +91,15 @@ async function getContentsByPayload({
: undefined

if (targetNode) {
const eventArgs = getNodeEventArgs(request, targetNode)
await trackEvent(request, EventAction.flowNode, eventArgs)
return await flowBuilderPlugin.getContentsByNode(targetNode, resolvedLocale)
const contents = await flowBuilderPlugin.getContentsByNode(
targetNode,
resolvedLocale
)
await trackFlowContent(request, contents)

return contents
}

return []
}

Expand All @@ -103,9 +110,13 @@ async function getContentsByFallback({
resolvedLocale,
}: FlowBuilderContext): Promise<FlowContent[]> {
const fallbackNode = await getFallbackNode(cmsApi, request)
const eventArgs = getNodeEventArgs(request, fallbackNode)
await trackEvent(request, EventAction.flowNode, eventArgs)
return await flowBuilderPlugin.getContentsByNode(fallbackNode, resolvedLocale)
const fallbackContents = await flowBuilderPlugin.getContentsByNode(
fallbackNode,
resolvedLocale
)
await trackFlowContent(request, fallbackContents)

return fallbackContents
}

async function getFallbackNode(cmsApi: FlowBuilderApi, request: ActionRequest) {
Expand All @@ -127,7 +138,8 @@ async function getFallbackNode(cmsApi: FlowBuilderApi, request: ActionRequest) {
request.session.user.extra_data.isFirstFallbackOption = !isFirstFallbackOption

trackEvent(request, EventAction.fallback, {
fallbackAttempt: isFirstFallbackOption ? 1 : 2,
fallbackOut: isFirstFallbackOption ? 1 : 2,
fallbackMessageId: request.input.message_id,
})

return fallbackNode
Expand Down
Loading

0 comments on commit a029f29

Please sign in to comment.