Skip to content

Commit

Permalink
chore(core): core-bot and router logic to work with botState
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbasten17 committed Oct 19, 2021
1 parent 2e43d73 commit 3c8aa2c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 59 deletions.
59 changes: 39 additions & 20 deletions packages/botonic-core/src/core-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BotonicEvent,
BotRequest,
BotResponse,
BotState,
Locales,
MessageEventAck,
MessageEventFrom,
Expand Down Expand Up @@ -81,26 +82,30 @@ export class CoreBot {
)
}

getString(id: string, session: Session): string {
// @ts-ignore
return getString(this.locales, session.__locale, id)
getString(id: string, botState: BotState): string {
if (!botState.locale) {
console.error('Locale is not defined')
return ''
}
return getString(this.locales, botState.locale, id)
}

setLocale(locale: string, session: Session): void {
session.__locale = locale
setLocale(locale: string, botState: BotState): void {
botState.locale = locale
}

async input({
input,
session,
lastRoutePath,
botState,
dataProvider,
}: BotRequest): Promise<BotResponse> {
session = session || {}
if (!session.__locale) session.__locale = 'en'
if (!botState.locale) botState.locale = 'en'
// @ts-ignore
const userId = input.userId

const parsedUserEvent = this.botonicOutputParser.inputToBotonicEvent(input)

const parsedUserEvent = this.botonicOutputParser.parseFromUserInput(input)
const userId = session.user.id
if (dataProvider) {
// TODO: Next iterations. Review cycle of commited events to DB when messages change their ACK
// @ts-ignore
Expand All @@ -120,7 +125,7 @@ export class CoreBot {
'pre',
input,
session,
lastRoutePath,
botState,
undefined,
undefined,
dataProvider
Expand All @@ -133,7 +138,7 @@ export class CoreBot {
...(await getComputedRoutes(this.routes, {
input,
session,
lastRoutePath,
botState,
})),
...this.defaultRoutes,
],
Expand All @@ -144,18 +149,19 @@ export class CoreBot {
const output = (this.router as Router).processInput(
input,
session,
lastRoutePath
botState
)

const request = {
getString: stringId => this.getString(stringId, session),
setLocale: locale => this.setLocale(locale, session),
getString: stringId => this.getString(stringId, botState),
setLocale: locale => this.setLocale(locale, botState),
session: session || {},
params: output.params || {},
input: input,
plugins: this.plugins,
defaultTyping: this.defaultTyping,
defaultDelay: this.defaultDelay,
lastRoutePath,
botState,
dataProvider,
}

Expand All @@ -170,14 +176,15 @@ export class CoreBot {
console.error(e)
}

lastRoutePath = output.lastRoutePath
botState.lastRoutePath = output.botState.lastRoutePath

if (this.plugins) {
await runPlugins(
this.plugins,
'post',
input,
session,
lastRoutePath,
botState,
response,
messageEvents,
dataProvider
Expand All @@ -199,13 +206,25 @@ export class CoreBot {
}
}

session.is_first_interaction = false
botState.isFirstInteraction = false

if (dataProvider) {
const user = await dataProvider.getUser(userId)
if (!user) {
// throw error
} else {
const updatedUser = { ...user, session, botState }
// @ts-ignore
await dataProvider.updateUser(updatedUser)
}
}
// TODO: return also updatedUser?
return {
input,
response,
messageEvents,
session,
lastRoutePath,
botState,
dataProvider,
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/botonic-core/src/data-provider/dynamodb-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ export function getUserEntity(table: Table): Entity<any> {
},
userId: [SORT_KEY_NAME, 0],
websocketId: 'string',
name: 'string',
userName: 'string',
channel: 'string',
idFromChannel: 'string',
isOnline: 'boolean',
route: 'string',
session: 'string',
session: 'map',
botState: 'map',
details: 'map',
},
table,
})
Expand Down
21 changes: 11 additions & 10 deletions packages/botonic-core/src/handoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function getOpenQueues(
}

export class HandOffBuilder {
_session: SessionWithBotonicAction
_botState: any
_queue: string
_onFinish: string
_email: string
Expand All @@ -66,8 +66,8 @@ export class HandOffBuilder {
_caseInfo: string
_shadowing: boolean

constructor(session: SessionWithBotonicAction) {
this._session = session
constructor(botState: any) {
this._botState = botState
}

withQueue(queueNameOrId: string): this {
Expand Down Expand Up @@ -112,7 +112,7 @@ export class HandOffBuilder {

async handOff(): Promise<void> {
return _humanHandOff(
this._session,
this._botState,
this._queue,
this._onFinish,
this._email,
Expand Down Expand Up @@ -154,7 +154,7 @@ interface HubtypeHandoffParams {
on_finish?: string
}
async function _humanHandOff(
session: SessionWithBotonicAction,
botState: any,
queueNameOrId = '',
onFinish: string,
agentEmail = '',
Expand Down Expand Up @@ -185,7 +185,8 @@ async function _humanHandOff(
if (onFinish) {
params.on_finish = onFinish
}
session._botonic_action = `create_case:${JSON.stringify(params)}`
botState.botonicAction = `create_case:${JSON.stringify(params)}`
botState.isHandoff = true
}

export async function storeCaseRating(
Expand Down Expand Up @@ -256,14 +257,14 @@ export async function getAgentVacationRanges(
}

export function cancelHandoff(
session: SessionWithBotonicAction,
botState: any,
typification: string | null = null
): void {
let action = 'discard_case'
if (typification) action = `${action}:${JSON.stringify({ typification })}`
session._botonic_action = action
botState.botonicAction = action
}

export function deleteUser(session: SessionWithBotonicAction): void {
session._botonic_action = `delete_user`
export function deleteUser(botState: any): void {
botState.botonicAction = `delete_user`
}
8 changes: 4 additions & 4 deletions packages/botonic-core/src/hubtype-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, { AxiosResponse } from 'axios'
import Pusher, { AuthOptions, Channel } from 'pusher-js'
import Channels from 'pusher-js/types/src/core/channels/channels'

import { Input, SessionUser } from './models'
import { Input } from './models'
import { getWebpackEnvVar } from './utils'

interface UnsentInput {
Expand All @@ -21,7 +21,7 @@ interface ServerConfig {
}
interface HubtypeServiceArgs {
appId: string
user: SessionUser
user: any

This comment has been minimized.

Copy link
@asastre

asastre Oct 21, 2021

Contributor

Set it to any because it's still not defined?

lastMessageId: string
lastMessageUpdateDate: string
onEvent: any
Expand Down Expand Up @@ -50,7 +50,7 @@ const PONG_TIMEOUT = 5 * 1000 // https://pusher.com/docs/channels/using_channels
*/
export class HubtypeService {
appId: string
user: SessionUser
user: any
lastMessageId: string
lastMessageUpdateDate: string
onEvent: any
Expand Down Expand Up @@ -213,7 +213,7 @@ export class HubtypeService {
/**
* @return {Promise<void>}
*/
async postMessage(user: SessionUser, message: any): Promise<void> {
async postMessage(user: any, message: any): Promise<void> {
try {
// @ts-ignore
await this.init(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class BotonicOutputParser {
* to be saved. This is, converting a botonic input like: '{id: 'msgId', data: 'rawData', payload: 'somePayload'}'
* into a BotonicEvent with the expected properties.
*/
parseFromUserInput(input: any): Partial<BotonicEvent> {
inputToBotonicEvent(input: any): Partial<BotonicEvent> {
return this.factory.parse(input)
}

Expand Down
15 changes: 11 additions & 4 deletions packages/botonic-core/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { DataProvider } from './data-provider'
import { BotonicEvent, Input, PluginConfig, RoutePath, Session } from './models'
import {
BotonicEvent,
BotState,
Input,
PluginConfig,
RoutePath,
Session,
} from './models'

type PluginMode = 'pre' | 'post'

Expand All @@ -26,7 +33,7 @@ export async function runPlugins(
mode: PluginMode,
input: Input,
session: Session,
lastRoutePath: RoutePath,
botState: BotState,
response: string | null = null,
messageEvents: Partial<BotonicEvent>[] | null = null,
dataProvider?: DataProvider
Expand All @@ -35,12 +42,12 @@ export async function runPlugins(
const p = await plugins[key]
try {
if (mode === 'pre')
await p.pre({ input, session, lastRoutePath, dataProvider })
await p.pre({ input, session, botState, dataProvider })
if (mode === 'post')
await p.post({
input,
session,
lastRoutePath,
botState,
response,
messageEvents,
dataProvider,
Expand Down
Loading

0 comments on commit 3c8aa2c

Please sign in to comment.