UNPKG

5.49 kBJavaScriptView Raw
1import { __assign, __awaiter, __generator } from "tslib";
2import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
3import { AWSLexProvider } from './Providers';
4var logger = new Logger('Interactions');
5var InteractionsClass = /** @class */ (function () {
6 /**
7 * Initialize PubSub with AWS configurations
8 *
9 * @param {InteractionsOptions} options - Configuration object for Interactions
10 */
11 function InteractionsClass(options) {
12 if (options === void 0) { options = {}; }
13 this._options = options;
14 logger.debug('Interactions Options', this._options);
15 this._pluggables = {};
16 }
17 InteractionsClass.prototype.getModuleName = function () {
18 return 'Interactions';
19 };
20 /**
21 *
22 * @param {InteractionsOptions} options - Configuration object for Interactions
23 * @return {InteractionsOptions} - The current configuration
24 */
25 InteractionsClass.prototype.configure = function (options) {
26 var _this = this;
27 var opt = options ? options.Interactions || options : {};
28 logger.debug('configure Interactions', { opt: opt });
29 this._options = __assign(__assign({ bots: {} }, opt), opt.Interactions);
30 var aws_bots_config = this._options.aws_bots_config;
31 var bots_config = this._options.bots;
32 if (!Object.keys(bots_config).length && aws_bots_config) {
33 // Convert aws_bots_config to bots object
34 if (Array.isArray(aws_bots_config)) {
35 aws_bots_config.forEach(function (bot) {
36 _this._options.bots[bot.name] = bot;
37 });
38 }
39 }
40 // configure bots to their specific providers
41 Object.keys(bots_config).forEach(function (botKey) {
42 var _a;
43 var bot = bots_config[botKey];
44 var providerName = bot.providerName || 'AWSLexProvider';
45 // add default provider if required
46 if (!_this._pluggables.AWSLexProvider &&
47 providerName === 'AWSLexProvider') {
48 _this._pluggables.AWSLexProvider = new AWSLexProvider();
49 }
50 // configure bot with it's respective provider
51 if (_this._pluggables[providerName]) {
52 _this._pluggables[providerName].configure((_a = {}, _a[bot.name] = bot, _a));
53 }
54 else {
55 logger.debug("bot " + bot.name + " was not configured as " + providerName + " provider was not found");
56 }
57 });
58 return this._options;
59 };
60 InteractionsClass.prototype.addPluggable = function (pluggable) {
61 var _this = this;
62 if (pluggable && pluggable.getCategory() === 'Interactions') {
63 if (!this._pluggables[pluggable.getProviderName()]) {
64 // configure bots for the new plugin
65 Object.keys(this._options.bots)
66 .filter(function (botKey) {
67 return _this._options.bots[botKey].providerName ===
68 pluggable.getProviderName();
69 })
70 .forEach(function (botKey) {
71 var _a;
72 var bot = _this._options.bots[botKey];
73 pluggable.configure((_a = {}, _a[bot.name] = bot, _a));
74 });
75 this._pluggables[pluggable.getProviderName()] = pluggable;
76 return;
77 }
78 else {
79 throw new Error('Pluggable ' + pluggable.getProviderName() + ' already plugged');
80 }
81 }
82 };
83 InteractionsClass.prototype.send = function (botname, message) {
84 return __awaiter(this, void 0, void 0, function () {
85 var botProvider;
86 return __generator(this, function (_a) {
87 switch (_a.label) {
88 case 0:
89 if (!this._options.bots || !this._options.bots[botname]) {
90 return [2 /*return*/, Promise.reject('Bot ' + botname + ' does not exist')];
91 }
92 botProvider = this._options.bots[botname].providerName || 'AWSLexProvider';
93 if (!this._pluggables[botProvider]) {
94 return [2 /*return*/, Promise.reject('Bot ' +
95 botProvider +
96 ' does not have valid pluggin did you try addPluggable first?')];
97 }
98 return [4 /*yield*/, this._pluggables[botProvider].sendMessage(botname, message)];
99 case 1: return [2 /*return*/, _a.sent()];
100 }
101 });
102 });
103 };
104 InteractionsClass.prototype.onComplete = function (botname, callback) {
105 if (!this._options.bots || !this._options.bots[botname]) {
106 throw new Error('Bot ' + botname + ' does not exist');
107 }
108 var botProvider = this._options.bots[botname].providerName || 'AWSLexProvider';
109 if (!this._pluggables[botProvider]) {
110 throw new Error('Bot ' +
111 botProvider +
112 ' does not have valid pluggin did you try addPluggable first?');
113 }
114 this._pluggables[botProvider].onComplete(botname, callback);
115 };
116 return InteractionsClass;
117}());
118export { InteractionsClass };
119export var Interactions = new InteractionsClass();
120Amplify.register(Interactions);
121//# sourceMappingURL=Interactions.js.map
\No newline at end of file