UNPKG

4.41 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 InteractionsProvider,
17 InteractionsMessage,
18 InteractionsResponse,
19} from './types';
20import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
21import { AWSLexProvider } from './Providers';
22const logger = new Logger('Interactions');
23
24export class InteractionsClass {
25 private _options: InteractionsOptions;
26
27 private _pluggables: InteractionsProviders;
28
29 /**
30 * Initialize PubSub with AWS configurations
31 *
32 * @param {InteractionsOptions} options - Configuration object for Interactions
33 */
34 constructor(options: InteractionsOptions) {
35 this._options = options;
36 logger.debug('Interactions Options', this._options);
37 this._pluggables = {};
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(
99 botname: string,
100 message: string
101 ): Promise<InteractionsResponse>;
102 public async send(
103 botname: string,
104 message: InteractionsMessage
105 ): Promise<InteractionsResponse>;
106 public async send(
107 botname: string,
108 message: object
109 ): Promise<InteractionsResponse>;
110 public async send(
111 botname: string,
112 message: string | object
113 ): Promise<InteractionsResponse> {
114 if (!this._options.bots || !this._options.bots[botname]) {
115 throw new Error('Bot ' + botname + ' does not exist');
116 }
117
118 const botProvider =
119 this._options.bots[botname].providerName || 'AWSLexProvider';
120
121 if (!this._pluggables[botProvider]) {
122 throw new Error(
123 'Bot ' +
124 botProvider +
125 ' does not have valid pluggin did you try addPluggable first?'
126 );
127 }
128 return await this._pluggables[botProvider].sendMessage(botname, message);
129 }
130
131 public onComplete(botname: string, callback: (err, confirmation) => void) {
132 if (!this._options.bots || !this._options.bots[botname]) {
133 throw new Error('Bot ' + botname + ' does not exist');
134 }
135 const botProvider =
136 this._options.bots[botname].providerName || 'AWSLexProvider';
137
138 if (!this._pluggables[botProvider]) {
139 throw new Error(
140 'Bot ' +
141 botProvider +
142 ' does not have valid pluggin did you try addPluggable first?'
143 );
144 }
145 this._pluggables[botProvider].onComplete(botname, callback);
146 }
147}
148
149export const Interactions = new InteractionsClass(null);
150Amplify.register(Interactions);