@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
45 lines • 2.23 kB
JavaScript
import { __awaiter } from "tslib";
import { Button, Callback, ContentType, PagingOptions, } from '../cms';
import { checkLocale } from '../nlp';
import { SearchByKeywords } from './search-by-keywords';
export class Search {
constructor(cms, normalizer, keywordsOptions) {
this.cms = cms;
this.normalizer = normalizer;
this.search = new SearchByKeywords(cms, normalizer, keywordsOptions);
}
/**
* It does not sort the results based on the {@link SearchResult.priority}.
* @param context must contain language
*/
searchByKeywords(inputText, matchType, context, paging = new PagingOptions()) {
return __awaiter(this, void 0, void 0, function* () {
const locale = checkLocale(context.locale);
const utterance = this.normalizer.normalize(locale, inputText);
const results = yield this.search.searchContentsFromInput(utterance, matchType, context, paging);
return this.search.filterChitchat(utterance.words, results);
});
}
respondFoundContents(results, confirmKeywordsFoundTextId, noKeywordsFoundTextId, context) {
return __awaiter(this, void 0, void 0, function* () {
if (results.length == 0) {
return this.cms.text(noKeywordsFoundTextId, context);
}
const chitchatCallback = results[0].getCallbackIfChitchat();
if (chitchatCallback) {
return this.cms.chitchat(chitchatCallback.id, context);
}
const buttonPromises = results.map((result) => __awaiter(this, void 0, void 0, function* () {
if (result.contentId.model == ContentType.URL) {
const url = yield this.cms.url(result.contentId.id, context);
return new Button(result.common.id, result.common.name, result.common.shortText, Callback.ofUrl(url.url));
}
return result.toButton();
}));
const buttons = yield Promise.all(buttonPromises);
const text = yield this.cms.text(confirmKeywordsFoundTextId, context);
return text.cloneWithButtons(buttons);
});
}
}
//# sourceMappingURL=search.js.map