botbuilder-core
Version:
Core components for Microsoft Bot Builder. Components in this library can run either in a browser or on the server.
59 lines • 2.94 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.SkypeMentionNormalizeMiddleware = void 0;
/**
* Middleware to patch mention Entities from Skype since they don't conform to expected values.
* Bots that interact with Skype should use this middleware if mentions are used.
*
* @remarks
* A Skype mention "text" field is of the format:
* <at id="28:2bc5b54d-5d48-4ff1-bd25-03dcbb5ce918">botname</at>
* But Activity.Text doesn't contain those tags and RemoveMentionText can't remove
* the entity from Activity.Text.
* This will remove the <at> nodes, leaving just the name.
*/
class SkypeMentionNormalizeMiddleware {
/**
* Performs the normalization of Skype mention Entities.
*
* @param activity [Activity](xref:botframework-schema.Activity) containing the mentions to normalize.
*/
static normalizeSkypeMentionText(activity) {
if (activity.channelId === 'skype' && activity.type === 'message') {
activity.entities.map((element) => {
if (element.type === 'mention') {
const text = element['text'];
const end = text.indexOf('>');
if (end > -1) {
const start = text.indexOf('<', end);
if (start > -1)
element['text'] = text.substring(end + 1, start);
}
}
});
}
}
/**
* Middleware implementation which corrects the Entity text of type [Mention](xref:botframework-schema.Mention) to a value that [removeMentionText](xref:botbuilder-core.TurnContext.removeMentionText) can work with.
*
* @param turnContext [TurnContext](xref:botbuilder-core.TurnContext) for the current turn of conversation.
* @param next Delegate to call to continue the bot middleware pipeline.
*/
onTurn(turnContext, next) {
return __awaiter(this, void 0, void 0, function* () {
SkypeMentionNormalizeMiddleware.normalizeSkypeMentionText(turnContext.activity);
yield next();
});
}
}
exports.SkypeMentionNormalizeMiddleware = SkypeMentionNormalizeMiddleware;
//# sourceMappingURL=skypeMentionNormalizeMiddleware.js.map
;