Skip to content

Commit

Permalink
chore(dev-template): project changes regarding botState data model
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbasten17 committed Oct 19, 2021
1 parent 0c6ec8d commit 5151797
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ import { handlers } from '.'

const dataProvider = dataProviderFactory(process.env.DATA_PROVIDER_URL)

async function botExecutor({ input, session, lastRoutePath, websocketId }) {
const { messageEvents } = await bot.input({
dataProvider,
async function botExecutor({ input, session, botState, websocketId }) {
const output = await bot.input({
input,
session,
lastRoutePath,
botState,
dataProvider,
})

const events = [
...output.messageEvents,
{ action: 'update_bot_state', ...output.botState },
{ action: 'update_session', ...output.session },
]
await handlers.run('sender', {
events,
websocketId,
})
await handlers.run('sender', { events: messageEvents, websocketId })
}

// eslint-disable-next-line no-undef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ import { dataProviderFactory } from '@botonic/core/lib/esm/data-provider'
import { decode } from 'jsonwebtoken'
import { ulid } from 'ulid'

const initialBotState = {
botId: '1234',
lastRoutePath: null,
isFirstInteraction: true,
retries: 0,
locale: 'en',
isHandoff: false,
isShadowing: false,
}

export const onAuth = async ({ websocketId, data, send }) => {
const { token } = JSON.parse(data)
const { userId } = decode(token)
const dp = dataProviderFactory(process.env.DATA_PROVIDER_URL)
let user = await dp.getUser(userId)
// console.log('got user', { user })
await dp.saveEvent({
eventType: EventTypes.CONNECTION,
userId,
Expand All @@ -17,21 +26,20 @@ export const onAuth = async ({ websocketId, data, send }) => {
status: ConnectionEventStatuses.CONNECTED,
})
if (!user) {
// console.log('create user connection id', websocketId)
user = await dp.saveUser({
const newUser = {
id: userId,
websocketId,
isOnline: true,
route: '/',
session: JSON.stringify({}),
})
// console.log('created user', { user })
botState: initialBotState,
session: {},
details: {}, // TODO: To be filled with geolocation info
}
user = await dp.saveUser(newUser)
} else {
// UPDATE USER CONNECTION
user = await dp.updateUser({
...user,
websocketId,
})
// console.log('updated user', { user })
}
}

0 comments on commit 5151797

Please sign in to comment.