diff --git a/src/alice.ts b/src/alice.ts index d539cb1..5af8bbb 100644 --- a/src/alice.ts +++ b/src/alice.ts @@ -173,16 +173,16 @@ export default class Alice implements IAlice { */ if (matchedScene) { if (await matchedScene.isLeaveCommand(ctxWithMiddlewares)) { - await matchedScene.handleSceneRequest(req, sendResponse, ctxWithMiddlewares, 'leave') + const sceneResponse = await matchedScene.handleSceneRequest(req, sendResponse, ctxWithMiddlewares, 'leave') session.setData('currentScene', null) this._handleLeaveScene() - return true + return sceneResponse } else { const sceneResponse = await matchedScene.handleSceneRequest( req, sendResponse, ctxWithMiddlewares, ) if (sceneResponse) { - return true + return sceneResponse } } } @@ -205,7 +205,7 @@ export default class Alice implements IAlice { req, sendResponse, ctxWithMiddlewares, 'enter', ) if (sceneResponse) { - return true + return sceneResponse } } } diff --git a/src/context.ts b/src/context.ts index af809d6..f6ef38b 100644 --- a/src/context.ts +++ b/src/context.ts @@ -35,6 +35,9 @@ export default class Context implements IContext { public sendResponse: (response: WebhookResponse) => void public enterScene: (sceneName: string) => void public leaveScene: () => void + + private _isReplied: boolean // forbids to send reply twice + constructor(params) { const { req, @@ -63,6 +66,8 @@ export default class Context implements IContext { this.replyBuilder = new ReplyBuilder(this.req) this.buttonBuilder = new ButtonBuilder() + this._isReplied = false + if (enterScene && leaveScene) { this.enterScene = enterScene this.leaveScene = leaveScene @@ -82,13 +87,13 @@ export default class Context implements IContext { return null } - public reply(replyMessage: string | IReply): void { + public reply(replyMessage: string | IReply): WebhookResponse { if (typeof replyMessage === 'undefined') { throw new Error('Reply message could not be empty!') } const message = this._createReply(replyMessage) - this._sendReply(message) + return this._sendReply(message) } public async replyWithImage(params: string | BigImageCard) { @@ -143,7 +148,9 @@ export default class Context implements IContext { return replyMessage } - private _sendReply(replyMessage: WebhookResponse) { + private _sendReply(replyMessage: WebhookResponse): any { + if (this._isReplied) return + this._isReplied = true /* * That fires when listening on port. */ diff --git a/src/tests/scene.spec.ts b/src/tests/scene.spec.ts index a08dc42..f138142 100644 --- a/src/tests/scene.spec.ts +++ b/src/tests/scene.spec.ts @@ -1,6 +1,6 @@ -import Alice from '../alice'; - -const Scene = require('../scene') +import Alice from '../alice' +import Scene from '../scene' +import { generateRequest } from './testUtils' test('creating scene with name', () => { const scene = new Scene('testName') @@ -18,3 +18,47 @@ test('registering an array of scenes', () => { expect(alice.scenes.length).toBe(2) }) +test('register scene and enter in', async () => { + const alice = new Alice() + const scene = new Scene('123') + scene.enter('1', ctx => ctx.reply('enter')) + scene.any(ctx => ctx.reply('scene-any')) + scene.command('3', ctx => ctx.reply('command')) + scene.leave('2', ctx => ctx.reply('leave')) + + alice.registerScene(scene) + alice.any(ctx => ctx.reply('hi')) + + let res + res = await alice.handleRequest(generateRequest('hello')) + expect(res.response.text).toBe('hi') + + res = await alice.handleRequest(generateRequest('1')) + expect(res.response.text).toBe('enter') + res = await alice.handleRequest(generateRequest('blablabla')) + expect(res.response.text).toBe('scene-any') + + res = await alice.handleRequest(generateRequest('2')) + expect(res.response.text).toBe('leave') +}) + +// test('changing scene', async () => { +// const alice = new Alice() +// const scene1 = new Scene('scene1') +// const scene2 = new Scene('scene2') +// scene1.enter('keyword', ctx => { +// ctx.reply('test') +// // ctx.leaveScene() +// // ctx.enterScene(scene2) +// }) +// scene2.any(ctx => ctx.reply('wazzup')) + +// alice.registerScene([scene1, scene2]) +// alice.any(ctx => ctx.reply('1')) + +// let data +// data = await alice.handleRequest(generateRequest('keyword')) +// expect(data.response.text).toBe('1') +// console.log(data) +// }) +