blip-cards 3.55.0 → 3.56.0

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 CHANGED
@@ -113,7 +113,94 @@ Is possible to group your cards using any rule you want. For that use the <blip-
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
115
 
116
+ ## Native Form Card
117
+ 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.
118
+
119
+ > **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.
120
+
121
+ ```json
122
+ {
123
+ "type": "application/vnd.blip.form+json",
124
+ "content": {
125
+ "label": { "type": "text/plain", "value": "Please fill in the fields below:" },
126
+ "validation": {
127
+ "submitLabel": "Send",
128
+ "fields": [
129
+ { "id": "name", "label": "Full name", "type": "text", "required": true, "minLength": 3 },
130
+ { "id": "email", "label": "E-mail", "type": "email", "required": true, "helpText": "We will send the protocol here." },
131
+ { "id": "phone", "label": "Phone", "type": "tel", "mask": "(00) 00000-0000" },
132
+ { "id": "cpf", "label": "CPF", "type": "text", "mask": "000.000.000-00", "required": true },
133
+ { "id": "account", "label": "Account number", "type": "number", "min": 1 },
134
+ { "id": "visit", "label": "Visit date", "type": "date", "min": "2026-01-01" },
135
+ { "id": "reason", "label": "Reason", "type": "select", "required": true, "options": [{ "value": "bill", "label": "Bill copy" }] },
136
+ { "id": "topics", "label": "Topics", "type": "checkbox", "options": [{ "value": "bill", "label": "Bill" }, { "value": "meter", "label": "Meter" }] },
137
+ { "id": "photo", "label": "Meter photo", "type": "file", "accept": "image/*,application/pdf", "maxSizeMb": 5 }
138
+ ]
139
+ }
140
+ }
141
+ }
142
+ ```
143
+
144
+ **Form properties** (`validation`)
145
+
146
+ | Property | Description |
147
+ | --- | --- |
148
+ | `fields` | Fields of a single-step form. |
149
+ | `steps` | `[{ "title"?: string, "fields": Field[] }]`. When present and non-empty, `fields` is ignored. Steps left without valid fields are dropped. |
150
+ | `submitLabel` / `nextLabel` / `backLabel` | Button labels. Defaults: `Enviar`, `Continuar`, `Voltar`. |
151
+ | `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. |
152
+
153
+ **Field types** (unknown types are rendered as `text`)
154
+
155
+ | `type` | Rendered as | Value sent |
156
+ | --- | --- | --- |
157
+ | `text` | text input | string |
158
+ | `number` | number input (`min`/`max`) | string |
159
+ | `email` | e-mail input (e-mail keyboard/autocomplete) | string |
160
+ | `tel` | phone input; without `mask`, 8 to 15 digits are required | string |
161
+ | `date` | date input (`min`/`max` as `YYYY-MM-DD`) | `YYYY-MM-DD` |
162
+ | `textarea` | multi-line text | string |
163
+ | `select` | dropdown (`options`) | option `value` |
164
+ | `radio` | radio group (`options`) | option `value` |
165
+ | `checkbox` | checkbox group (`options`) | array of option `value`s (`[]` when none) |
166
+ | `file` | file picker (`accept`, `maxSizeMb`, default 20) — requires `on-upload-file` | uploaded media URI |
167
+
168
+ **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).
169
+
170
+ - **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.
171
+ - `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.
172
+ - `select` / `radio`: used only when it matches one option `value`; otherwise the field starts empty.
173
+ - `checkbox`: an array (or a single string) of option values; values that are not options are dropped and the result follows the options order.
174
+ - `file`: not supported, `value` is ignored.
175
+ - 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.
176
+ - 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.
177
+ - 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.
178
+ - **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.
179
+ - **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.
180
+
181
+ ```json
182
+ { "id": "cpf", "label": "CPF do titular", "type": "text", "mask": "000.000.000-00", "required": true, "value": "{{contact.extras.cpf}}" }
183
+ ```
184
+
185
+ - **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.
186
+ - **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.
187
+ - **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.
188
+ - 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.
189
+ - **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.
190
+ - 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.
191
+ - `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.
192
+ - 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.
193
+
194
+ **Reading the answers in Builder** (no backend change is required; verified against Take.Blip.Builder 0.7.524 and on HMG):
195
+
196
+ - 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}}`).
197
+ - 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`.
198
+ - 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.
199
+ - `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.
200
+ - `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`.
201
+
116
202
  ## Contributions
203
+
117
204
  Any contribution to the code via pull request would be nice and any idea and/or suggestion are very welcome.
118
205
 
119
206
  However, please do not feel bad if your pull requests or contributions do not get merged or implemented into `blip-cards`.