1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | import {
|
14 | InteractionsOptions,
|
15 | InteractionsProviders,
|
16 | InteractionsProvider,
|
17 | InteractionsMessage,
|
18 | InteractionsResponse,
|
19 | } from './types';
|
20 | import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
|
21 | import { AWSLexProvider } from './Providers';
|
22 | const logger = new Logger('Interactions');
|
23 |
|
24 | export class InteractionsClass {
|
25 | private _options: InteractionsOptions;
|
26 |
|
27 | private _pluggables: InteractionsProviders;
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
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 |
|
47 |
|
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 |
|
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 |
|
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 |
|
149 | export const Interactions = new InteractionsClass(null);
|
150 | Amplify.register(Interactions);
|