blip-cards 3.55.1 → 3.57.0-translation.1
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/README.md +188 -0
- package/dist/blip-cards.css +1 -1
- package/dist/blip-cards.js +1 -1
- package/package.json +11 -1
package/README.md
CHANGED
|
@@ -112,8 +112,196 @@ Is possible to group your cards using any rule you want. For that use the <blip-
|
|
|
112
112
|
| readonly | Currently being used only in the quick reply. Could be implemented in other components too. This is used to avoid click actions in the quick reply options. If this attribute is true, than the <em>editable</em> and <em>deletable</em> should be false (OPTIONAL) | Boolean |
|
|
113
113
|
| hide-options | Used only only in the quick reply. This is used to hide the quick reply options (OPTIONAL) | Boolean |
|
|
114
114
|
| disable-link | Used only in the plain text. This is used to do not render tag 'a' (OPTIONAL) | Boolean |
|
|
115
|
+
| translation | Renders host-provided translations in the bubbles, with a toggle on the timestamp row. See [Message translation](#message-translation) (OPTIONAL) | Object |
|
|
116
|
+
|
|
117
|
+
## Message translation
|
|
118
|
+
`<blip-group-card>` and `<blip-card>` accept an optional `translation` prop that renders translated content inside the bubble and a small translation control below it, on the same row as the message time:
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
10:12 · [icon] Ver original em espanhol (received message, left aligned)
|
|
122
|
+
10:17 · [icon] Ver envio em espanhol (sent message, right aligned)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
The library never translates anything by itself: the host owns the translation requests and the cache, and the cards only render what `getTranslation` returns.
|
|
126
|
+
|
|
127
|
+
When the prop is absent or `enabled` is not `true`, the rendered markup is identical to the version without this feature (no extra elements, classes, comment nodes or callbacks).
|
|
128
|
+
|
|
129
|
+
```html
|
|
130
|
+
<blip-group-card :documents="documents" :translations="translations" :translation="translation" />
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
translation: {
|
|
135
|
+
enabled: true,
|
|
136
|
+
// Global default. Each bubble may override it locally through the toggle
|
|
137
|
+
// (requesting a translation through "Traduzir" also shows it once it is done);
|
|
138
|
+
// changing this value resets every local override.
|
|
139
|
+
showOriginal: false,
|
|
140
|
+
// Must be synchronous and read reactive state (e.g. Vuex getters) so the cards re-render on updates.
|
|
141
|
+
getTranslation: (fullDocument) => ({
|
|
142
|
+
status: 'done', // 'pending' | 'done' | 'error'
|
|
143
|
+
sourceLanguage: 'espanhol', // customer language, human-readable and already in the case to display
|
|
144
|
+
targetLanguage: 'português', // attendant language, human-readable and already in the case to display
|
|
145
|
+
text: 'Olá, tudo bem?', // optional
|
|
146
|
+
title: undefined, // optional
|
|
147
|
+
options: undefined, // optional, string[]
|
|
148
|
+
direction: 'received' // optional, 'received' | 'sent'
|
|
149
|
+
}), // or null when there is no translation for the message
|
|
150
|
+
onTranslate: (fullDocument) => {}, // optional, may return a Promise; called by the "Traduzir"/"Tentar novamente" actions
|
|
151
|
+
canTranslate: (fullDocument) => true // optional, defaults to `position === 'left'` (received messages)
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`fullDocument` is the original message envelope (`id`, `type`, `content`, ...).
|
|
156
|
+
|
|
157
|
+
| Field returned by `getTranslation` | Replaces (while the translation is shown) |
|
|
158
|
+
|---|---|
|
|
159
|
+
| `text` | Plain text (`text/plain`) content, select header text, media caption (image and file `text`) and the text of a received reply (`application/vnd.lime.reply+json` with a `text/plain` replied value) |
|
|
160
|
+
| `title` | Image title |
|
|
161
|
+
| `options[i]` | Displayed label of the select option `i`. Display only: the payload sent when an option is selected is always the original one |
|
|
162
|
+
|
|
163
|
+
**Direction.** `direction` from `getTranslation` when it is `'received'` or `'sent'`; otherwise `fullDocument.direction` when it is one of these values; otherwise `'sent'` for `position === 'right'` and `'received'` for any other position.
|
|
164
|
+
|
|
165
|
+
- **Received** messages: `text` is the customer message translated to the attendant language. The bubble shows it by default and the toggle reads "Ver original em {sourceLanguage}"; showing the original switches it to "Ver em {targetLanguage}".
|
|
166
|
+
- **Sent** messages: `text` is the attendant's original text (before it was translated and sent). The bubble shows it by default and the toggle reads "Ver envio em {sourceLanguage}"; showing the content actually sent (`document`) switches it to "Ver em {targetLanguage}". Sent messages are only evaluated when `canTranslate` is provided (the default only allows `position === 'left'`).
|
|
167
|
+
|
|
168
|
+
Toggle states (rendered after the time, or after the group time for the last bubble of a `<blip-group-card>` group):
|
|
169
|
+
|
|
170
|
+
| State | When | Toggle |
|
|
171
|
+
|---|---|---|
|
|
172
|
+
| none | prop absent, `enabled !== true`, `canTranslate` false, card being edited, `getTranslation` throws, or no translation and no `onTranslate` | not rendered |
|
|
173
|
+
| translate | no translation (or `done` without content) and `onTranslate` provided | "Traduzir" |
|
|
174
|
+
| pending | `status: 'pending'` | "Traduzindo..." as plain text (the button stays focusable with `aria-disabled="true"`) |
|
|
175
|
+
| error | `status: 'error'` | "Não foi possível traduzir · Tentar novamente" (retry only with `onTranslate`) |
|
|
176
|
+
| done | `status: 'done'` with a non-empty `text`, `title` or option | "Ver original em {language}" / "Ver envio em {language}" / "Ver em {language}" (see Direction); "Ver original" / "Ver envio" / "Ver tradução" when the language is missing |
|
|
177
|
+
|
|
178
|
+
The control becomes an `aria-live="polite"` region right after it is mounted, so bubbles rendered on load are not announced but later state changes (e.g. "Traduzindo..." → "Ver original em espanhol") are. The icon is `aria-hidden`.
|
|
179
|
+
|
|
180
|
+
Placement:
|
|
181
|
+
- Standalone cards render the control in their own time row (`10:12 · ...`). Without `date`, the control is rendered without the leading `·`.
|
|
182
|
+
- In `<blip-group-card>`, only the last bubble of a group has a time row (the group one), so its control is rendered there; the other bubbles of the group render the control on a row of their own below the bubble, without the time. A message is matched to its group row by `fullDocument.id`; messages without `id`, or whose `id` is repeated in `documents`, always use their own row.
|
|
183
|
+
- List menus (non-`immediate` selects) have no time row and always use their own row.
|
|
184
|
+
- The link color defaults to `#006e78`, a teal darker than the design one (`#00838F`) so that small text keeps a WCAG AA contrast of at least 4.5:1 on white (6.0:1) and on light-gray chat backgrounds such as `#ededed` (5.1:1) or `#e7e7e7` (4.9:1). Hosts can override it with the `--blip-cards-translation-link-color` CSS custom property; check the contrast against their own background when doing so.
|
|
185
|
+
|
|
186
|
+
Supported cards: `text/plain` (the `#blip.payload.text` metadata variant is not translated), `application/vnd.lime.select+json`, `application/vnd.blip.rich-select+json`, image and file `application/vnd.lime.media-link+json` and text replies. Other cards ignore the prop.
|
|
187
|
+
|
|
188
|
+
### Labels
|
|
189
|
+
Labels come from the existing `translations` prop under `messageTranslation`. Missing keys fall back to the pt-BR defaults below. `{language}` is replaced by the language name exactly as returned by the host (the library never changes its case, so pass e.g. "espanhol" for pt-BR sentences). When the language is missing or empty, the matching `*NoLanguage` label is used instead (if a host sets it to an empty string, the `{language}` placeholder is dropped from the regular label). Hosts that translate the regular labels should also translate the `*NoLanguage` ones, otherwise the pt-BR defaults are shown.
|
|
190
|
+
|
|
191
|
+
```js
|
|
192
|
+
translations: {
|
|
193
|
+
messageTranslation: {
|
|
194
|
+
viewOriginal: 'Ver original em {language}', // received, translation shown; {language} = sourceLanguage
|
|
195
|
+
viewTranslation: 'Ver em {language}', // original shown; {language} = targetLanguage
|
|
196
|
+
viewSent: 'Ver envio em {language}', // sent, attendant text shown; {language} = sourceLanguage
|
|
197
|
+
viewOriginalNoLanguage: 'Ver original', // viewOriginal without sourceLanguage
|
|
198
|
+
viewTranslationNoLanguage: 'Ver tradução', // viewTranslation without targetLanguage
|
|
199
|
+
viewSentNoLanguage: 'Ver envio', // viewSent without sourceLanguage
|
|
200
|
+
translate: 'Traduzir',
|
|
201
|
+
translating: 'Traduzindo...',
|
|
202
|
+
failed: 'Não foi possível traduzir',
|
|
203
|
+
retry: 'Tentar novamente'
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Rules for hosts
|
|
209
|
+
- **Do not swap the message content.** Never replace `document.content` (or any field of the envelope) with the translated text. Pass the original messages in `documents` and provide the translation only through `getTranslation`. Reply, edit and selection payloads always use the original document.
|
|
210
|
+
- `getTranslation` must be cheap and side-effect free: it runs on every render of a supported card. Trigger requests only from `onTranslate`.
|
|
211
|
+
- Report failures through `getTranslation` (`status: 'error'`). Errors thrown or promises rejected by `onTranslate` are swallowed by the cards.
|
|
212
|
+
|
|
213
|
+
### Security notes
|
|
214
|
+
- Translated strings are treated as untrusted input and go through the same sanitization pipeline as the original field of each card. Footer labels and languages are rendered with text interpolation only (never `v-html`).
|
|
215
|
+
- Translating sends message content to the host's translation provider. The host is responsible for consent, data-protection rules and for not sending content that must not leave the platform.
|
|
216
|
+
|
|
217
|
+
## Native Form Card
|
|
218
|
+
A structured form (several fields answered in a single interaction, optionally split in steps) is rendered when the bot sends an `application/vnd.blip.form+json` message.
|
|
219
|
+
|
|
220
|
+
> **Which type to send:** the platform deserializes `application/vnd.lime.input+json` into Lime's fixed `Input` type, which only keeps `label` and `validation.rule`/`type`/`regex`/`error`, so `fields`, `steps`, `submitLabel` and every other property below are dropped before the message reaches the client (the card then shows only the opening text and a default "Enviar" button). To use the form, send the same content under `application/vnd.blip.form+json`, which the platform forwards untouched. No backend change is needed: the content is authored in the bot flow. The card still also renders an `application/vnd.lime.input+json` whose `validation.type` is `application/vnd.lime.form+json` for hosts that deliver the content without the platform filtering, but flows should use `application/vnd.blip.form+json`. Only Blip Chat renders this type; other channels (e.g. WhatsApp) do not know it. What the bot receives is an `application/json` message, as described below.
|
|
221
|
+
|
|
222
|
+
```json
|
|
223
|
+
{
|
|
224
|
+
"type": "application/vnd.blip.form+json",
|
|
225
|
+
"content": {
|
|
226
|
+
"label": { "type": "text/plain", "value": "Please fill in the fields below:" },
|
|
227
|
+
"validation": {
|
|
228
|
+
"submitLabel": "Send",
|
|
229
|
+
"fields": [
|
|
230
|
+
{ "id": "name", "label": "Full name", "type": "text", "required": true, "minLength": 3 },
|
|
231
|
+
{ "id": "email", "label": "E-mail", "type": "email", "required": true, "helpText": "We will send the protocol here." },
|
|
232
|
+
{ "id": "phone", "label": "Phone", "type": "tel", "mask": "(00) 00000-0000" },
|
|
233
|
+
{ "id": "cpf", "label": "CPF", "type": "text", "mask": "000.000.000-00", "required": true },
|
|
234
|
+
{ "id": "account", "label": "Account number", "type": "number", "min": 1 },
|
|
235
|
+
{ "id": "visit", "label": "Visit date", "type": "date", "min": "2026-01-01" },
|
|
236
|
+
{ "id": "reason", "label": "Reason", "type": "select", "required": true, "options": [{ "value": "bill", "label": "Bill copy" }] },
|
|
237
|
+
{ "id": "topics", "label": "Topics", "type": "checkbox", "options": [{ "value": "bill", "label": "Bill" }, { "value": "meter", "label": "Meter" }] },
|
|
238
|
+
{ "id": "photo", "label": "Meter photo", "type": "file", "accept": "image/*,application/pdf", "maxSizeMb": 5 }
|
|
239
|
+
]
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
**Form properties** (`validation`)
|
|
246
|
+
|
|
247
|
+
| Property | Description |
|
|
248
|
+
| --- | --- |
|
|
249
|
+
| `fields` | Fields of a single-step form. |
|
|
250
|
+
| `steps` | `[{ "title"?: string, "fields": Field[] }]`. When present and non-empty, `fields` is ignored. Steps left without valid fields are dropped. |
|
|
251
|
+
| `submitLabel` / `nextLabel` / `backLabel` | Button labels. Defaults: `Enviar`, `Continuar`, `Voltar`. |
|
|
252
|
+
| `messages` | Overrides the default (pt-BR) validation messages by key: `required`, `email`, `tel`, `minLength`, `maxLength`, `min`, `max`, `pattern`, `mask`, `fileType`, `fileSize`, `uploadFailed`, `uploading`. `{min}`, `{max}` and `{mask}` are replaced. |
|
|
253
|
+
|
|
254
|
+
**Field types** (unknown types are rendered as `text`)
|
|
255
|
+
|
|
256
|
+
| `type` | Rendered as | Value sent |
|
|
257
|
+
| --- | --- | --- |
|
|
258
|
+
| `text` | text input | string |
|
|
259
|
+
| `number` | number input (`min`/`max`) | string |
|
|
260
|
+
| `email` | e-mail input (e-mail keyboard/autocomplete) | string |
|
|
261
|
+
| `tel` | phone input; without `mask`, 8 to 15 digits are required | string |
|
|
262
|
+
| `date` | date input (`min`/`max` as `YYYY-MM-DD`) | `YYYY-MM-DD` |
|
|
263
|
+
| `textarea` | multi-line text | string |
|
|
264
|
+
| `select` | dropdown (`options`) | option `value` |
|
|
265
|
+
| `radio` | radio group (`options`) | option `value` |
|
|
266
|
+
| `checkbox` | checkbox group (`options`) | array of option `value`s (`[]` when none) |
|
|
267
|
+
| `file` | file picker (`accept`, `maxSizeMb`, default 20) — requires `on-upload-file` | uploaded media URI |
|
|
268
|
+
|
|
269
|
+
**Field properties**: `id` and `label` (required; ids must be unique across all steps), `required`, `placeholder`, `helpText`, `minLength`/`maxLength` (text, email, tel, textarea), `min`/`max` (number, date), `pattern` (JS regex source that must match the whole value; invalid expressions are ignored), `mask` (text and tel), `errorMessage` (replaces any validation message of the field, except the "wait for the upload" notice, which can only be changed with `messages.uploading`), `options` (`[{ "value", "label" }]`), `accept` and `maxSizeMb` (file), `value` (pre-fill, see below).
|
|
270
|
+
|
|
271
|
+
- **Pre-fill** (`value`): fills a field with data the bot already knows, usually a Builder variable such as `{{contact.extras.cpf}}`. Pre-filled values are editable, validated like typed values and sent in the content and summary.
|
|
272
|
+
- `text`, `number`, `email`, `tel`, `date`, `textarea`: a string, trimmed (numbers are converted to strings). With a `mask`, the value is formatted with it (`"12345678909"` → `123.456.789-09`). `number` values must follow the HTML number format (`42`, `-1.5`, `2.5e1`; not `+5`, `0x10` or `1.`) and `date` values must be a valid `YYYY-MM-DD`; anything else is ignored.
|
|
273
|
+
- `select` / `radio`: used only when it matches one option `value`; otherwise the field starts empty.
|
|
274
|
+
- `checkbox`: an array (or a single string) of option values; values that are not options are dropped and the result follows the options order.
|
|
275
|
+
- `file`: not supported, `value` is ignored.
|
|
276
|
+
- A string that still contains an unresolved variable (`{{...}}` anywhere, e.g. `"{{contact.extras.cpf}}"` or `"Rua {{contact.street}}"` when the contact has no such data) is treated as empty. In a `checkbox` array, only those items are dropped.
|
|
277
|
+
- A pre-filled value that is invalid (e.g. `"maria@email"` in an `email` field) is not flagged on load. It shows its error and blocks sending when the user tries to continue.
|
|
278
|
+
- Pre-fills are applied when the card is created. If the same card later receives an updated document, only newly added fields are pre-filled; values of existing fields (including the user's edits) are kept. A field that keeps its `id` but changes `type` keeps its current value only if it is valid for the new type under these same rules (e.g. a text `"abc"` does not survive a change to a `select` without such option); otherwise it gets the pre-fill of its new type. A `select`/`radio` value that is no longer among the `options` counts as empty (required error, or sent as `""`), and `checkbox` values keep only existing options.
|
|
279
|
+
- **Privacy**: pre-fill only the data the user needs to confirm or that the form needs (data minimisation). Pre-filled values are part of the bot message: they remain visible in the conversation history and transcripts, and in older cards that are shown disabled.
|
|
280
|
+
- **Valid state**: text inputs, selects and textareas holding a valid, non-empty value get a green border (`$color-positive`) once left (or after the user tries to continue), and pre-filled fields get it right away. Empty fields, radio/checkbox groups and `file` fields (which show ✓ after the upload) keep the neutral style. Error style takes precedence.
|
|
281
|
+
|
|
282
|
+
```json
|
|
283
|
+
{ "id": "cpf", "label": "CPF do titular", "type": "text", "mask": "000.000.000-00", "required": true, "value": "{{contact.extras.cpf}}" }
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
- **Masks**: `0` is a digit, `A` a letter, `*` a letter or digit; any other character is a literal inserted automatically, e.g. `000.000.000-00`, `(00) 00000-0000`, `00000-000`. The value is formatted while typing and is sent as displayed. A masked field is valid only when completely filled.
|
|
287
|
+
- **Validation** happens in real time: errors are shown under a field after it is left (or after the user tries to continue) and update while typing. The form cannot be advanced/sent while there are errors in the current step; focus goes to the first invalid field.
|
|
288
|
+
- **Steps**: with more than one step, a "Etapa n de N" header with a progress bar is shown; `Voltar` keeps the values and only the current step is validated before moving on. Single-step forms have no header nor back button.
|
|
289
|
+
- Fields without an `id`, `select`/`radio`/`checkbox` fields without `options` and `file` fields when the host does not provide `on-upload-file` are ignored. Labels are rendered as plain text.
|
|
290
|
+
- **Upload**: hosts enable `file` fields by passing `on-upload-file` to `blip-card`/`blip-group-card`: `(file: File) => Promise<string | { uri: string }>`. The file type (`accept`, MIME type or extension, wildcards such as `image/*`) and size are checked before calling it; the form cannot be sent while an upload is pending, and a rejected promise shows an error so the user can choose another file.
|
|
291
|
+
- After the form is sent, the read-only summary shows the file name of each `file` field as a link. It calls `on-media-selected(uri)` when the host provides it (blip-chat renews an expired link before opening it) and otherwise opens the address in a new tab. Only `http(s)` addresses are linked.
|
|
292
|
+
- `on-selected` is called with a `"label: value"` summary text (dates formatted with the browser locale, option labels for `select`/`radio`/`checkbox`, the file name as a link (`[name](https://…)`) for `file`, so the file can still be opened from the conversation history) and `{ type: 'application/json', content: { [field.id]: value } }` containing the fields of all steps. Empty optional fields are sent as `""` (`[]` for `checkbox`). Once sent, the card shows a read-only summary.
|
|
293
|
+
- When `hide-options` is true, the fields and all buttons are disabled. Hosts such as blip-chat set it on every card that is not the latest message of the conversation.
|
|
294
|
+
|
|
295
|
+
**Reading the answers in Builder** (no backend change is required; verified against Take.Blip.Builder 0.7.524 and on HMG):
|
|
296
|
+
|
|
297
|
+
- Save the user input of the state that waits for the form in a variable (e.g. `formulario`). It holds the JSON content, and each field is read with `{{formulario@<field id>}}` (e.g. `{{formulario@cpf}}`).
|
|
298
|
+
- Leave the input validation of that state empty or set it to type `application/json`. A type validation of `application/vnd.lime.form+json` or `application/vnd.blip.form+json` rejects the answer, because the reply is `application/json`.
|
|
299
|
+
- Use only letters, digits and `_` in field ids (`data_visita`, not `data-visita`): the variable property syntax does not accept `-`, and `.` is read as a nested property.
|
|
300
|
+
- `checkbox` values are returned as a JSON array (`{{formulario@topicos}}` → `["conta","medidor"]`); indexes such as `@topicos[0]` are not supported, so use a script to split them.
|
|
301
|
+
- `file` values are media links signed for a limited time (30 minutes for blip-chat uploads on HMG). Forward or store the file within that window, or renew the link with the `set /refresh-media-uri` command to `postmaster@media.msging.net`.
|
|
115
302
|
|
|
116
303
|
## Contributions
|
|
304
|
+
|
|
117
305
|
Any contribution to the code via pull request would be nice and any idea and/or suggestion are very welcome.
|
|
118
306
|
|
|
119
307
|
However, please do not feel bad if your pull requests or contributions do not get merged or implemented into `blip-cards`.
|