Skip to content

Commit

Permalink
support for empty messages. closes #138
Browse files Browse the repository at this point in the history
Configure RuleType to decide if message is required.

Currently, SingleChoice and MultipleChoice can be sent
without message when options is defined.
  • Loading branch information
andersonba committed Jul 10, 2018
1 parent 4387329 commit c8ea474
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/core/__tests__/bot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,30 @@ test('auto reply message for single choice', async () => {
expect(onTalk).not.toBeCalledWith('Really?', {}, 'session');
});

['SingleChoice', 'MultipleChoice'].forEach((ruleType) => {
test(`send options without message in ${ruleType}`, async () => {
const onTalk = jest.fn();
const rules = loadYaml(`
- type: ${ruleType}
options:
- Red
- White
- type: ${ruleType}
`);
const bot = new YveBot(rules, OPTS)
.on('talk', onTalk)
.start();

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

expect(onTalk).toHaveBeenCalledTimes(1);
expect(onTalk).toBeCalledWith('', rules[0], 'session');
expect(onTalk).not.toBeCalledWith('', rules[1], 'session');
});
});

test('auto reply message for multiple choice', async () => {
const onTalk = jest.fn();
const rules = loadYaml(`
Expand Down
5 changes: 3 additions & 2 deletions src/core/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class Controller {
}
await utils.runActions(bot, rule, 'preActions');

if (rule.message) {
if (utils.isRuleMessageRequired(bot, rule)) {
await this.sendMessage(rule.message, rule);
}

Expand All @@ -77,8 +77,9 @@ export class Controller {
return this;
}

public async sendMessage(message: string, rule: IRule): Promise<this> {
public async sendMessage(input: string | null, rule: IRule): Promise<this> {
const { bot } = this;
const message = input || '';

bot.dispatch('typing');

Expand Down
2 changes: 2 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const types: { [name: string]: IRuleTypeExecutor } = {
warning: 'Unknown option',
},
],
requiredMessage: (rule: IRule) => !rule.options.length,
},

MultipleChoice: {
Expand Down Expand Up @@ -78,6 +79,7 @@ const types: { [name: string]: IRuleTypeExecutor } = {
warning: 'Unknown options',
},
],
requiredMessage: (rule: IRule) => !rule.options.length,
},
};

Expand Down
13 changes: 12 additions & 1 deletion src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function compileTemplate(template: string, payload: any): string {
}

export function calculateDelayToTypeMessage(message: string, time: number): number {
return (message || '').length * time;
return message.length * time;
}

export function isMatchAnswer(answer: Answer, option: string | number) {
Expand Down Expand Up @@ -175,3 +175,14 @@ export function getRuleByIndex(bot: YveBot, idx: number): IRule {
const rule = bot.rules[idx] ? bot.rules[idx] : sanitizeRule({ exit: true });
return { ...bot.options.rule, ...rule };
}

export function isRuleMessageRequired(bot: YveBot, rule: IRule): boolean {
if (
!rule.type ||
!bot.types[rule.type] ||
typeof bot.types[rule.type].requiredMessage !== 'function'
) {
return !!rule.message;
}
return !bot.types[rule.type].requiredMessage(rule);
}
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ export interface IRuleValidator {
}

export interface IRuleType {
requiredMessage?: (IRule) => boolean;
executors?: IRuleTypeExecutor[];
}

export interface IRuleTypeExecutor {
requiredMessage?: (IRule) => boolean;
validators?: IRuleValidator[];
transform?: RuleTypeTransform;
}
Expand Down

0 comments on commit c8ea474

Please sign in to comment.