Skip to content

Commit

Permalink
support replyMessage in single/multiple options. closes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonba committed Nov 25, 2017
1 parent e1e7b6b commit ec0d12b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/core.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/ui.js

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions src/core/__tests__/bot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,61 @@ test('auto reply message', async () => {
expect(onTalk).toBeCalledWith('Thanks', {}, 'session');
});

test('auto reply message for single choice', async () => {
const onTalk = jest.fn();
const rules = loadYaml(`
- message: Color
type: SingleChoice
replyMessage: Nice color!
options:
- label: red
replyMessage: Red! Nice!
- label: white
replyMessage: Really?
`);
const bot = new YveBot(rules, OPTS)
.on('talk', onTalk)
.start();

await sleep();
bot.hear('red');
await sleep();

expect(onTalk).toBeCalledWith('Color', rules[0], 'session');
expect(onTalk).toBeCalledWith('Red! Nice!', {}, 'session');
expect(onTalk).not.toBeCalledWith('Nice color!', {}, 'session');
expect(onTalk).not.toBeCalledWith('Really?', {}, 'session');
});

test('auto reply message for multiple choice', async () => {
const onTalk = jest.fn();
const rules = loadYaml(`
- message: Color
type: MultipleChoice
replyMessage: Nice color!
options:
- label: red
replyMessage: Red! Nice!
- label: white
replyMessage: Really?
- label: blue
replyMessage: Nooo!
`);
const bot = new YveBot(rules, OPTS)
.on('talk', onTalk)
.start();

await sleep();
bot.hear('red, white');
await sleep();

expect(onTalk).toBeCalledWith('Color', rules[0], 'session');
expect(onTalk).toBeCalledWith('Red! Nice!', {}, 'session');
expect(onTalk).not.toBeCalledWith('Nice color!', {}, 'session');
expect(onTalk).not.toBeCalledWith('Nooo!', {}, 'session');
expect(onTalk).not.toBeCalledWith('Really?', {}, 'session');
});

test('compiled template', async () => {
const onTalk = jest.fn();
const rules = loadYaml(`
Expand Down
27 changes: 24 additions & 3 deletions src/core/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function validateAnswer(
rule: IRule,
bot: YveBot,
executorIndex: number,
) {
): Promise<Answer | Answer[]> {
const ruleValidators = rule.validators || [];
const typeExecutors = bot.types[rule.type].executors || [];
const currentTypeExecutor = typeExecutors[executorIndex] || {};
Expand Down Expand Up @@ -38,6 +38,26 @@ async function validateAnswer(
return answers;
}

function getReplyMessage(rule: IRule, answers: Answer | Answer[]): string | null {
const { replyMessage } = rule;
if (!rule.options.length) {
return replyMessage;
}
let opt;
// multiple
if (answers instanceof Array) {
[opt = null] = answers
.map((a) => utils.findOptionByAnswer(rule.options, a))
.filter((o) => o.replyMessage) ;
}
// single
opt = utils.findOptionByAnswer(rule.options, answers);
if (opt && opt.replyMessage) {
return opt.replyMessage;
}
return replyMessage;
}

function compileMessage(bot: YveBot, message: string): string {
const output = bot.store.output();
const { indexes } = bot.controller;
Expand Down Expand Up @@ -242,9 +262,10 @@ export class Controller {
bot.store.set(`output.${output}`, answer);
}

if (rule.replyMessage) {
const replyMessage = getReplyMessage(rule, answer);
if (replyMessage) {
const replyRule = Object.assign({}, bot.options.rule);
await this.sendMessage(rule.replyMessage, replyRule);
await this.sendMessage(replyMessage, replyRule);
}

// run post-actions
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface IRuleOption {
label?: string;
value?: string | number;
synonyms?: string[];
replyMessage?: string;
next?: RuleNext;
}

Expand Down

0 comments on commit ec0d12b

Please sign in to comment.