diff --git a/packages/botonic-core/src/handoff.ts b/packages/botonic-core/src/handoff.ts index de6f9e1ab6..0ab9a345a3 100644 --- a/packages/botonic-core/src/handoff.ts +++ b/packages/botonic-core/src/handoff.ts @@ -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, @@ -74,6 +79,7 @@ export class HandOffBuilder { _shadowing: boolean _extraData: HandoffExtraData _bot_event: BotEventData + _subscribeHelpdeskEvents: HelpdeskEvent[] constructor(session: Session) { this._session = session @@ -144,6 +150,11 @@ export class HandOffBuilder { return this } + withSubscribeHelpdeskEvents(events: HelpdeskEvent[]): this { + this._subscribeHelpdeskEvents = events + return this + } + async handOff(): Promise { return _humanHandOff( this._session, @@ -158,7 +169,8 @@ export class HandOffBuilder { this._autoIdleMessage, this._shadowing, this._extraData, - this._bot_event + this._bot_event, + this._subscribeHelpdeskEvents ) } } @@ -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, @@ -210,7 +223,8 @@ async function _humanHandOff( autoIdleMessage = '', shadowing = false, extraData: HandoffExtraData | undefined = undefined, - botEvent: BotEventData + botEvent: BotEventData, + subscribeHelpdeskEvents: HelpdeskEvent[] = [] ) { const params: HubtypeHandoffParams = {} @@ -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 { diff --git a/packages/botonic-core/tests/handoff.test.ts b/packages/botonic-core/tests/handoff.test.ts index 5dda275755..3100f0f8e3 100644 --- a/packages/botonic-core/tests/handoff.test.ts +++ b/packages/botonic-core/tests/handoff.test.ts @@ -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([ @@ -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) + }) })