@telia-ace/widget-conversation 1.0.15 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +5 -5
- package/README.md +307 -256
- package/dist/agent.d.ts +17 -17
- package/dist/conversation-controller.d.ts +88 -88
- package/dist/conversation-form-adapter.d.ts +7 -7
- package/dist/conversation-platform.d.ts +50 -50
- package/dist/conversation-provider.d.ts +22 -22
- package/dist/conversation.js +783 -5
- package/dist/conversation.js.map +1 -1
- package/dist/index.d.ts +9 -9
- package/dist/index.js +868 -9
- package/dist/index.js.map +1 -1
- package/dist/resolve-entity-value.d.ts +10 -10
- package/dist/return-button.js.map +1 -1
- package/dist/types.d.ts +95 -110
- package/dist/ui/conversation/conversation-component.d.ts +37 -37
- package/dist/ui/conversation/conversation-content.d.ts +11 -12
- package/dist/ui/conversation/conversation-input.d.ts +6 -6
- package/dist/ui/conversation/conversation-loader.d.ts +8 -8
- package/dist/ui/conversation/conversation-message.d.ts +7 -7
- package/dist/ui/conversation/conversation-styles.d.ts +11 -11
- package/dist/ui/conversation/conversation.d.ts +6 -6
- package/dist/ui/conversation/index.d.ts +2 -3
- package/dist/ui/conversation/message-types/agent.d.ts +11 -11
- package/dist/ui/conversation/message-types/custom-message-list-item.d.ts +9 -9
- package/dist/ui/conversation/message-types/custom.d.ts +9 -9
- package/dist/ui/conversation/message-types/index.d.ts +5 -5
- package/dist/ui/conversation/message-types/system.d.ts +12 -12
- package/dist/ui/conversation/message-types/timestamp.d.ts +8 -8
- package/dist/ui/conversation/message-types/user.d.ts +8 -8
- package/dist/ui/index.d.ts +2 -2
- package/dist/ui/return-button/index.d.ts +2 -2
- package/dist/ui/return-button/return-button-component.d.ts +19 -19
- package/dist/ui/return-button/return-button.d.ts +6 -6
- package/dist/user.d.ts +7 -7
- package/dist/utils/custom-message.d.ts +3 -3
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/open-chat-widget.d.ts +4 -4
- package/package.json +8 -8
- package/dist/index2.js +0 -1678
- package/dist/index2.js.map +0 -1
package/LICENSE.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Copyright © 2021, Telia Company AB. All rights reserved.
|
|
2
|
-
|
|
3
|
-
THIS PACKAGE AND CONTAINING SOFTWARE IS PART OF
|
|
4
|
-
THE HUMANY SERVICE. BY USING THIS SOFTWARE YOU
|
|
5
|
-
AGREE TO THE FOLLOWING TERMS AND CONDITIONS:
|
|
1
|
+
Copyright © 2021, Telia Company AB. All rights reserved.
|
|
2
|
+
|
|
3
|
+
THIS PACKAGE AND CONTAINING SOFTWARE IS PART OF
|
|
4
|
+
THE HUMANY SERVICE. BY USING THIS SOFTWARE YOU
|
|
5
|
+
AGREE TO THE FOLLOWING TERMS AND CONDITIONS:
|
|
6
6
|
http://www.humany.com/legal
|
package/README.md
CHANGED
|
@@ -1,256 +1,307 @@
|
|
|
1
|
-
# Widget Conversation API
|
|
2
|
-
|
|
3
|
-
The Widget Conversation API provides support for reading and writing to a conversational component inside a widget.
|
|
4
|
-
|
|
5
|
-
## Explore the API
|
|
6
|
-
|
|
7
|
-
You can try out and watch the Conversation API in action by clicking the button below.
|
|
8
|
-
Keep in mind that this is for demonstration purposes only and may not be best practice.
|
|
9
|
-
We do not recommend binding functions to the `Window` object.
|
|
10
|
-
|
|
11
|
-
[](https://codesandbox.io/s/elegant-jones-rnyrd?fontsize=14&hidenavigation=1&theme=dark)
|
|
12
|
-
|
|
13
|
-
## Accessing the API
|
|
14
|
-
|
|
15
|
-
Inside a plugin, access the global instance of `ConversationPlatform` by passing in the current `Container` to the `getInstance()` method. It will return a `Promise` that is resolved to the `ConversationPlatform` instance. On the instance, register a provider and pass in a name and handler by calling the `registerProvider()` function as shown below.
|
|
16
|
-
|
|
17
|
-
The handler function will be called once a conversational component for the specified provider is activated in the widget. Use the provider to interact with the conversation.
|
|
18
|
-
|
|
19
|
-
##### Example
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
import { ConversationPlatform } from '@telia-ace/widget-conversation';
|
|
23
|
-
|
|
24
|
-
const MyPlugin = async (container) => {
|
|
25
|
-
const platform = await ConversationPlatform.getInstance(container);
|
|
26
|
-
|
|
27
|
-
platform.registerProvider('my-chat', (conversation, component) => {
|
|
28
|
-
// start interacting with the conversation here
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## Writing to the conversation
|
|
34
|
-
|
|
35
|
-
When writing content to the conversation you will first need to create an `Agent` object and then use its `print()` function. Specify the type of entry to print and pass in the content.
|
|
36
|
-
|
|
37
|
-
### `print(content: object)`
|
|
38
|
-
|
|
39
|
-
Returns a `ConversationEntry` message that can be used to update and/or remove the content from the conversation at a later stage.
|
|
40
|
-
|
|
41
|
-
##### Example
|
|
42
|
-
|
|
43
|
-
```ts
|
|
44
|
-
const agent = conversation.createAgent();
|
|
45
|
-
const entry = agent.print('Lorem ipsum');
|
|
46
|
-
|
|
47
|
-
entry.update({
|
|
48
|
-
// ...
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
entry.remove();
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### Messages
|
|
55
|
-
|
|
56
|
-
#### Simple text message
|
|
57
|
-
|
|
58
|
-
Used to render plain text without HTML-formatting.
|
|
59
|
-
|
|
60
|
-
##### Example
|
|
61
|
-
|
|
62
|
-
```ts
|
|
63
|
-
// print user message
|
|
64
|
-
conversation.user.print('Lorem ipsum');
|
|
65
|
-
|
|
66
|
-
// print agent message
|
|
67
|
-
const agent = conversation.createAgent();
|
|
68
|
-
agent.print('Lorem ipsum');
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
#### Message with list of actions
|
|
72
|
-
|
|
73
|
-
Example of how to render a list with actions. Is only supported on `Agent` and as system message.
|
|
74
|
-
|
|
75
|
-
| Name | Type | Description |
|
|
76
|
-
| --------- | -------- | ------------------------------------------------------------- |
|
|
77
|
-
| `title` | `string` | Title for the entry. |
|
|
78
|
-
| `body` | `string` | Body content for the entry. Supports HTML. |
|
|
79
|
-
| `actions` | `object` | Key-value-pairs representing available actions for the entry. |
|
|
80
|
-
|
|
81
|
-
##### Example
|
|
82
|
-
|
|
83
|
-
```ts
|
|
84
|
-
agent.print({
|
|
85
|
-
title: 'Download invoices',
|
|
86
|
-
body: 'Click an invoice below to download a copy.',
|
|
87
|
-
actions: {
|
|
88
|
-
invoice_190201: 'Invoice 190201',
|
|
89
|
-
invoice_190301: 'Invoice 190301',
|
|
90
|
-
invoice_190401: 'Invoice 190401',
|
|
91
|
-
},
|
|
92
|
-
});
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
#### Message with a form
|
|
96
|
-
|
|
97
|
-
Example of how to render a form that can be handled by the `conversation.form` event. Is only supported on `Agent` and as system message.
|
|
98
|
-
|
|
99
|
-
| Name | Type | Description |
|
|
100
|
-
| ------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
101
|
-
| `title` | `string` | Title for the entry. |
|
|
102
|
-
| `body` | `string` | Body content for the entry. Supports HTML. |
|
|
103
|
-
| `form` | `(FormBuilder) => void` | A callback function for building the form. Refer to the [`@telia-ace/widget-forms`](https://www.npmjs.com/package/@telia-ace/widget-forms) package for more information about `FormBuilder`. |
|
|
104
|
-
| `key` | `string` | The key used to refer to the form when validating and submitting the form. |
|
|
105
|
-
|
|
106
|
-
##### Example
|
|
107
|
-
|
|
108
|
-
```ts
|
|
109
|
-
agent.print({
|
|
110
|
-
title: 'Log in',
|
|
111
|
-
body: 'Enter your ID to login',
|
|
112
|
-
key: 'my-login-form',
|
|
113
|
-
form: (builder) => {
|
|
114
|
-
builder
|
|
115
|
-
.createComponent({
|
|
116
|
-
component: 'Text',
|
|
117
|
-
type: 'number',
|
|
118
|
-
name: 'id',
|
|
119
|
-
title: 'ID',
|
|
120
|
-
required: true,
|
|
121
|
-
})
|
|
122
|
-
.createComponent({
|
|
123
|
-
title: 'Log in',
|
|
124
|
-
actionKey: 'submit',
|
|
125
|
-
name: 'submit',
|
|
126
|
-
component: 'Submit',
|
|
127
|
-
type: 'submit',
|
|
128
|
-
evaluate: true,
|
|
129
|
-
value: 'Log in',
|
|
130
|
-
});
|
|
131
|
-
},
|
|
132
|
-
});
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
### Specify a sender name and/or avatar
|
|
136
|
-
|
|
137
|
-
To create an agent with a name and avatar use the `createAgent` method on the conversation.
|
|
138
|
-
|
|
139
|
-
##### Example
|
|
140
|
-
|
|
141
|
-
```ts
|
|
142
|
-
const agent = conversation.createAgent({
|
|
143
|
-
name: 'Mr. Agent',
|
|
144
|
-
avatar: 'https://www.site.com/avatar.jpg',
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
agent.print({
|
|
148
|
-
title: 'I found the following invoices associated to your account:',
|
|
149
|
-
actions: {
|
|
150
|
-
action1: 'Action 1',
|
|
151
|
-
action2: 'Action 2',
|
|
152
|
-
action3: 'Action 3',
|
|
153
|
-
},
|
|
154
|
-
});
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
### Writing system messages
|
|
158
|
-
|
|
159
|
-
In order to print a system message to the conversation you invoke the `print` method on the `conversation`. The `print` method accepts the same arguments as on the `Agent`.
|
|
160
|
-
|
|
161
|
-
#### Example
|
|
162
|
-
|
|
163
|
-
```ts
|
|
164
|
-
conversation.print('Lorem ipsum');
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
### Loading/typing indicator
|
|
168
|
-
|
|
169
|
-
In many cases you will likely fetch data from an external resource before the content is written to the conversation. In this case you should use the `loading()` function on the `ConversationProvider` to inform the user that something is about to happen. Even in cases when the response is available immediately it gives a better user experience to present a loading indicator for a short while.
|
|
170
|
-
|
|
171
|
-
In cases where you want to notify the user that the agent is currently typing, you should use the `typing` function on `Agent`.
|
|
172
|
-
|
|
173
|
-
#### Example
|
|
174
|
-
|
|
175
|
-
##### Loading
|
|
176
|
-
|
|
177
|
-
```ts
|
|
178
|
-
const done = conversation.loading();
|
|
179
|
-
// ...
|
|
180
|
-
done(); // remove loader
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
##### Typing indicator
|
|
184
|
-
|
|
185
|
-
```ts
|
|
186
|
-
const done = agent.typing();
|
|
187
|
-
// ...
|
|
188
|
-
done(); // remove loader
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
## Reading from the conversation
|
|
192
|
-
|
|
193
|
-
The second parameter to your provider handler is a `ComponentNode` instance representing the conversational component. On it you can read the component's properties and react to action emitted by the component.
|
|
194
|
-
|
|
195
|
-
### Actions
|
|
196
|
-
|
|
197
|
-
The following actions are emitted from the conversational component.
|
|
198
|
-
|
|
199
|
-
**Important**: For default actions it is important to call `next()` unless you want to completely stop the execution flow for the particular action. Not doing so will stop any succeeding handlers and may unintentionally break functionality.
|
|
200
|
-
|
|
201
|
-
#### `conversation.user-typing`
|
|
202
|
-
|
|
203
|
-
Is emitted when the user´s typing indicator is changed.
|
|
204
|
-
|
|
205
|
-
| Name | Type | Description |
|
|
206
|
-
| ------------ | -------- | ---------------------------------------------- |
|
|
207
|
-
| `textLength` | `number` | The current text length of the user's message. |
|
|
208
|
-
|
|
209
|
-
#### `conversation.user-submit`
|
|
210
|
-
|
|
211
|
-
Is emitted when the user submits a message.
|
|
212
|
-
|
|
213
|
-
| Name | Type | Description |
|
|
214
|
-
| ------ | -------- | ------------------- |
|
|
215
|
-
| `text` | `string` | The submitted text. |
|
|
216
|
-
|
|
217
|
-
#### `conversation.action`
|
|
218
|
-
|
|
219
|
-
Is emitted when the user clicks on an action.
|
|
220
|
-
|
|
221
|
-
| Name | Type | Description |
|
|
222
|
-
| ------ | -------- | ---------------------------- |
|
|
223
|
-
| `text` | `string` | Key of the submitted action. |
|
|
224
|
-
|
|
225
|
-
#### `conversation.form`
|
|
226
|
-
|
|
227
|
-
Is emitted when the data of a form is changed.
|
|
228
|
-
|
|
229
|
-
| Name | Type | Description |
|
|
230
|
-
| ----------- | ---------- | --------------------------------------------------------- |
|
|
231
|
-
| `data` | `FormData` | The form data. |
|
|
232
|
-
| `formKey` | `string` | The unique key for the form. |
|
|
233
|
-
| `actionKey` | `string` | The key of the form component responsible for the change. |
|
|
234
|
-
|
|
235
|
-
In order to subscribe to any of these actions you have access to the `ComponentNodeController`. Below is an example how to subscribe to the `conversation.form` action.
|
|
236
|
-
|
|
237
|
-
```ts
|
|
238
|
-
component.actions.watch('conversation.form', (input, next) => {
|
|
239
|
-
return next(input);
|
|
240
|
-
});
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
### Submitting forms
|
|
244
|
-
|
|
245
|
-
In order to listen for form events you should subscribe to the `conversation.form` event. By passing a `key` to the form, you are able to target the form in this listener.
|
|
246
|
-
|
|
247
|
-
```ts
|
|
248
|
-
component.actions.watch('conversation.form', (input, next) => {
|
|
249
|
-
if (input.formKey === 'my-login-form' && input.actionKey === 'submit') {
|
|
250
|
-
const username = input.data.username;
|
|
251
|
-
const password = input.data.password;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
return next(input);
|
|
255
|
-
});
|
|
256
|
-
```
|
|
1
|
+
# Widget Conversation API
|
|
2
|
+
|
|
3
|
+
The Widget Conversation API provides support for reading and writing to a conversational component inside a widget.
|
|
4
|
+
|
|
5
|
+
## Explore the API
|
|
6
|
+
|
|
7
|
+
You can try out and watch the Conversation API in action by clicking the button below.
|
|
8
|
+
Keep in mind that this is for demonstration purposes only and may not be best practice.
|
|
9
|
+
We do not recommend binding functions to the `Window` object.
|
|
10
|
+
|
|
11
|
+
[](https://codesandbox.io/s/elegant-jones-rnyrd?fontsize=14&hidenavigation=1&theme=dark)
|
|
12
|
+
|
|
13
|
+
## Accessing the API
|
|
14
|
+
|
|
15
|
+
Inside a plugin, access the global instance of `ConversationPlatform` by passing in the current `Container` to the `getInstance()` method. It will return a `Promise` that is resolved to the `ConversationPlatform` instance. On the instance, register a provider and pass in a name and handler by calling the `registerProvider()` function as shown below.
|
|
16
|
+
|
|
17
|
+
The handler function will be called once a conversational component for the specified provider is activated in the widget. Use the provider to interact with the conversation.
|
|
18
|
+
|
|
19
|
+
##### Example
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { ConversationPlatform } from '@telia-ace/widget-conversation';
|
|
23
|
+
|
|
24
|
+
const MyPlugin = async (container) => {
|
|
25
|
+
const platform = await ConversationPlatform.getInstance(container);
|
|
26
|
+
|
|
27
|
+
platform.registerProvider('my-chat', (conversation, component) => {
|
|
28
|
+
// start interacting with the conversation here
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Writing to the conversation
|
|
34
|
+
|
|
35
|
+
When writing content to the conversation you will first need to create an `Agent` object and then use its `print()` function. Specify the type of entry to print and pass in the content.
|
|
36
|
+
|
|
37
|
+
### `print(content: object)`
|
|
38
|
+
|
|
39
|
+
Returns a `ConversationEntry` message that can be used to update and/or remove the content from the conversation at a later stage.
|
|
40
|
+
|
|
41
|
+
##### Example
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
const agent = conversation.createAgent();
|
|
45
|
+
const entry = agent.print('Lorem ipsum');
|
|
46
|
+
|
|
47
|
+
entry.update({
|
|
48
|
+
// ...
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
entry.remove();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Messages
|
|
55
|
+
|
|
56
|
+
#### Simple text message
|
|
57
|
+
|
|
58
|
+
Used to render plain text without HTML-formatting.
|
|
59
|
+
|
|
60
|
+
##### Example
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
// print user message
|
|
64
|
+
conversation.user.print('Lorem ipsum');
|
|
65
|
+
|
|
66
|
+
// print agent message
|
|
67
|
+
const agent = conversation.createAgent();
|
|
68
|
+
agent.print('Lorem ipsum');
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### Message with list of actions
|
|
72
|
+
|
|
73
|
+
Example of how to render a list with actions. Is only supported on `Agent` and as system message.
|
|
74
|
+
|
|
75
|
+
| Name | Type | Description |
|
|
76
|
+
| --------- | -------- | ------------------------------------------------------------- |
|
|
77
|
+
| `title` | `string` | Title for the entry. |
|
|
78
|
+
| `body` | `string` | Body content for the entry. Supports HTML. |
|
|
79
|
+
| `actions` | `object` | Key-value-pairs representing available actions for the entry. |
|
|
80
|
+
|
|
81
|
+
##### Example
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
agent.print({
|
|
85
|
+
title: 'Download invoices',
|
|
86
|
+
body: 'Click an invoice below to download a copy.',
|
|
87
|
+
actions: {
|
|
88
|
+
invoice_190201: 'Invoice 190201',
|
|
89
|
+
invoice_190301: 'Invoice 190301',
|
|
90
|
+
invoice_190401: 'Invoice 190401',
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### Message with a form
|
|
96
|
+
|
|
97
|
+
Example of how to render a form that can be handled by the `conversation.form` event. Is only supported on `Agent` and as system message.
|
|
98
|
+
|
|
99
|
+
| Name | Type | Description |
|
|
100
|
+
| ------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
101
|
+
| `title` | `string` | Title for the entry. |
|
|
102
|
+
| `body` | `string` | Body content for the entry. Supports HTML. |
|
|
103
|
+
| `form` | `(FormBuilder) => void` | A callback function for building the form. Refer to the [`@telia-ace/widget-forms`](https://www.npmjs.com/package/@telia-ace/widget-forms) package for more information about `FormBuilder`. |
|
|
104
|
+
| `key` | `string` | The key used to refer to the form when validating and submitting the form. |
|
|
105
|
+
|
|
106
|
+
##### Example
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
agent.print({
|
|
110
|
+
title: 'Log in',
|
|
111
|
+
body: 'Enter your ID to login',
|
|
112
|
+
key: 'my-login-form',
|
|
113
|
+
form: (builder) => {
|
|
114
|
+
builder
|
|
115
|
+
.createComponent({
|
|
116
|
+
component: 'Text',
|
|
117
|
+
type: 'number',
|
|
118
|
+
name: 'id',
|
|
119
|
+
title: 'ID',
|
|
120
|
+
required: true,
|
|
121
|
+
})
|
|
122
|
+
.createComponent({
|
|
123
|
+
title: 'Log in',
|
|
124
|
+
actionKey: 'submit',
|
|
125
|
+
name: 'submit',
|
|
126
|
+
component: 'Submit',
|
|
127
|
+
type: 'submit',
|
|
128
|
+
evaluate: true,
|
|
129
|
+
value: 'Log in',
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Specify a sender name and/or avatar
|
|
136
|
+
|
|
137
|
+
To create an agent with a name and avatar use the `createAgent` method on the conversation.
|
|
138
|
+
|
|
139
|
+
##### Example
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
const agent = conversation.createAgent({
|
|
143
|
+
name: 'Mr. Agent',
|
|
144
|
+
avatar: 'https://www.site.com/avatar.jpg',
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
agent.print({
|
|
148
|
+
title: 'I found the following invoices associated to your account:',
|
|
149
|
+
actions: {
|
|
150
|
+
action1: 'Action 1',
|
|
151
|
+
action2: 'Action 2',
|
|
152
|
+
action3: 'Action 3',
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Writing system messages
|
|
158
|
+
|
|
159
|
+
In order to print a system message to the conversation you invoke the `print` method on the `conversation`. The `print` method accepts the same arguments as on the `Agent`.
|
|
160
|
+
|
|
161
|
+
#### Example
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
conversation.print('Lorem ipsum');
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Loading/typing indicator
|
|
168
|
+
|
|
169
|
+
In many cases you will likely fetch data from an external resource before the content is written to the conversation. In this case you should use the `loading()` function on the `ConversationProvider` to inform the user that something is about to happen. Even in cases when the response is available immediately it gives a better user experience to present a loading indicator for a short while.
|
|
170
|
+
|
|
171
|
+
In cases where you want to notify the user that the agent is currently typing, you should use the `typing` function on `Agent`.
|
|
172
|
+
|
|
173
|
+
#### Example
|
|
174
|
+
|
|
175
|
+
##### Loading
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
const done = conversation.loading();
|
|
179
|
+
// ...
|
|
180
|
+
done(); // remove loader
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
##### Typing indicator
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
const done = agent.typing();
|
|
187
|
+
// ...
|
|
188
|
+
done(); // remove loader
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Reading from the conversation
|
|
192
|
+
|
|
193
|
+
The second parameter to your provider handler is a `ComponentNode` instance representing the conversational component. On it you can read the component's properties and react to action emitted by the component.
|
|
194
|
+
|
|
195
|
+
### Actions
|
|
196
|
+
|
|
197
|
+
The following actions are emitted from the conversational component.
|
|
198
|
+
|
|
199
|
+
**Important**: For default actions it is important to call `next()` unless you want to completely stop the execution flow for the particular action. Not doing so will stop any succeeding handlers and may unintentionally break functionality.
|
|
200
|
+
|
|
201
|
+
#### `conversation.user-typing`
|
|
202
|
+
|
|
203
|
+
Is emitted when the user´s typing indicator is changed.
|
|
204
|
+
|
|
205
|
+
| Name | Type | Description |
|
|
206
|
+
| ------------ | -------- | ---------------------------------------------- |
|
|
207
|
+
| `textLength` | `number` | The current text length of the user's message. |
|
|
208
|
+
|
|
209
|
+
#### `conversation.user-submit`
|
|
210
|
+
|
|
211
|
+
Is emitted when the user submits a message.
|
|
212
|
+
|
|
213
|
+
| Name | Type | Description |
|
|
214
|
+
| ------ | -------- | ------------------- |
|
|
215
|
+
| `text` | `string` | The submitted text. |
|
|
216
|
+
|
|
217
|
+
#### `conversation.action`
|
|
218
|
+
|
|
219
|
+
Is emitted when the user clicks on an action.
|
|
220
|
+
|
|
221
|
+
| Name | Type | Description |
|
|
222
|
+
| ------ | -------- | ---------------------------- |
|
|
223
|
+
| `text` | `string` | Key of the submitted action. |
|
|
224
|
+
|
|
225
|
+
#### `conversation.form`
|
|
226
|
+
|
|
227
|
+
Is emitted when the data of a form is changed.
|
|
228
|
+
|
|
229
|
+
| Name | Type | Description |
|
|
230
|
+
| ----------- | ---------- | --------------------------------------------------------- |
|
|
231
|
+
| `data` | `FormData` | The form data. |
|
|
232
|
+
| `formKey` | `string` | The unique key for the form. |
|
|
233
|
+
| `actionKey` | `string` | The key of the form component responsible for the change. |
|
|
234
|
+
|
|
235
|
+
In order to subscribe to any of these actions you have access to the `ComponentNodeController`. Below is an example how to subscribe to the `conversation.form` action.
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
component.actions.watch('conversation.form', (input, next) => {
|
|
239
|
+
return next(input);
|
|
240
|
+
});
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Submitting forms
|
|
244
|
+
|
|
245
|
+
In order to listen for form events you should subscribe to the `conversation.form` event. By passing a `key` to the form, you are able to target the form in this listener.
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
component.actions.watch('conversation.form', (input, next) => {
|
|
249
|
+
if (input.formKey === 'my-login-form' && input.actionKey === 'submit') {
|
|
250
|
+
const username = input.data.username;
|
|
251
|
+
const password = input.data.password;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return next(input);
|
|
255
|
+
});
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Adding custom list items
|
|
259
|
+
|
|
260
|
+
In order to add custom list items to the conversation you may register your own custom React component, which will be mapped to a key also provided by you.
|
|
261
|
+
|
|
262
|
+
You register the component like below:
|
|
263
|
+
|
|
264
|
+
```ts
|
|
265
|
+
const platform = await ConversationPlatform.getInstance(container);
|
|
266
|
+
|
|
267
|
+
platform.registerConversationItem('my-custom-list-item', import('./my-custom-list-item'));
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Once the component has been registered it will be available to be used as any other item in the `print` method on the Conversation API.
|
|
271
|
+
|
|
272
|
+
### Secondary action
|
|
273
|
+
|
|
274
|
+
You may add a secondary action next to the user input field, which will fire a custom event that you provide.
|
|
275
|
+
|
|
276
|
+
In order to accomplish this you need to add the `secondaryAction` property to the conversation component in the configuration.
|
|
277
|
+
It accepts an object with the following properties:
|
|
278
|
+
|
|
279
|
+
| Name | Type | Description |
|
|
280
|
+
| -------- | -------- | ------------------------------------------------------------------- |
|
|
281
|
+
| `action` | `string` | An action that will be dispatched when the button is pressed |
|
|
282
|
+
| `label` | `string` | Will be used as a "title" and aria-label on the HTML button element |
|
|
283
|
+
| `icon` | `string` | An icon to be displayed, defaults to "browse" icon if left empty |
|
|
284
|
+
|
|
285
|
+
```json
|
|
286
|
+
"conversation": {
|
|
287
|
+
"type": "conversation",
|
|
288
|
+
"properties": {
|
|
289
|
+
"secondaryAction": {
|
|
290
|
+
"action": "my-secondary-action",
|
|
291
|
+
"label": "Label for the secondary action",
|
|
292
|
+
"icon": "my-icon"
|
|
293
|
+
}
|
|
294
|
+
// ...other properties
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
When you have added this property to the component, you may listen to it in your code.
|
|
300
|
+
|
|
301
|
+
```ts
|
|
302
|
+
component.actions.watch('conversation.action', (input, next) => {
|
|
303
|
+
if (input === 'my-secondary-action') {
|
|
304
|
+
// handle action
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
```
|
package/dist/agent.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { Symbol } from '@telia-ace/widget-ui';
|
|
2
|
-
import { ConversationEntry } from './conversation-controller';
|
|
3
|
-
import { ConversationProvider } from './conversation-provider';
|
|
4
|
-
import { ConversationMessageSettings, ConverstationMessageItem } from './types';
|
|
5
|
-
export declare type AgentSettings = {
|
|
6
|
-
name?: string;
|
|
7
|
-
avatar?: string | Symbol;
|
|
8
|
-
};
|
|
9
|
-
export declare class Agent {
|
|
10
|
-
private provider;
|
|
11
|
-
name?: string;
|
|
12
|
-
avatar?: string | Symbol;
|
|
13
|
-
constructor(provider: ConversationProvider, settings: AgentSettings);
|
|
14
|
-
print: (settings: string | ConversationMessageSettings | ConverstationMessageItem<any> | ConverstationMessageItem<any>[]) => ConversationEntry | undefined;
|
|
15
|
-
typing: () => (() => void) | undefined;
|
|
16
|
-
update(settings: AgentSettings): void;
|
|
17
|
-
}
|
|
1
|
+
import { Symbol } from '@telia-ace/widget-ui';
|
|
2
|
+
import { ConversationEntry } from './conversation-controller';
|
|
3
|
+
import { ConversationProvider } from './conversation-provider';
|
|
4
|
+
import { ConversationMessageSettings, ConverstationMessageItem } from './types';
|
|
5
|
+
export declare type AgentSettings = {
|
|
6
|
+
name?: string;
|
|
7
|
+
avatar?: string | Symbol;
|
|
8
|
+
};
|
|
9
|
+
export declare class Agent {
|
|
10
|
+
private provider;
|
|
11
|
+
name?: string;
|
|
12
|
+
avatar?: string | Symbol;
|
|
13
|
+
constructor(provider: ConversationProvider, settings: AgentSettings);
|
|
14
|
+
print: (settings: string | ConversationMessageSettings | ConverstationMessageItem<any> | ConverstationMessageItem<any>[]) => ConversationEntry | undefined;
|
|
15
|
+
typing: () => (() => void) | undefined;
|
|
16
|
+
update(settings: AgentSettings): void;
|
|
17
|
+
}
|