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
 */
13
import {
14
	InteractionsOptions,
15
	InteractionsProviders,
16
	InteractionsProvider,
17
	InteractionsMessage,
18
	InteractionsResponse,
19
} from './types';
20 1
import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
21 1
import { AWSLexProvider } from './Providers';
22 1
const logger = new Logger('Interactions');
23

24 1
export 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 1
		this._options = options;
36 1
		logger.debug('Interactions Options', this._options);
37 1
		this._pluggables = {};
38
	}
39

40 1
	public getModuleName() {
41 1
		return 'Interactions';
42
	}
43

44
	/**
45
	 *
46
	 * @param {InteractionsOptions} options - Configuration object for Interactions
47
	 * @return {Object} - The current configuration
48
	 */
49 1
	configure(options: InteractionsOptions) {
50 1
		const opt = options ? options.Interactions || options : {};
51 1
		logger.debug('configure Interactions', { opt });
52 1
		this._options = { bots: {}, ...opt, ...opt.Interactions };
53

54 1
		const aws_bots_config = this._options.aws_bots_config;
55 1
		const bots_config = this._options.bots;
56

57 1
		if (!Object.keys(bots_config).length && aws_bots_config) {
58
			// Convert aws_bots_config to bots object
59 1
			if (Array.isArray(aws_bots_config)) {
60 1
				aws_bots_config.forEach(bot => {
61 1
					this._options.bots[bot.name] = bot;
62
				});
63
			}
64
		}
65

66
		// Check if AWSLex provider is already on pluggables
67 1
		if (
68 1
			!this._pluggables.AWSLexProvider &&
69
			bots_config &&
70
			Object.keys(bots_config)
71 1
				.map(key => bots_config[key])
72 1
				.find(bot => !bot.providerName || bot.providerName === 'AWSLexProvider')
73
		) {
74 1
			this._pluggables.AWSLexProvider = new AWSLexProvider();
75
		}
76

77 1
		Object.keys(this._pluggables).map(key => {
78 1
			this._pluggables[key].configure(this._options.bots);
79
		});
80

81 1
		return this._options;
82
	}
83

84 1
	public addPluggable(pluggable: InteractionsProvider) {
85 1
		if (pluggable && pluggable.getCategory() === 'Interactions') {
86 1
			if (!this._pluggables[pluggable.getProviderName()]) {
87 1
				pluggable.configure(this._options.bots);
88 1
				this._pluggables[pluggable.getProviderName()] = pluggable;
89 1
				return;
90
			} else {
91 0
				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 1
	public async send(
111
		botname: string,
112
		message: string | object
113
	): Promise<InteractionsResponse> {
114 1
		if (!this._options.bots || !this._options.bots[botname]) {
115 1
			throw new Error('Bot ' + botname + ' does not exist');
116
		}
117

118 1
		const botProvider =
119 1
			this._options.bots[botname].providerName || 'AWSLexProvider';
120

121 1
		if (!this._pluggables[botProvider]) {
122 0
			throw new Error(
123
				'Bot ' +
124
					botProvider +
125
					' does not have valid pluggin did you try addPluggable first?'
126
			);
127
		}
128 1
		return await this._pluggables[botProvider].sendMessage(botname, message);
129
	}
130

131 1
	public onComplete(botname: string, callback: (err, confirmation) => void) {
132 1
		if (!this._options.bots || !this._options.bots[botname]) {
133 1
			throw new Error('Bot ' + botname + ' does not exist');
134
		}
135
		const botProvider =
136 1
			this._options.bots[botname].providerName || 'AWSLexProvider';
137

138 1
		if (!this._pluggables[botProvider]) {
139 0
			throw new Error(
140
				'Bot ' +
141
					botProvider +
142
					' does not have valid pluggin did you try addPluggable first?'
143
			);
144
		}
145 1
		this._pluggables[botProvider].onComplete(botname, callback);
146
	}
147 1
}
148

149 1
export const Interactions = new InteractionsClass(null);
150 1
Amplify.register(Interactions);

Read our documentation on viewing source code .

Loading