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

fix(core): bot state not restoring on timeout #1826

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 5 additions & 5 deletions packages/bp/src/core/dialog/janitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TYPES } from 'core/app/types'
import { BotService } from 'core/bots'
import { BotpressConfig, ConfigProvider } from 'core/config'
import { SessionRepository, createExpiry, SessionIdFactory, DialogSession } from 'core/dialog/sessions'
import { StateManager } from 'core/dialog/state-manager'
import { JobService } from 'core/distributed'
import { Event, EventRepository } from 'core/events'
import { EventCollector } from 'core/events/event-collector'
Expand Down Expand Up @@ -38,7 +39,8 @@ export class DialogJanitor extends Janitor {
@inject(TYPES.UserRepository) private userRepo: ChannelUserRepository,
@inject(TYPES.JobService) private jobService: JobService,
@inject(TYPES.EventCollector) private eventCollector: EventCollector,
@inject(TYPES.EventRepository) private eventRepository: EventRepository
@inject(TYPES.EventRepository) private eventRepository: EventRepository,
@inject(TYPES.StateManager) private stateManager: StateManager
) {
super(logger)
}
Expand Down Expand Up @@ -113,16 +115,14 @@ export class DialogJanitor extends Janitor {
// Store the timeout event so we can detect loops
this.eventCollector.storeEvent(InternalEvent)

const { result: user } = await this.userRepo.getOrCreate(channel, target, botId)
// Restore the state for the timeout event
await this.stateManager.restore(InternalEvent)

// We clean the queue because that processTimeout will return the same event context
// if there is no jump, so we don't want the timeout event to process the previous queue
// The reason to return the event is to persist any changes made to session state or context
// in the session timeout hook
InternalEvent.state.context = { ...session.context, queue: undefined } as IO.DialogContext
InternalEvent.state.session = session.session_data as IO.CurrentSession
InternalEvent.state.user = user.attributes
InternalEvent.state.temp = session.temp_data

update.event = await this.dialogEngine.processTimeout(botId, sessionId, InternalEvent)

Expand Down
Loading