Skip to content

Commit 2717be3

Browse files
authored
Merge pull request #39 from rogodec/feature/32/scene_commands
Fix scene enter command && additional features scene enter && leave commands
2 parents bb12163 + 583b2d6 commit 2717be3

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

src/alice.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ export default class Alice {
145145
* activation trigger
146146
*/
147147
if (matchedScene) {
148-
if (matchedScene.isLeaveCommand(requestedCommandName)) {
149-
await matchedScene.handleRequest(req, sendResponse, ctxWithMiddlewares)
148+
if (await matchedScene.isLeaveCommand(ctxWithMiddlewares)) {
149+
await matchedScene.handleRequest(req, sendResponse, ctxWithMiddlewares, 'leave')
150150
session.setData('currentScene', null)
151+
this._handleLeaveScene()
151152
return true
152153
} else {
153154
const sceneResponse = await matchedScene.handleRequest(
@@ -162,12 +163,19 @@ export default class Alice {
162163
/*
163164
* Looking for scene's activational phrases
164165
*/
165-
const matchedScene = this.scenes.find((scene) =>
166-
scene.isEnterCommand(requestedCommandName))
166+
let matchedScene = null
167+
for (const scene of this.scenes) {
168+
const result = await scene.isEnterCommand(ctxWithMiddlewares)
169+
if (result) {
170+
matchedScene = scene
171+
}
172+
}
173+
167174
if (matchedScene) {
168175
session.setData('currentScene', matchedScene.name)
176+
this._handleEnterScene(matchedScene.name)
169177
const sceneResponse = await matchedScene.handleRequest(
170-
req, sendResponse, ctxWithMiddlewares,
178+
req, sendResponse, ctxWithMiddlewares, 'enter',
171179
)
172180
if (sceneResponse) {
173181
return true

src/ctx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class Ctx implements CtxInterface {
4444
this.messageId = req.session.message_id
4545
this.userId = req.session.user_id
4646
this.payload = req.request.payload
47-
this.message = req.request.original_utterance
47+
this.message = req.request.command
4848

4949
this.session = session
5050

src/scene.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const selectCommand = (req) => req.request.command
1111

1212
export default class Scene extends Alice {
1313
public name: string
14-
public enterCommand: Command
15-
public leaveCommand: Command
14+
public enterCommand: Commands
15+
public leaveCommand: Commands
1616
public anyCallback: (ctx: Ctx) => void
1717
public commands: Commands
1818
public config: configInterface
@@ -41,17 +41,17 @@ export default class Scene extends Alice {
4141
*/
4242
public enter(name, callback) {
4343
if (!name) { throw new Error('Enter command name is not specified') }
44-
this.enterCommand = new Command(name, callback)
45-
this.commands.add(name, callback)
44+
this.enterCommand = new Commands(this.config.fuseOptions || null)
45+
this.enterCommand.add(name, callback)
4646
}
4747

4848
/*
4949
* Trigger to leave the scene
5050
*/
5151
public leave(name, callback) {
5252
if (!name) { throw new Error('Leave command name is not specified') }
53-
this.leaveCommand = new Command(name, callback)
54-
this.commands.add(name, callback)
53+
this.leaveCommand = new Commands(this.config.fuseOptions || null)
54+
this.leaveCommand.add(name, callback)
5555
}
5656

5757
public command(name, callback) {
@@ -62,30 +62,37 @@ export default class Scene extends Alice {
6262
this.anyCallback = callback
6363
}
6464

65-
public isEnterCommand(commandName) {
65+
public async isEnterCommand(ctx) {
6666
if (!this.enterCommand) { return false }
67-
return this.enterCommand.name.toLowerCase() === commandName.toLowerCase()
67+
const matched = await this.enterCommand.search(ctx)
68+
return matched.length !== 0
6869
}
6970

70-
public isLeaveCommand(commandName) {
71+
public async isLeaveCommand(ctx) {
7172
if (!this.leaveCommand) { return false }
72-
return this.leaveCommand.name.toLowerCase() === commandName.toLowerCase()
73+
const matched = await this.leaveCommand.search(ctx)
74+
return matched.length !== 0
7375
}
7476

7577
public async handleRequest(
7678
req: WebhookRequest,
7779
sendResponse: (res: WebhookResponse) => void,
7880
ctx: CtxInterface,
81+
type: string = null,
7982
): Promise<any> {
8083

8184
ctx.sendResponse = sendResponse
8285
ctx.leaveScene = super._handleLeaveScene
8386
ctx.enterScene = super._handleEnterScene
8487

85-
const requestedCommands = await this.commands.search(ctx)
88+
let requestedCommands = []
8689

87-
if (this.isLeaveCommand(ctx.message)) {
88-
this._handleLeaveScene()
90+
if (type === 'enter') {
91+
requestedCommands = [this.enterCommand.get()[0]]
92+
} else if (type === 'leave') {
93+
requestedCommands = [this.leaveCommand.get()[0]]
94+
} else {
95+
requestedCommands = await this.commands.search(ctx)
8996
}
9097

9198
if (requestedCommands.length !== 0) {

0 commit comments

Comments
 (0)