Skip to content

The problem is related to RichTextPlain serialization inside formatted rich text #1599

Description

@kenzow0w

Describe the bug
Hi! I found a possible issue with the Rich Message implementation in telegrambots 10.2.0.

The problem is related to RichTextPlain serialization inside formatted rich text.

According to the Java model, RichTextBold contains:


@JsonProperty("text")
private RichText text;

and RichTextPlain is implemented as:

public class RichTextPlain implements RichText {
    @JsonProperty("type")
    private final String type = "plain";

    @JsonProperty("text")
    private String text;
}

Therefore, when I create a bold text like:


new RichTextBold(
    new RichTextPlain("Иван")
)

the library serializes it as:

{
  "type": "bold",
  "text": {
    "type": "plain",
    "text": "Иван"
  }
}

However, Telegram Bot API does not accept plain as a rich text type. Plain text is expected to be represented directly as a JSON string.

For example, Telegram accepts:

{
  "type": "bold",
  "text": {
    "type": "bold",
    "text": "Иван"
  }
}

but rejects:

{
  "type": "bold",
  "text": {
    "type": "plain",
    "text": "Иван"
  }
}

The error returned by Telegram is:

Bad Request: Can't parse PageBlockTableCell: Unsupported rich text type

I confirmed this using sendRichMessage with a table. The following payload works:

{
  "blocks": [
    {
      "type": "table",
      "cells": [
        [
          {
"type": "bold",
            "text": {
              "type": "bold",
              "text": "Иван"
            },
            "align": "left",
            "valign": "middle"
          }
        ]
      ]
    }
  ]
}

while the payload generated by the Java library:

{
  "blocks": [
    {
      "type": "table",
      "cells": [
        [
          {
            "text": {
              "type": "bold",
              "text": {
                "type": "plain",
                "text": "Иван"
              }
            },
            "align": "left",
            "valign": "middle"
          }
        ]
      ]
    }
  ]
}

This is my code for generate this body:

    private RichBlockTableCell header(String text) {
        RichTextBold bold = new RichTextBold(
                new RichTextPlain(text)
        );

        return RichBlockTableCell.builder()
                .text(bold)
                .isHeader(true)
                .align("left")
                .valign("middle")
                .build();
    }
            InputRichBlockTable table = InputRichBlockTable.builder()
                    .cells(List.of(
                            List.of(
                                    header("Иван")
                            ),
                            List.of(
                                    header("500")
                            )
                    ))
                    .build();

            InputRichMessage inputRichMessage = InputRichMessage.builder()
                    .blocks(List.of(table))
                    .build();
            SendRichMessage sendRichMessage = SendRichMessage
                    .builder()
                    .chatId(chatId)
                    .richMessage(inputRichMessage)
                    .build();

is rejected by Telegram.

So the issue seems to be that RichTextPlain is modeled as a RichText subtype with "type": "plain", while the Telegram API expects plain text to be serialized as a JSON string.

Could you please confirm whether this is a bug in the telegrambots 10.2.0 Rich Message model/serialization?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions