Skip to content

Commit

Permalink
plugin-hubtype-analytics: Add target bot_interaction_id on feedback k…
Browse files Browse the repository at this point in the history
…nowledge base events (#2877)

## Description

**@botonic/core**
Add a new bot_interaction_id attribute in request.input.
Add bot_interaction_id in HandoffBuilder.withBotEvent

**@botonic/react**
Be able to store in a Text message the value of botInteractionId. Use
the value of botInteractionId when creating a feedback_knowledge_base
event

**@botonic/plugin-flow-builder**
When create a Text message using a knowledge base, add the attribute of
botInteractionId

**@botonic/plugin-hubtype-analytics**
Add in feedback_knowledge_base event the attribute
feedback_target_bot_interaction_id
Refactor and split Webviews events in 2 classes and also add optionals
attributes

## Context

This new attribute is needed in the knowledge base feedback events.

## Testing

Fix tests of @botonic/core, @botonic/plugin-flow-builder,
@botnic/plugin-flow-builder adding bot_interaction_id in Input tests
Add feedback_target_bot_interaction_id in feedback_knowledge_base test
  • Loading branch information
Iru89 committed Jul 29, 2024
1 parent 7f987e9 commit c9b75e8
Show file tree
Hide file tree
Showing 30 changed files with 167 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/botonic-core/src/handoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type HandoffExtraData = {
interface BotEventData {
language: string
country: string
bot_interaction_id?: string
}

function contextDefaults(context: any): BackendContext {
Expand Down
3 changes: 2 additions & 1 deletion packages/botonic-core/src/models/legacy-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ export interface Input extends Partial<NluResult> {
context?: {
campaign?: Campaign
}
message_id?: string
message_id: string
bot_interaction_id: string
}

export interface Campaign {
Expand Down
38 changes: 35 additions & 3 deletions packages/botonic-core/tests/routing/router.match-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,61 @@ import { BotRequest, INPUT, Input } from '../../src'
import { Router } from '../../src/routing'
import { testRoute, testSession } from '../helpers/routing'

const textInput: Input = { type: INPUT.TEXT, text: 'hi' }
const textInput: Input = {
type: INPUT.TEXT,
text: 'hi',
bot_interaction_id: 'testInteractionId',
message_id: 'testMessageId',
}

const textInputComplex: Input = {
type: INPUT.TEXT,
text: 'Cömplêx input &% 🚀',
bot_interaction_id: 'testInteractionId',
message_id: 'testMessageId',
}

const textPayloadInput: Input = {
type: INPUT.TEXT,
text: 'hi',
payload: 'foo',
bot_interaction_id: 'testInteractionId',
message_id: 'testMessageId',
}

const postbackInput: Input = {
type: INPUT.POSTBACK,
payload: 'foo',
bot_interaction_id: 'testInteractionId',
message_id: 'testMessageId',
}
const textPayloadInput: Input = { type: INPUT.TEXT, text: 'hi', payload: 'foo' }
const postbackInput: Input = { type: INPUT.POSTBACK, payload: 'foo' }

const audioInput: Input = {
type: INPUT.AUDIO,
src: 'data:audio/mpeg;base64,iVBORw0KG',
bot_interaction_id: 'testInteractionId',
message_id: 'testMessageId',
}

const documentInput: Input = {
type: INPUT.DOCUMENT,
src: 'data:application/pdf;base64,iVBORw0KG',
bot_interaction_id: 'testInteractionId',
message_id: 'testMessageId',
}

const imageInput: Input = {
type: INPUT.IMAGE,
src: 'data:image/png;base64,iVBORw0KG',
bot_interaction_id: 'testInteractionId',
message_id: 'testMessageId',
}

const videoInput: Input = {
type: INPUT.VIDEO,
src: 'data:video/mp4;base64,iVBORw0KG',
bot_interaction_id: 'testInteractionId',
message_id: 'testMessageId',
}

const requestInput: BotRequest = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class FlowHandoff extends ContentFieldsBase {
handOffBuilder.withBotEvent({
language: request.session.user.extra_data.language,
country: request.session.user.extra_data.country,
bot_interaction_id: request.input.bot_interaction_id,
})
this.isTestIntegration = request.session.is_test_integration
await handOffBuilder.handOff()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Text } from '@botonic/react'
import { ActionRequest, Text } from '@botonic/react'
import React from 'react'

import { ContentFieldsBase } from './content-fields-base'
Expand All @@ -20,12 +20,13 @@ export class FlowKnowledgeBase extends ContentFieldsBase {
return newKnowledgeBase
}

toBotonic(id: string): JSX.Element {
toBotonic(id: string, request: ActionRequest): JSX.Element {
return (
<Text
key={id}
feedbackEnabled={this.feedbackEnabled}
inferenceId={this.inferenceId}
botInteractionId={request.input.bot_interaction_id}
>
{this.text}
</Text>
Expand Down
9 changes: 6 additions & 3 deletions packages/botonic-plugin-flow-builder/tests/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ResolvedPlugins,
} from '@botonic/core'
import { ActionRequest } from '@botonic/react'
import { v4 as uuid } from 'uuid'

import BotonicPluginFlowBuilder, {
FlowBuilderAction,
Expand Down Expand Up @@ -48,7 +47,7 @@ export function createFlowBuilderPlugin({
}

interface RequestArgs {
input: Input
input: Omit<Input, 'bot_interaction_id' | 'message_id'>
plugins?: ResolvedPlugins
provider?: ProviderType
isFirstInteraction?: boolean
Expand All @@ -74,7 +73,11 @@ export function createRequest({
_hubtype_api: 'https://api.hubtype.com',
is_test_integration: false,
},
input: { ...input, message_id: uuid() },
input: {
bot_interaction_id: 'testInteractionId',
message_id: 'testMessageId',
...input,
},
lastRoutePath: '',
plugins,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { HtEvent } from './ht-event'
export class HtEventFeedbackKnowledgebase extends HtEvent {
action: EventAction.FeedbackKnowledgebase
knowledgebase_inference_id: string
feedback_target_bot_interaction_id: string
feedback_target_id: string
feedback_group_id: string
possible_options: string[]
Expand All @@ -22,6 +23,7 @@ export class HtEventFeedbackKnowledgebase extends HtEvent {
this.type = EventType.WebEvent
this.action = event.action
this.knowledgebase_inference_id = event.knowledgebaseInferenceId
this.feedback_target_bot_interaction_id = event.feedbackBotInteractionId
this.feedback_target_id = event.feedbackTargetId
this.feedback_group_id = event.feedbackGroupId
this.possible_options = event.possibleOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { EventAction, EventType, EventWebview, RequestData } from '../types'
import { EventAction, EventType, EventWebviewEnd, RequestData } from '../types'
import { HtEvent } from './ht-event'

export class HtEventWebview extends HtEvent {
action: EventAction.WebviewStep | EventAction.WebviewEnd
export class HtEventWebviewEnd extends HtEvent {
action: EventAction.WebviewEnd
flow_thread_id?: string
webview_thread_id: string
webview_name: string
webview_step_name?: string
webview_end_step_name?: string
webview_end_step_n?: number
webview_end_fail_type?: string
webview_end_fail_message?: string

constructor(event: EventWebview, requestData: RequestData) {
constructor(event: EventWebviewEnd, requestData: RequestData) {
super(event, requestData)
this.type = EventType.WebEvent
this.action = event.action
this.flow_thread_id = event.flowThreadId
this.webview_thread_id = event.webviewThreadId
this.webview_name = event.webviewName
this.webview_step_name = event.webviewStepName
this.webview_end_step_name = event.webviewStepName
this.webview_end_step_n = event.webviewStepNumber
this.webview_end_fail_type = event.webviewEndFailType
this.webview_end_fail_message = event.webviewEndFailMessage
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { EventAction, EventType, EventWebviewStep, RequestData } from '../types'
import { HtEvent } from './ht-event'

export class HtEventWebviewStep extends HtEvent {
action: EventAction.WebviewStep
flow_thread_id?: string
webview_thread_id: string
webview_name: string
webview_step_name?: string
webview_step_n?: number

constructor(event: EventWebviewStep, requestData: RequestData) {
super(event, requestData)
this.type = EventType.WebEvent
this.action = event.action
this.flow_thread_id = event.flowThreadId
this.webview_thread_id = event.webviewThreadId
this.webview_name = event.webviewName
this.webview_step_name = event.webviewStepName
this.webview_step_n = event.webviewStepNumber
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { EventAction, EventType, HtEventProps, RequestData } from '../types'

const excludedEvents = [
EventAction.FeedbackCase,
EventAction.FeedbackConversation,
EventAction.FeedbackKnowledgebase,
EventAction.FeedbackWebview,
EventAction.WebviewStep,
EventAction.WebviewEnd,
]

export class HtEvent {
chat_id: string
type: EventType
Expand All @@ -9,12 +18,17 @@ export class HtEvent {
bot_version?: string
flow_version?: string
action: EventAction
bot_interaction_id?: string

constructor(event: HtEventProps, requestData: RequestData) {
this.chat_id = requestData.userId
this.chat_language = requestData.language
this.chat_country = requestData.country
this.format_version = 2
this.action = event.action

if (!excludedEvents.includes(event.action)) {
this.bot_interaction_id = requestData.botInteractionId
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export { HtEventIntent } from './ht-event-intent'
export { HtEventIntentSmart } from './ht-event-intent-smart'
export { HtEventKeyword } from './ht-event-keyword'
export { HtEventKnowledgeBase } from './ht-event-knowledge-base'
export { HtEventWebview } from './ht-event-webview'
export { HtEventWebviewEnd } from './ht-event-webview-end'
export { HtEventWebviewStep } from './ht-event-webview-step'
3 changes: 2 additions & 1 deletion packages/botonic-plugin-hubtype-analytics/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BotRequest, Plugin } from '@botonic/core'
import axios, { AxiosError } from 'axios'
import axios from 'axios'

import { HtEvent } from './event-models'
import { EventType, HtEventProps, RequestData } from './types'
Expand Down Expand Up @@ -44,6 +44,7 @@ export default class BotonicPluginHubtypeAnalytics implements Plugin {
language: this.getLanguage(request),
country: this.getCountry(request),
userId: request.session.user.id,
botInteractionId: request.input?.bot_interaction_id,
}
}

Expand Down
20 changes: 17 additions & 3 deletions packages/botonic-plugin-hubtype-analytics/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface EventFeedback extends HtBaseEventProps {
export interface EventFeedbackKnowledgebase extends HtBaseEventProps {
action: EventAction.FeedbackKnowledgebase
knowledgebaseInferenceId: string
feedbackBotInteractionId: string
feedbackTargetId: string
feedbackGroupId: string
possibleOptions: string[]
Expand Down Expand Up @@ -130,11 +131,22 @@ export interface EventFallback extends HtBaseEventProps {
fallbackMessageId: string
}

export interface EventWebview extends HtBaseEventProps {
action: EventAction.WebviewStep | EventAction.WebviewEnd
export interface EventWebviewStep extends HtBaseEventProps {
action: EventAction.WebviewStep
flowThreadId?: string
webviewThreadId: string
webviewName: string
webviewStepName: string
webviewStepNumber?: number
}

export interface EventWebviewEnd extends HtBaseEventProps {
action: EventAction.WebviewEnd
flowThreadId?: string
webviewThreadId: string
webviewName: string
webviewStepName?: string
webviewStepNumber?: number
webviewEndFailType?: string
webviewEndFailMessage?: string
}
Expand All @@ -156,11 +168,13 @@ export type HtEventProps =
| EventIntentSmart
| EventKnowledgeBase
| EventFallback
| EventWebview
| EventWebviewStep
| EventWebviewEnd
| EventCustom

export interface RequestData {
language: string
country: string
userId: string
botInteractionId: string
}
7 changes: 5 additions & 2 deletions packages/botonic-plugin-hubtype-analytics/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
HtEventIntentSmart,
HtEventKeyword,
HtEventKnowledgeBase,
HtEventWebview,
HtEventWebviewEnd,
HtEventWebviewStep,
} from './event-models'
import { EventAction, HtEventProps, RequestData } from './types'

Expand Down Expand Up @@ -55,8 +56,10 @@ export function createHtEvent(
return new HtEventFallback(htEventProps, requestData)

case EventAction.WebviewStep:
return new HtEventWebviewStep(htEventProps, requestData)

case EventAction.WebviewEnd:
return new HtEventWebview(htEventProps, requestData)
return new HtEventWebviewEnd(htEventProps, requestData)

case EventAction.Custom:
return new HtEventCustom(htEventProps, requestData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('Create custom events', () => {
value: '12345',
},
custom_sensitive_fields: { bank_account: '1234567890' },
bot_interaction_id: 'testInteractionId',
type: EventType.WebEvent,
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('Create fallback events', () => {
user_input: 'userInputTest',
fallback_out: 1,
fallback_message_id: 'fallbackMessageIdTest',
bot_interaction_id: 'testInteractionId',
type: EventType.BotEvent,
})
})
Expand All @@ -41,6 +42,7 @@ describe('Create fallback events', () => {
user_input: 'userInputTest',
fallback_out: 2,
fallback_message_id: 'fallbackMessageIdTest',
bot_interaction_id: 'testInteractionId',
type: EventType.BotEvent,
})
})
Expand All @@ -62,6 +64,7 @@ describe('Create fallback events', () => {
user_input: 'userInputTest',
fallback_out: 1,
fallback_message_id: 'fallbackMessageIdTest',
bot_interaction_id: 'testInteractionId',
type: EventType.BotEvent,
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('Create feedback knowledgebase event', () => {
action: EventAction.FeedbackKnowledgebase,
feedbackTargetId: 'messageIdTest',
feedbackGroupId: 'groupIdTest',
feedbackBotInteractionId: 'testInteractionId',
knowledgebaseInferenceId: 'knowledgebaseInferenceIdTest',
possibleOptions: ['thumbs_down', 'thumbs_up'],
possibleValues: [0, 1],
Expand All @@ -23,6 +24,7 @@ describe('Create feedback knowledgebase event', () => {
action: EventAction.FeedbackKnowledgebase,
feedback_target_id: 'messageIdTest',
feedback_group_id: 'groupIdTest',
feedback_target_bot_interaction_id: 'testInteractionId',
knowledgebase_inference_id: 'knowledgebaseInferenceIdTest',
possible_options: ['thumbs_down', 'thumbs_up'],
possible_values: [0, 1],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('Create flow event', () => {
flow_node_id: 'flowNodeIdTest',
flow_node_content_id: 'flowNodeContentIdTest',
flow_node_is_meaningful: false,
bot_interaction_id: 'testInteractionId',
type: EventType.BotEvent,
})
})
Expand Down
Loading

0 comments on commit c9b75e8

Please sign in to comment.