Skip to content

Commit

Permalink
feat: handle lex confirm state
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jan 10, 2019
1 parent 58592e3 commit edb194d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/controller/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { INJECT_CLOCK } from 'src/BaseService';
import { INJECT_STORAGE } from 'src/BotService';
import { CheckRBAC, Controller, ControllerData, Handler } from 'src/controller';
import { BaseController, BaseControllerOptions, ErrorReplyType } from 'src/controller/BaseController';
import { createCompletion } from 'src/controller/helpers';
import { createCommandCompletion } from 'src/controller/helpers';
import { Role } from 'src/entity/auth/Role';
import { Token } from 'src/entity/auth/Token';
import { User } from 'src/entity/auth/User';
Expand Down Expand Up @@ -124,7 +124,7 @@ export class AccountController extends BaseController<AccountControllerData> imp
const name = user.name;

if (cmd.getHeadOrDefault('confirm', 'no') !== 'yes') {
const completion = createCompletion(cmd, 'confirm', this.translate('account-delete.confirm', {
const completion = createCommandCompletion(cmd, 'confirm', this.translate('account-delete.confirm', {
name,
}));
await this.bot.executeCommand(completion);
Expand Down
4 changes: 2 additions & 2 deletions src/controller/TokenController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { INJECT_CLOCK } from 'src/BaseService';
import { INJECT_STORAGE } from 'src/BotService';
import { CheckRBAC, Controller, ControllerData, Handler } from 'src/controller';
import { BaseController, BaseControllerOptions } from 'src/controller/BaseController';
import { createCompletion } from 'src/controller/helpers';
import { createCommandCompletion } from 'src/controller/helpers';
import { Token } from 'src/entity/auth/Token';
import { Command, CommandVerb } from 'src/entity/Command';
import { Context } from 'src/entity/Context';
Expand Down Expand Up @@ -70,7 +70,7 @@ export class TokenController extends BaseController<TokenControllerData> impleme
const user = this.getUserOrFail(ctx);
const before = cmd.getHeadOrNumber('before', this.clock.getSeconds());
if (cmd.getHeadOrDefault('confirm', 'no') !== 'yes') {
const completion = createCompletion(cmd, 'confirm', this.translate('delete.confirm', {
const completion = createCommandCompletion(cmd, 'confirm', this.translate('delete.confirm', {
before,
name: user.name,
}));
Expand Down
10 changes: 7 additions & 3 deletions src/controller/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { isNumber, isString } from 'lodash';
import { BaseError } from 'noicejs';

import { NOUN_FRAGMENT } from 'src/controller/CompletionController';
import { Command, CommandVerb } from 'src/entity/Command';
import { Command, CommandOptions, CommandVerb } from 'src/entity/Command';
import { Parser } from 'src/parser';
import { Schema } from 'src/schema';
import { mustExist } from 'src/utils';
import { Dict, dictToMap, mapToDict } from 'src/utils/Map';

export function createCompletion(cmd: Command, key: string, msg: string): Command {
export function createCommandCompletion(cmd: Command, key: string, msg: string): Command {
const ctx = mustExist(cmd.context);
const parser = mustExist(ctx.parser);
return createCompletion(cmd, key, msg, parser);
}

export function createCompletion(cmd: CommandOptions, key: string, msg: string, parser: Parser): Command {
const existingData = mapToDict(cmd.data);
const data = dictToMap({
...existingData,
Expand Down Expand Up @@ -67,7 +71,7 @@ export function collectOrComplete<TData extends Dict<CollectData>>(cmd: Command,
if (def.required) {
return {
complete: false,
fragment: createCompletion(cmd, key, def.prompt),
fragment: createCommandCompletion(cmd, key, def.prompt),
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/listener/DiscordListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import { NotFoundError } from 'src/error/NotFoundError';
import { FetchOptions, Listener, ListenerData } from 'src/listener';
import { SessionListener } from 'src/listener/SessionListener';
import { doesExist, mustExist } from 'src/utils';
import { TYPE_TEXT } from 'src/utils/Mime';
import { createServiceCounter } from 'src/utils/metrics';
import { TYPE_TEXT } from 'src/utils/Mime';

export interface DiscordListenerData extends ListenerData {
presence?: PresenceData;
Expand Down
47 changes: 19 additions & 28 deletions src/parser/LexParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as AWS from 'aws-sdk';
import { isNil, isString, kebabCase } from 'lodash';

import { BotServiceOptions } from 'src/BotService';
import { NOUN_FRAGMENT } from 'src/controller/CompletionController';
import { createCompletion } from 'src/controller/helpers';
import { Command, CommandData, CommandDataValue, CommandOptions, CommandVerb } from 'src/entity/Command';
import { Context } from 'src/entity/Context';
import { Fragment } from 'src/entity/Fragment';
Expand All @@ -11,7 +11,6 @@ import { InvalidArgumentError } from 'src/error/InvalidArgumentError';
import { Parser, ParserData } from 'src/parser';
import { BaseParser } from 'src/parser/BaseParser';
import { doesExist, leftPad, mustExist } from 'src/utils';
import { dictToMap } from 'src/utils/Map';
import { TYPE_TEXT } from 'src/utils/Mime';
import { TemplateScope } from 'src/utils/Template';

Expand Down Expand Up @@ -87,49 +86,41 @@ export class LexParser extends BaseParser<LexParserData> implements Parser {
return [];
}

const [intent, verb] = post.intentName.split('_');
const [intent, intentVerb] = post.intentName.split('_');
const noun = kebabCase(intent);
const verb = intentVerb as CommandVerb;
const data = this.getSlots(post.slots);

this.logger.debug({ data, intent, noun, verb }, 'decoded message');
switch (post.dialogState) {
// completions
case 'ConfirmIntent':
return [];
case 'ElicitIntent':
return [];
return [createCompletion({
context,
data,
labels: this.labels,
noun,
verb,
}, 'confirm', 'please confirm', this)];
case 'ElicitSlot':
if (!isString(post.slotToElicit)) {
this.logger.warn({ body }, 'lex parsed message without slot to elicit');
return [];
}
return this.createCompletion(context, noun, verb as CommandVerb, data, post.slotToElicit);
// command
return [createCompletion({
context,
data,
labels: this.labels,
noun,
verb,
}, post.slotToElicit, 'missing field', this)];
case 'ReadyForFulfillment':
return this.createReply(context, noun, verb as CommandVerb, data);
// message
case 'Failed':
case 'Fulfilled':
return this.createReply(context, noun, verb, data);
default:
// error
this.logger.warn({ post }, 'unsupported dialog state');
return [];
}
}

protected async createCompletion(context: Context, noun: string, verb: CommandVerb, data: CommandData, key: string) {
const fragment = dictToMap({
key: [key],
msg: [`missing slot: ${key}`],
noun: [noun],
parser: [this.id],
verb: [verb],
});
return this.createReply(context, NOUN_FRAGMENT, CommandVerb.Create, new Map<string, CommandDataValue>([
...data,
...fragment,
]));
}

protected async createReply(context: Context, noun: string, verb: CommandVerb, data: CommandData): Promise<Array<Command>> {
const replyContext = await this.createContext(context);
const cmdOptions: CommandOptions = {
Expand Down

0 comments on commit edb194d

Please sign in to comment.