botbuilder-dialogs-adaptive
Version:
Rule system for the Microsoft BotBuilder dialog system.
73 lines • 3.55 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AttachmentInput = exports.AttachmentOutputFormat = void 0;
const adaptive_expressions_1 = require("adaptive-expressions");
const inputDialog_1 = require("./inputDialog");
var AttachmentOutputFormat;
(function (AttachmentOutputFormat) {
AttachmentOutputFormat["all"] = "all";
AttachmentOutputFormat["first"] = "first";
})(AttachmentOutputFormat = exports.AttachmentOutputFormat || (exports.AttachmentOutputFormat = {}));
/**
* Input dialog which prompts the user to send a file.
*/
class AttachmentInput extends inputDialog_1.InputDialog {
constructor() {
super(...arguments);
this.outputFormat = new adaptive_expressions_1.EnumExpression(AttachmentOutputFormat.first);
}
/**
* @param property The key of the conditional selector configuration.
* @returns The converter for the selector configuration.
*/
getConverter(property) {
switch (property) {
case 'outputFormat':
return new adaptive_expressions_1.EnumExpressionConverter(AttachmentOutputFormat);
default:
return super.getConverter(property);
}
}
onComputeId() {
return `AttachmentInput[${this.prompt && this.prompt.toString()}]`;
}
/**
* @protected
* Called when input has been received.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @returns [InputState](xref:botbuilder-dialogs-adaptive.InputState) which reflects whether input was recognized as valid or not.
*/
onRecognizeInput(dc) {
return __awaiter(this, void 0, void 0, function* () {
// Recognize input and filter out non-attachments
const input = dc.state.getValue(inputDialog_1.InputDialog.VALUE_PROPERTY);
const attachments = Array.isArray(input) ? input : [input];
const first = attachments.length > 0 ? attachments[0] : undefined;
if (typeof first != 'object' || (!first.contentUrl && !first.content)) {
return inputDialog_1.InputState.unrecognized;
}
// Format output and return success
switch (this.outputFormat.getValue(dc.state)) {
case AttachmentOutputFormat.all:
dc.state.setValue(inputDialog_1.InputDialog.VALUE_PROPERTY, attachments);
break;
case AttachmentOutputFormat.first:
dc.state.setValue(inputDialog_1.InputDialog.VALUE_PROPERTY, first);
break;
}
return inputDialog_1.InputState.valid;
});
}
}
exports.AttachmentInput = AttachmentInput;
AttachmentInput.$kind = 'Microsoft.AttachmentInput';
//# sourceMappingURL=attachmentInput.js.map