UNPKG

4.05 kBPlain TextView Raw
1/*
2 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5 * the License. A copy of the License is located at
6 *
7 * http://aws.amazon.com/apache2.0/
8 *
9 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11 * and limitations under the License.
12 */
13import {
14 InteractionsOptions,
15 InteractionsProviders,
16 InteractionsResponse,
17 InteractionsProvider,
18} from './types';
19import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
20import { AWSLexProvider } from './Providers';
21const logger = new Logger('Interactions');
22
23export class InteractionsClass {
24 private _options: InteractionsOptions;
25
26 private _pluggables: InteractionsProviders;
27
28 /**
29 * Initialize PubSub with AWS configurations
30 *
31 * @param {InteractionsOptions} options - Configuration object for Interactions
32 */
33 constructor(options: InteractionsOptions) {
34 this._options = options;
35 logger.debug('Interactions Options', this._options);
36 this._pluggables = {};
37 Amplify.register(this);
38 }
39
40 public getModuleName() {
41 return 'Interactions';
42 }
43
44 /**
45 *
46 * @param {InteractionsOptions} options - Configuration object for Interactions
47 * @return {Object} - The current configuration
48 */
49 configure(options: InteractionsOptions) {
50 const opt = options ? options.Interactions || options : {};
51 logger.debug('configure Interactions', { opt });
52 this._options = { bots: {}, ...opt, ...opt.Interactions };
53
54 const aws_bots_config = this._options.aws_bots_config;
55 const bots_config = this._options.bots;
56
57 if (!Object.keys(bots_config).length && aws_bots_config) {
58 // Convert aws_bots_config to bots object
59 if (Array.isArray(aws_bots_config)) {
60 aws_bots_config.forEach(bot => {
61 this._options.bots[bot.name] = bot;
62 });
63 }
64 }
65
66 // Check if AWSLex provider is already on pluggables
67 if (
68 !this._pluggables.AWSLexProvider &&
69 bots_config &&
70 Object.keys(bots_config)
71 .map(key => bots_config[key])
72 .find(bot => !bot.providerName || bot.providerName === 'AWSLexProvider')
73 ) {
74 this._pluggables.AWSLexProvider = new AWSLexProvider();
75 }
76
77 Object.keys(this._pluggables).map(key => {
78 this._pluggables[key].configure(this._options.bots);
79 });
80
81 return this._options;
82 }
83
84 public addPluggable(pluggable: InteractionsProvider) {
85 if (pluggable && pluggable.getCategory() === 'Interactions') {
86 if (!this._pluggables[pluggable.getProviderName()]) {
87 pluggable.configure(this._options.bots);
88 this._pluggables[pluggable.getProviderName()] = pluggable;
89 return;
90 } else {
91 throw new Error(
92 'Bot ' + pluggable.getProviderName() + ' already plugged'
93 );
94 }
95 }
96 }
97
98 public async send(botname: string, message: string | Object) {
99 if (!this._options.bots || !this._options.bots[botname]) {
100 throw new Error('Bot ' + botname + ' does not exist');
101 }
102
103 const botProvider =
104 this._options.bots[botname].providerName || 'AWSLexProvider';
105
106 if (!this._pluggables[botProvider]) {
107 throw new Error(
108 'Bot ' +
109 botProvider +
110 ' does not have valid pluggin did you try addPluggable first?'
111 );
112 }
113 return await this._pluggables[botProvider].sendMessage(botname, message);
114 }
115
116 public onComplete(botname: string, callback: (err, confirmation) => void) {
117 if (!this._options.bots || !this._options.bots[botname]) {
118 throw new Error('Bot ' + botname + ' does not exist');
119 }
120 const botProvider =
121 this._options.bots[botname].providerName || 'AWSLexProvider';
122
123 if (!this._pluggables[botProvider]) {
124 throw new Error(
125 'Bot ' +
126 botProvider +
127 ' does not have valid pluggin did you try addPluggable first?'
128 );
129 }
130 this._pluggables[botProvider].onComplete(botname, callback);
131 }
132}
133
134export const Interactions = new InteractionsClass(null);