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