Skip to content

Commit

Permalink
feat: implement aliceStateMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 2, 2018
1 parent 6991279 commit ca4cd35
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 38 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"ramda": "^0.25.0"
},
"devDependencies": {
"@types/express": "^4.16.0",
"@types/jest": "^23.1.3",
"@types/node": "^10.5.1",
"conventional-changelog-cli": "^2.0.0",
Expand Down
6 changes: 3 additions & 3 deletions src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {
import {
applyMiddlewares,
MiddlewareType,

createAliceStateMiddleware,
} from './middlewares'

import aliceStateMiddleware from './middlewares/aliceStateMiddleware'

const DEFAULT_SESSIONS_LIMIT: number = 1000

export default class Alice {
Expand All @@ -36,7 +36,7 @@ export default class Alice {
this.anyCallback = null
this.welcomeCallback = null
this.commands = new Commands(config.fuseOptions || null)
this.middlewares = [createAliceStateMiddleware()]
this.middlewares = [aliceStateMiddleware()]
this.scenes = []
this.currentScene = null
this.sessions = new Sessions()
Expand Down
10 changes: 9 additions & 1 deletion src/button.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
const button = (params) => {
export interface ButtonParams {
title: string,
text: string,
tts?: string,
url?: string,
hide?: boolean,
payload?: {}
}
const button = (params: ButtonParams) => {
// Button has been created from string
if (typeof params === 'string') {
return {
Expand Down
3 changes: 1 addition & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const TYPE_FIGURE = 'figure'
const TYPE_REGEXP = 'regexp'
const TYPE_ARRAY = 'array'

class Commands {
export default class Commands {
public commands: Command[]
public fuseOptions: {}
constructor(config = null) {
Expand Down Expand Up @@ -93,5 +93,4 @@ class Commands {
module.exports = Commands
module.exports.Command = Command

export default Commands
export { Command }
18 changes: 0 additions & 18 deletions src/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,3 @@ export async function applyMiddlewares(middlewares, ctx: Ctx): Promise<Ctx> {
}
return newContext
}

export function createAliceStateMiddleware() {
const store = new Map()

return async (ctx) => {
const key = ctx.session.sessionId
let { state } = store.get(key) || { state: {} }
Object.defineProperty(ctx, 'state', {
get() { return state },
set(value) { state = Object.assign({}, value) },
})
store.set(key, {
state,
})

return ctx
}
}
17 changes: 17 additions & 0 deletions src/middlewares/aliceStateMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default function aliceStateMiddleware() {
const store = new Map()

return async (ctx) => {
const key = ctx.session.sessionId
let { state } = store.get(key) || { state: {} }
Object.defineProperty(ctx, 'state', {
get() { return state },
set(value) { state = Object.assign({}, value) },
})
store.set(key, {
state,
})

return ctx
}
}
40 changes: 30 additions & 10 deletions src/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@

const {
DEFAULT_END_SESSION,
ALICE_PROTOCOL_VERSION
ALICE_PROTOCOL_VERSION,
} = require('./constants')
import { ButtonParams } from './button'

const reply = (params) => {
const data = {
interface ReplyType {
response: {
text: string,
buttons?: ButtonParams[],
end_session: boolean,
},
session?: {},
version: string,
}

interface ParamsType {
text: string,
tts?: string,
shouldEndSession?: boolean,
endSession?: boolean,
end_session?: boolean,
session: {},
buttons: any[]
}
const reply = (params: ParamsType) => {
const data: ReplyType = {
response: {
buttons: [],
end_session: DEFAULT_END_SESSION
end_session: DEFAULT_END_SESSION,
},
session: null,
version: ALICE_PROTOCOL_VERSION
version: ALICE_PROTOCOL_VERSION,
}

if (typeof params === 'string') {
Expand All @@ -26,16 +46,16 @@ const reply = (params) => {
endSession,
end_session,
session,
buttons
buttons,
} = params

if (text) data.response.text = text
if (tts) data.response.tts = tts
if (buttons) data.response.buttons = buttons
if (text) { data.response.text = text }
if (tts) { data.response.tts = tts }
if (buttons) { data.response.buttons = buttons }
if (shouldEndSession || end_session || endSession) {
data.response.end_session = shouldEndSession || end_session || endSession
}
if (session) data.session = session
if (session) { data.session = session }

return data
} else {
Expand Down
14 changes: 11 additions & 3 deletions src/scene.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import Alice from './alice'
const Commands = require('./commands')
const Command = require('./commands').Command
const Ctx = require('./ctx')
import Commands from './commands'
import Command from './Command'
import Ctx from './ctx'

const selectCommand = (req) => req.request.command

export default class Scene extends Alice {
public name: string
public enterCommand: Command
public leaveCommand: Command
public anyCallback: (ctx: Ctx) => void
public commands: Commands
public config: {}

constructor(name, config = {}) {
super()
this.name = name
Expand All @@ -16,6 +23,7 @@ export default class Scene extends Alice {
this.enterCommand = null
this.leaveCommand = null
}

get title() {
return this.name
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"src/types/*"
]
},
"types": ["node", "jest"],
"types": ["node", "jest", "express"],
// typeRoots option has been previously configured
"typeRoots": [
// add path to @types
Expand Down

0 comments on commit ca4cd35

Please sign in to comment.