Skip to content

Commit

Permalink
botonic-core: add HandoffBuilder.withSubscribeHelpdeskEvents (#2917)
Browse files Browse the repository at this point in the history
## Description

Add function withSubscribeHelpdeskEvents in HandoffBuilder to subsrcibe:
status_changed and agent_message_created
  • Loading branch information
Iru89 authored Oct 4, 2024
1 parent 887a81a commit 07b2094
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
21 changes: 19 additions & 2 deletions packages/botonic-core/src/handoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ interface BotEventData {
country: string
}

export enum HelpdeskEvent {
StatusChanged = 'status_changed',
AgentMessageCreated = 'agent_message_created',
}

function contextDefaults(context: any): BackendContext {
return {
timeoutMs: context.timeoutMs || 10000,
Expand Down Expand Up @@ -74,6 +79,7 @@ export class HandOffBuilder {
_shadowing: boolean
_extraData: HandoffExtraData
_bot_event: BotEventData
_subscribeHelpdeskEvents: HelpdeskEvent[]

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

withSubscribeHelpdeskEvents(events: HelpdeskEvent[]): this {
this._subscribeHelpdeskEvents = events
return this
}

async handOff(): Promise<void> {
return _humanHandOff(
this._session,
Expand All @@ -158,7 +169,8 @@ export class HandOffBuilder {
this._autoIdleMessage,
this._shadowing,
this._extraData,
this._bot_event
this._bot_event,
this._subscribeHelpdeskEvents
)
}
}
Expand Down Expand Up @@ -196,6 +208,7 @@ interface HubtypeHandoffParams {
on_finish?: string
case_extra_data?: HandoffExtraData
bot_event?: BotEventData
subscribe_helpdesk_events?: HelpdeskEvent[]
}
async function _humanHandOff(
session: Session,
Expand All @@ -210,7 +223,8 @@ async function _humanHandOff(
autoIdleMessage = '',
shadowing = false,
extraData: HandoffExtraData | undefined = undefined,
botEvent: BotEventData
botEvent: BotEventData,
subscribeHelpdeskEvents: HelpdeskEvent[] = []
) {
const params: HubtypeHandoffParams = {}

Expand Down Expand Up @@ -249,6 +263,9 @@ async function _humanHandOff(
if (botEvent) {
params.bot_event = botEvent
}
if (subscribeHelpdeskEvents.length > 0) {
params.subscribe_helpdesk_events = subscribeHelpdeskEvents
}
if (!session.is_test_integration) {
session._botonic_action = `${BotonicAction.CreateCase}:${JSON.stringify(params)}`
} else {
Expand Down
11 changes: 10 additions & 1 deletion packages/botonic-core/tests/handoff.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-nocheck
import { BotonicAction, PATH_PAYLOAD_IDENTIFIER } from '../src'
import { HandOffBuilder, humanHandOff } from '../src/handoff'
import { HandOffBuilder, HelpdeskEvent, humanHandOff } from '../src/handoff'

describe('Handoff', () => {
test.each([
Expand Down Expand Up @@ -137,4 +137,13 @@ describe('Handoff', () => {
const expectedBotonicAction = `${BotonicAction.CreateTestCase}:payload1`
expect(builder._session._botonic_action).toEqual(expectedBotonicAction)
})

test('Create a handoff and subscribe to agent_messsage_created', () => {
const builder = new HandOffBuilder({})
.withSubscribeHelpdeskEvents([HelpdeskEvent.AgentMessageCreated])
.withOnFinishPayload('payload1')
builder.handOff()
const expectedBotonicAction = `${BotonicAction.CreateCase}:{"force_assign_if_not_available":true,"on_finish":"payload1","subscribe_helpdesk_events":["agent_message_created"]}`
expect(builder._session._botonic_action).toEqual(expectedBotonicAction)
})
})

0 comments on commit 07b2094

Please sign in to comment.