Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): cap chat ids and batch ids in send bodies
Yudhi Armyndharis6ac31e922cb4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 21c60eccd..e0b966c22 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- Answer `400` for a send route's `chatId` or a bulk `batchId` over 255 characters, where PostgreSQL answered `500` once the value passed its index entry limit.- Index ingress dead letters by delivery id, so the reconciler's per-delivery checks stop scanning every dead letter of an instance; the first boot builds the index.- Replay stranded ingress events past one whose stored payload is not valid JSON, instead of failing every sweep on it.- Hand a chat back to the bot when the plugin instance that took it over is deleted, instead of silencing every other plugin on it.diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex b59b3f99a..69356f475 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -1747,7 +1747,7 @@ Send a plain text message.| Field | Type | Required | Constraints | Description || ----------------- | -------- | -------- | ------------------------------ | -------------------------------------------------------------------------------------------- |-| chatId | string | Yes | non-empty | `[email protected]` or `[email protected]` |+| chatId | string | Yes | non-empty, max 255 | `[email protected]` or `[email protected]` || text | string | Yes | non-empty, max 4096 | Message text || mentions | string[] | No | array of WIDs | WIDs to @mention (e.g. `["[email protected]"]`). See **Mentions** below || linkPreview | boolean | No | — | `false` suppresses it; on Baileys `true` is required to get one. See **Link previews** below |@@ -1873,7 +1873,7 @@ Render a stored text template (header/body/footer joined by blank lines, `{{vars| Field | Type | Required | Constraints | Description || ------------ | ----------------------- | ----------- | ------------------------------------------------- | ----------------------------------------------------------- |-| chatId | string | Yes | non-empty | Target chat |+| chatId | string | Yes | non-empty, max 255 | Target chat || templateId | string | Conditional | non-empty; required when `templateName` is absent | Stored template id || templateName | string | Conditional | non-empty; required when `templateId` is absent | Stored template name || vars | Record\<string,string\> | No | object | Substituted into `{{placeholder}}` tokens; defaults to `{}` |@@ -1914,7 +1914,7 @@ Send an image (by URL or base64) with an optional caption.| Field | Type | Required | Constraints | Description || --------------- | -------- | ----------- | ------------------------------------- | ---------------------------------------------------------------------------------------- |-| chatId | string | Yes | non-empty | Target chat |+| chatId | string | Yes | non-empty, max 255 | Target chat || url | string | Conditional | URL; required when `base64` is absent | http/https media URL (SSRF-guarded; a blocked internal or unreachable URL maps to `400`) || base64 | string | Conditional | string; required when `url` is absent | Base64 media data (capped to the media byte limit) || mimetype | string | Conditional | string; required when using `base64` | MIME type of the media |@@ -2034,14 +2034,14 @@ Send a location pin.**Request body** — `SendLocationDto`-| Field | Type | Required | Constraints | Description |-| --------------- | ------ | -------- | --------------- | --------------------------------------------------------------------------------- |-| chatId | string | Yes | non-empty | Target chat |-| latitude | number | Yes | valid latitude | Latitude (out-of-range → `400`) |-| longitude | number | Yes | valid longitude | Longitude (out-of-range → `400`) |-| description | string | No | string | Pin description |-| address | string | No | string | Pin address |-| quotedMessageId | string | No | non-empty | Quote an earlier message, making this a reply — see [Quoted sends](#quoted-sends) |+| Field | Type | Required | Constraints | Description |+| --------------- | ------ | -------- | ------------------ | --------------------------------------------------------------------------------- |+| chatId | string | Yes | non-empty, max 255 | Target chat |+| latitude | number | Yes | valid latitude | Latitude (out-of-range → `400`) |+| longitude | number | Yes | valid longitude | Longitude (out-of-range → `400`) |+| description | string | No | string | Pin description |+| address | string | No | string | Pin address |+| quotedMessageId | string | No | non-empty | Quote an earlier message, making this a reply — see [Quoted sends](#quoted-sends) |```json{@@ -2075,12 +2075,12 @@ Send a contact card (vCard).**Request body** — `SendContactDto`-| Field | Type | Required | Constraints | Description |-| --------------- | ------ | -------- | ----------- | --------------------------------------------------------------------------------- |-| chatId | string | Yes | non-empty | Target chat |-| contactName | string | Yes | non-empty | Display name for the contact card |-| contactNumber | string | Yes | non-empty | Contact phone number |-| quotedMessageId | string | No | non-empty | Quote an earlier message, making this a reply — see [Quoted sends](#quoted-sends) |+| Field | Type | Required | Constraints | Description |+| --------------- | ------ | -------- | ------------------ | --------------------------------------------------------------------------------- |+| chatId | string | Yes | non-empty, max 255 | Target chat |+| contactName | string | Yes | non-empty | Display name for the contact card |+| contactNumber | string | Yes | non-empty | Contact phone number |+| quotedMessageId | string | No | non-empty | Quote an earlier message, making this a reply — see [Quoted sends](#quoted-sends) |```json{ "chatId": "[email protected]", "contactName": "John Doe", "contactNumber": "628987654321" }@@ -2136,7 +2136,7 @@ Send a native WhatsApp poll.| Field | Type | Required | Constraints | Description || -------------------- | -------- | -------- | ----------------------------------------- | ------------------------------------------------------------ |-| chatId | string | Yes | non-empty | Target chat |+| chatId | string | Yes | non-empty, max 255 | Target chat || name | string | Yes | max 255 | Poll question / title || options | string[] | Yes | 2–12 items, each non-empty, max 100 chars | Options to vote on || allowMultipleAnswers | boolean | No | — | Allow picking several options (default single choice) |@@ -2173,12 +2173,12 @@ Reply to a message, quoting a prior message.**Request body** — `ReplyMessageDto`-| Field | Type | Required | Constraints | Description |-| --------------- | -------- | -------- | ------------- | ---------------------------------------- |-| chatId | string | Yes | non-empty | Target chat |-| quotedMessageId | string | Yes | non-empty | WhatsApp id of the message being quoted |-| text | string | Yes | non-empty | Reply text |-| mentions | string[] | No | array of WIDs | WIDs to @mention. See **Mentions** above |+| Field | Type | Required | Constraints | Description |+| --------------- | -------- | -------- | ------------------ | ---------------------------------------- |+| chatId | string | Yes | non-empty, max 255 | Target chat |+| quotedMessageId | string | Yes | non-empty | WhatsApp id of the message being quoted |+| text | string | Yes | non-empty | Reply text |+| mentions | string[] | No | array of WIDs | WIDs to @mention. See **Mentions** above |```json{ "chatId": "[email protected]", "quotedMessageId": "[email protected]_3EB0ABCD", "text": "Replying to you" }@@ -2210,7 +2210,7 @@ Tap a choice on a WhatsApp Business button / list prompt by sending the structur| Field | Type | Required | Constraints | Description || --------- | ------ | -------- | ------------------- | ---------------------------------------------------------------------------- |-| chatId | string | Yes | non-empty | Chat that holds the prompt |+| chatId | string | Yes | non-empty, max 255 | Chat that holds the prompt || messageId | string | Yes | non-empty | WhatsApp id of the inbound prompt (`buttons` on `message.received`) || buttonId | string | Yes | non-empty, max 256 | Stable id of the choice (`buttons[].id`) || text | string | No | non-empty, max 4096 | Visible label; resolved from the stored prompt when omitted, else `buttonId` |@@ -2251,11 +2251,11 @@ Forward a message from one chat to another.**Request body** — `ForwardMessageDto`-| Field | Type | Required | Constraints | Description |-| ---------- | ------ | -------- | ----------- | ------------------------------------- |-| fromChatId | string | Yes | non-empty | Source chat |-| toChatId | string | Yes | non-empty | Destination chat |-| messageId | string | Yes | non-empty | WhatsApp id of the message to forward |+| Field | Type | Required | Constraints | Description |+| ---------- | ------ | -------- | ------------------ | ------------------------------------- |+| fromChatId | string | Yes | non-empty, max 255 | Source chat |+| toChatId | string | Yes | non-empty, max 255 | Destination chat |+| messageId | string | Yes | non-empty | WhatsApp id of the message to forward |```json{ "fromChatId": "[email protected]", "toChatId": "[email protected]", "messageId": "[email protected]_3EB0XYZ" }@@ -2287,7 +2287,7 @@ Add or remove a reaction to a message (an empty emoji removes the reaction).| Field | Type | Required | Constraints | Description || --------- | ------ | -------- | --------------------- | -------------------------------------------------------------------------------- |-| chatId | string | Yes | non-empty | Target chat |+| chatId | string | Yes | non-empty, max 255 | Target chat || messageId | string | Yes | non-empty | Message to react to || emoji | string | Yes | string (may be empty) | Reaction emoji; an empty string removes the reaction. The field must be present. |@@ -2321,7 +2321,7 @@ Delete a message (for everyone by default); also clears the stored record and fl| Field | Type | Required | Constraints | Description || ----------- | ------- | -------- | ------------------------ | ------------------------------------------------------ |-| chatId | string | Yes | non-empty | Target chat |+| chatId | string | Yes | non-empty, max 255 | Target chat || messageId | string | Yes | non-empty | Message to delete || forEveryone | boolean | No | boolean (default `true`) | Delete for everyone; defaults to `true` in the service |@@ -2355,7 +2355,7 @@ Edit the text of a message sent by this account; also updates the stored record'| Field | Type | Required | Constraints | Description || --------- | -------- | -------- | ----------------------- | ------------------------------------------------- |-| chatId | string | Yes | non-empty | Chat containing the message |+| chatId | string | Yes | non-empty, max 255 | Chat containing the message || messageId | string | Yes | non-empty | Message to edit (the send response's `messageId`) || body | string | Yes | non-empty, ≤ 4096 chars | New text content || mentions | string[] | No | array of WIDs | Re-applies participant tags to the new body |@@ -2390,7 +2390,7 @@ Send messages to multiple recipients as an async batch — returns immediately a| Field | Type | Required | Constraints | Description || -------- | --------------------- | -------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |-| batchId | string | No | string | Auto-generated `batch_<hex>` if omitted; a duplicate id returns `400` |+| batchId | string | No | string, max 255 | Auto-generated `batch_<hex>` if omitted; a duplicate id returns `400` || messages | BulkMessageItemDto[] | Yes | array, max 100, nested-validated | The batch items (see below); exact duplicate entries (same `chatId`, `type`, `content` and `variables`) are collapsed before processing, first occurrence wins, order preserved; distinct messages to the same `chatId` are all sent || options | BulkMessageOptionsDto | No | nested-validated | Pacing/error options (see below) |@@ -2398,7 +2398,7 @@ Each `BulkMessageItemDto`: `{ chatId: string, type: 'text'|'image'|'video'|'audi`BulkMessageOptionsDto`: `{ delayBetweenMessages?: number (1000–60000, default 3000), randomizeDelay?: boolean (default true), stopOnError?: boolean (default false) }`.-Each item must carry what its `type` sends: a non-empty `chatId`, a non-empty `content.text` for `text`, and a `url` or `base64` under `content.<type>` for a media type. Without `base64`, the `url` must be an absolute http(s) URL; a `url` holding a placeholder is checked once `variables` fill it in. If any item does not, the request answers `400` and nothing is queued. The check runs again per item after `variables` and the `message:sending` gate, where a failure fails that item only.+Each item must carry what its `type` sends: a non-empty `chatId` of at most 255 characters, a non-empty `content.text` for `text`, and a `url` or `base64` under `content.<type>` for a media type. Without `base64`, the `url` must be an absolute http(s) URL; a `url` holding a placeholder is checked once `variables` fill it in. If any item does not, the request answers `400` and nothing is queued. The check runs again per item after `variables` and the `message:sending` gate, where a failure fails that item only.Each item's base64 media is checked against the media byte cap (`MEDIA_DOWNLOAD_MAX_BYTES`) twice: at batch creation, and again per item after `variables` and the `message:sending` plugin gate are applied. An item that outgrows the cap only after rendering fails individually (`failed` in `results`, with `message:failed` fired) instead of being sent. `totalMessages` in the response reflects the de-duplicated item count.diff --git a/openapi.json b/openapi.jsonindex b30053b51..0182bdce1 100644--- a/openapi.json+++ b/openapi.json@@ -19994,7 +19994,8 @@"chatId": {"type": "string","description": "WhatsApp chat ID ([email protected] for individual, [email protected] for groups)",- "example": "[email protected]"+ "example": "[email protected]",+ "maxLength": 255},"text": {"type": "string",@@ -20063,7 +20064,8 @@"chatId": {"type": "string","description": "WhatsApp chat ID ([email protected] for individual, [email protected] for groups)",- "example": "[email protected]"+ "example": "[email protected]",+ "maxLength": 255},"templateId": {"type": "string",@@ -20111,7 +20113,8 @@"chatId": {"type": "string","description": "WhatsApp chat ID",- "example": "[email protected]"+ "example": "[email protected]",+ "maxLength": 255},"url": {"type": "string",@@ -20167,7 +20170,8 @@"chatId": {"type": "string","description": "WhatsApp chat ID",- "example": "[email protected]"+ "example": "[email protected]",+ "maxLength": 255},"url": {"type": "string",@@ -20226,7 +20230,8 @@"properties": {"chatId": {"type": "string",- "description": "Chat ID (e.g. [email protected])"+ "description": "Chat ID (e.g. [email protected])",+ "maxLength": 255},"latitude": {"type": "number",@@ -20260,7 +20265,8 @@"type": "object","properties": {"chatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"contactName": {"type": "string",@@ -20287,7 +20293,8 @@"properties": {"chatId": {"type": "string",- "description": "Chat ID (e.g. [email protected] or [email protected])"+ "description": "Chat ID (e.g. [email protected] or [email protected])",+ "maxLength": 255},"name": {"type": "string",@@ -20330,7 +20337,8 @@"type": "object","properties": {"chatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"quotedMessageId": {"type": "string"@@ -20362,7 +20370,8 @@"type": "object","properties": {"chatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"messageId": {"type": "string",@@ -20389,10 +20398,12 @@"type": "object","properties": {"fromChatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"toChatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"messageId": {"type": "string"@@ -20408,7 +20419,8 @@"type": "object","properties": {"chatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"messageId": {"type": "string"@@ -21065,7 +21077,8 @@"type": "object","properties": {"chatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"messageId": {"type": "string"@@ -21084,7 +21097,8 @@"type": "object","properties": {"chatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"pollMessageId": {"type": "string",@@ -21109,7 +21123,8 @@"type": "object","properties": {"chatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"messageId": {"type": "string"@@ -21134,7 +21149,8 @@"type": "object","properties": {"chatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"messageId": {"type": "string"@@ -21149,7 +21165,8 @@"type": "object","properties": {"chatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"messageId": {"type": "string"@@ -21169,7 +21186,8 @@"type": "object","properties": {"chatId": {- "type": "string"+ "type": "string",+ "maxLength": 255},"messageId": {"type": "string"@@ -21288,7 +21306,8 @@"chatId": {"type": "string","description": "Recipient chat ID",- "example": "[email protected]"+ "example": "[email protected]",+ "maxLength": 255},"type": {"type": "string",@@ -21347,7 +21366,8 @@"properties": {"batchId": {"type": "string",- "description": "Custom batch ID (auto-generated if not provided)"+ "description": "Custom batch ID (auto-generated if not provided)",+ "maxLength": 255},"messages": {"description": "Array of messages (max 100 per request; exact duplicate entries are collapsed — first occurrence wins)",diff --git a/src/modules/message/dto/bulk-message.dto.ts b/src/modules/message/dto/bulk-message.dto.tsindex 66d4b9949..b3ef588c4 100644--- a/src/modules/message/dto/bulk-message.dto.ts+++ b/src/modules/message/dto/bulk-message.dto.ts@@ -18,6 +18,8 @@ import { Type } from 'class-transformer';import { Validate } from 'class-validator';import { ToStrictBoolean } from '../../../common/utils/strict-boolean';import {+ BATCH_ID_MAX_LENGTH,+ CHAT_ID_MAX_LENGTH,MENTIONS_DESCRIPTION,MENTIONS_MAX,MENTION_WID_MAX_LENGTH,@@ -114,9 +116,10 @@ export class BulkMessageContentDto {}class BulkMessageItemDto {- @ApiProperty({ description: 'Recipient chat ID', example: '[email protected]' })+ @ApiProperty({ description: 'Recipient chat ID', example: '[email protected]', maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty({ description: 'Message type', enum: ['text', 'image', 'video', 'audio', 'document'] })@@ -164,9 +167,13 @@ class BulkMessageOptionsDto {export const BULK_MESSAGES_MAX = 100;export class SendBulkMessageDto {- @ApiPropertyOptional({ description: 'Custom batch ID (auto-generated if not provided)' })+ @ApiPropertyOptional({+ description: 'Custom batch ID (auto-generated if not provided)',+ maxLength: BATCH_ID_MAX_LENGTH,+ })@IsOptional()@IsString()+ @MaxLength(BATCH_ID_MAX_LENGTH)batchId?: string;@ApiProperty({diff --git a/src/modules/message/dto/id-length.dto.spec.ts b/src/modules/message/dto/id-length.dto.spec.tsnew file mode 100644index 000000000..10df02464--- /dev/null+++ b/src/modules/message/dto/id-length.dto.spec.ts@@ -0,0 +1,66 @@+import { DECORATORS } from '@nestjs/swagger';+import { plainToInstance } from 'class-transformer';+import { validate } from 'class-validator';+import {+ ClickButtonDto,+ DeleteMessageDto,+ EditMessageDto,+ ForwardMessageDto,+ PinMessageDto,+ ReactMessageDto,+ ReplyMessageDto,+ SendContactDto,+ SendLocationDto,+ SendPollDto,+ StarMessageDto,+ UnpinMessageDto,+ VotePollDto,+} from './message-actions.dto';+import { SendBulkMessageDto } from './bulk-message.dto';+import { BATCH_ID_MAX_LENGTH, CHAT_ID_MAX_LENGTH, SendMediaMessageDto, SendTextMessageDto } from './send-message.dto';+import { SendTemplateMessageDto } from './send-template.dto';++// A chat id and a batch id are stored under btree indexes, and PostgreSQL refuses an index entry past+// about 2.7 KB, which the client then saw as a 500. The REST bodies refuse them as a 400 first, and+// publish the bound so a client generated from the schema knows it.+const fields: Array<[string, new () => object, string, number]> = [+ ['SendTextMessageDto', SendTextMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['SendMediaMessageDto', SendMediaMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['SendLocationDto', SendLocationDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['SendContactDto', SendContactDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['SendPollDto', SendPollDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['SendTemplateMessageDto', SendTemplateMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['ReplyMessageDto', ReplyMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['ForwardMessageDto', ForwardMessageDto, 'fromChatId', CHAT_ID_MAX_LENGTH],+ ['ForwardMessageDto', ForwardMessageDto, 'toChatId', CHAT_ID_MAX_LENGTH],+ ['ReactMessageDto', ReactMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['DeleteMessageDto', DeleteMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['PinMessageDto', PinMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['UnpinMessageDto', UnpinMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['StarMessageDto', StarMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['EditMessageDto', EditMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['VotePollDto', VotePollDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['ClickButtonDto', ClickButtonDto, 'chatId', CHAT_ID_MAX_LENGTH],+ ['SendBulkMessageDto', SendBulkMessageDto, 'batchId', BATCH_ID_MAX_LENGTH],+];++describe('caller-chosen id length caps', () => {+ // Only the property under test is read: the other required fields of each body are left out on purpose.+ const constraintsOf = async (dto: new () => object, field: string, value: string) => {+ const errors = await validate(plainToInstance(dto, { [field]: value }, { enableImplicitConversion: true }));+ return errors.find(error => error.property === field)?.constraints;+ };++ it.each(fields)('%s.%s accepts the cap and refuses one more character', async (_name, dto, field, max) => {+ expect((await constraintsOf(dto, field, 'a'.repeat(max)))?.maxLength).toBeUndefined();+ expect((await constraintsOf(dto, field, 'a'.repeat(max + 1)))?.maxLength).toEqual(+ expect.stringContaining(String(max)),+ );+ });++ it.each(fields)('%s.%s publishes the cap in the schema', (_name, dto, field, max) => {+ const published = Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, dto.prototype as object, field) as+ { maxLength?: number } | undefined;+ expect(published?.maxLength).toBe(max);+ });+});diff --git a/src/modules/message/dto/message-actions.dto.ts b/src/modules/message/dto/message-actions.dto.tsindex ca12666ae..a57b4f502 100644--- a/src/modules/message/dto/message-actions.dto.ts+++ b/src/modules/message/dto/message-actions.dto.ts@@ -15,6 +15,7 @@ import {} from 'class-validator';import { ToStrictBoolean, ToStrictNumber } from '../../../common/utils/strict-boolean';import {+ CHAT_ID_MAX_LENGTH,MENTIONS_DESCRIPTION,MENTIONS_MAX,MENTION_WID_MAX_LENGTH,@@ -39,9 +40,10 @@ export const CONTACT_NUMBER_MAX_LENGTH = 30;export const REACTION_EMOJI_MAX_LENGTH = 32;export class SendLocationDto {- @ApiProperty({ description: 'Chat ID (e.g. [email protected])' })+ @ApiProperty({ description: 'Chat ID (e.g. [email protected])', maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty({ example: -6.2088 })@@ -74,9 +76,10 @@ export class SendLocationDto {}export class SendContactDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty({ maxLength: CONTACT_NAME_MAX_LENGTH })@@ -99,9 +102,10 @@ export class SendContactDto {}export class SendPollDto {- @ApiProperty({ description: 'Chat ID (e.g. [email protected] or [email protected])' })+ @ApiProperty({ description: 'Chat ID (e.g. [email protected] or [email protected])', maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty({ description: 'Poll question / title', maxLength: 255, example: 'Where should we meet?' })@@ -145,9 +149,10 @@ export class SendPollDto {}export class ReplyMessageDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty()@@ -179,14 +184,16 @@ export class ReplyMessageDto {}export class ForwardMessageDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)fromChatId!: string;- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)toChatId!: string;@ApiProperty()@@ -196,9 +203,10 @@ export class ForwardMessageDto {}export class ReactMessageDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty()@@ -217,9 +225,10 @@ export class ReactMessageDto {}export class DeleteMessageDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty()@@ -246,9 +255,10 @@ export class DeleteMessageDto {export const PIN_DURATIONS_SECONDS = [86400, 604800, 2592000] as const;export class PinMessageDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty()@@ -274,9 +284,10 @@ export class PinMessageDto {export const POLL_VOTE_MAX_OPTIONS = 12;export class VotePollDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty({ description: 'The poll creation message to vote on.' })@@ -298,9 +309,10 @@ export class VotePollDto {}export class ClickButtonDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty({ description: 'WhatsApp id of the business prompt message that offered the buttons.' })@@ -332,9 +344,10 @@ export class ClickButtonDto {}export class StarMessageDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty()@@ -351,9 +364,10 @@ export class StarMessageDto {}export class UnpinMessageDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty()@@ -363,9 +377,10 @@ export class UnpinMessageDto {}export class EditMessageDto {- @ApiProperty()+ @ApiProperty({ maxLength: CHAT_ID_MAX_LENGTH })@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty()diff --git a/src/modules/message/dto/send-message.dto.ts b/src/modules/message/dto/send-message.dto.tsindex 02b5bfcac..65f530b2e 100644--- a/src/modules/message/dto/send-message.dto.ts+++ b/src/modules/message/dto/send-message.dto.ts@@ -41,6 +41,15 @@ export const CUSTOM_PREVIEW_DESCRIPTION_MAX_LENGTH = 1024;// (src/core/agent-tools/tools/message.tools.ts) so MCP and REST enforce the same limit.export const MESSAGE_TEXT_MAX_LENGTH = 4096;+/**+ * Caps for the ids a caller chooses: a chat id and a bulk batch id. Both end up in a btree index+ * (the message chat indexes, UNIQUE(sessionId, batchId)), and PostgreSQL refuses an index entry past+ * about 2.7 KB with an error the client sees as a 500, where SQLite takes any length. A WhatsApp JID is+ * well under 100 characters, so 255 leaves room for every real id and matches the idempotency key cap.+ */+export const CHAT_ID_MAX_LENGTH = 255;+export const BATCH_ID_MAX_LENGTH = 255;+// The cap on a prompt choice's id. It must equal BUTTON_TEXT_MAX_LENGTH in the Baileys message// mapper, which refuses to offer any inbound choice whose id is longer, so an id past this bound// can never name a choice that exists. Validating it against the text cap instead accepted such a@@ -91,9 +100,11 @@ export class SendTextMessageDto {@ApiProperty({description: 'WhatsApp chat ID ([email protected] for individual, [email protected] for groups)',example: '[email protected]',+ maxLength: CHAT_ID_MAX_LENGTH,})@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiProperty({@@ -190,9 +201,11 @@ export class SendMediaMessageDto {@ApiProperty({description: 'WhatsApp chat ID',example: '[email protected]',+ maxLength: CHAT_ID_MAX_LENGTH,})@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiPropertyOptional({diff --git a/src/modules/message/dto/send-template.dto.ts b/src/modules/message/dto/send-template.dto.tsindex a32bfe854..0799dbfdc 100644--- a/src/modules/message/dto/send-template.dto.ts+++ b/src/modules/message/dto/send-template.dto.ts@@ -11,16 +11,18 @@ import {Validate,} from 'class-validator';import { ToStrictBoolean } from '../../../common/utils/strict-boolean';-import { MENTIONS_DESCRIPTION, MENTIONS_MAX, MENTION_WID_MAX_LENGTH } from './send-message.dto';+import { CHAT_ID_MAX_LENGTH, MENTIONS_DESCRIPTION, MENTIONS_MAX, MENTION_WID_MAX_LENGTH } from './send-message.dto';import { IsMentionWidConstraint } from './is-mention-wid.validator';export class SendTemplateMessageDto {@ApiProperty({description: 'WhatsApp chat ID ([email protected] for individual, [email protected] for groups)',example: '[email protected]',+ maxLength: CHAT_ID_MAX_LENGTH,})@IsString()@IsNotEmpty()+ @MaxLength(CHAT_ID_MAX_LENGTH)chatId!: string;@ApiPropertyOptional({