Skip to content

InputRichBlock*.to_dict() fails when RichText is a string or RichText object #2609

Description

@All-The-Foxes

InputRichBlock serialization assumes that every RichText value implements .to_dict(), but RichText fields can contain plain strings as well as RichText* objects (RichTextBold, etc). send_rich_message() fails during serialization for both cases. This is on the latest TeleBot version.

With a plain string:

rich_message = InputRichMessage(
    blocks = [
        InputRichBlockPreformatted(
            text = "test",
            language = ""
        )
    ]
)
await bot.send_rich_message(chat_id=chat_id,rich_message=rich_message)

result:

File ".../telebot/types.py", line 16726, in to_dict
    data['text'] = self.text.to_dict()
                   ^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'to_dict'

Using a RichText object:

rich_message = InputRichMessage(
    blocks = [
        InputRichBlockFooter(
            text = RichTextBold(
                text = "bold footer text"
            )
        )
    ]
)
await bot.send_rich_message(chat_id=chat_id,rich_message=rich_message)

result:

File ".../telebot/types.py", line 16696, in to_dict
    data['text'] = self.text.to_dict()
                   ^^^^^^^^^^^^^^^^^
AttributeError: 'RichTextBold' object has no attribute 'to_dict'

This also affects relaying received rich messages and trying to send them exactly as they were received, which I was attempting to do to test my bot's implementation, since received blocks contain nested RichText values that can be passed into the corresponding InputRichBlock objects. InputRichBlock*.to_dict() assumes "data['text'] = self.text.to_dict()" but RichText can recursively contain strings, lists, and other RichText* objects.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions