Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

botonic-core: add HandoffBuilder.withSubscribeHelpdeskEvents #2917

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
Loading