FormattedString
Represents the formatted string after parsing. This class provides a unified interface for working with formatted text that can be used as both message text and caption content in Telegram Bot API calls.
Implements
Constructors
FormattedString(rawText: string, rawEntities?: MessageEntity[]);
Creates a new Formatted
instance.
Properties
rawEntities
rawEntities: MessageEntity[];
The entities backing this FormattedString.
rawText
rawText: string;
The plain text content
Methods
caption (getter)
get caption();
Gets the caption text. This is an alias for the raw text content. Used when this FormattedString is used as caption content.
text (getter)
get text();
Gets the plain text content. This is an alias for the raw text content. Used when this FormattedString is used as message text.
title (getter)
get title();
Gets the title. Alias for rawText. Used when this FormattedString is used as title.
text_entities (getter)
get text_entities();
Gets the text_entities. Alias for rawEntities. Used when this FormattedString is used as text with text_entities.
title_entities (getter)
get title_entities();
Gets the title_entities. Alias for rawEntities. Used when this FormattedString is used as title_entities.
caption_entities (getter)
get caption_entities();
Gets the caption entities. This is an alias for the raw entities. Used when this FormattedString is used as caption content.
entities (getter)
get entities();
Gets the message entities. This is an alias for the raw entities. Used when this FormattedString is used as message text.
toString
toString();
Returns the string representation of this Formatted
object
b
b(text: Stringable);
Combines this FormattedString with a bold formatted string
bold
bold(text: Stringable);
Combines this FormattedString with a bold formatted string
i
i(text: Stringable);
Combines this FormattedString with an italic formatted string
italic
italic(text: Stringable);
Combines this FormattedString with an italic formatted string
s
s(text: Stringable);
Combines this FormattedString with a strikethrough formatted string
strikethrough
strikethrough(text: Stringable);
Combines this FormattedString with a strikethrough formatted string
u
u(text: Stringable);
Combines this FormattedString with an underline formatted string
underline
underline(text: Stringable);
Combines this FormattedString with an underline formatted string
a
a(text: Stringable, url: string);
Combines this FormattedString with a link formatted string
link
link(text: Stringable, url: string);
Combines this FormattedString with a link formatted string
code
code(text: Stringable);
Combines this FormattedString with a code formatted string
pre
pre(text: Stringable, language: string);
Combines this FormattedString with a pre formatted string (code block)
spoiler
spoiler(text: Stringable);
Combines this FormattedString with a spoiler formatted string
blockquote
blockquote(text: Stringable);
Combines this FormattedString with a blockquote formatted string
expandableBlockquote
expandableBlockquote(text: Stringable);
Combines this FormattedString with an expandable blockquote formatted string
mentionUser
mentionUser(text: Stringable, userId: number);
Combines this FormattedString with a user mention formatted string
customEmoji
customEmoji(placeholder: Stringable, emoji: string);
Combines this FormattedString with a custom emoji formatted string
linkMessage
linkMessage(
text: Stringable,
chatId: number,
messageId: number,
);
Combines this FormattedString with a message link formatted string
plain
plain(text: string);
Combines this FormattedString with plain text
split
split(separator: FormattedString): FormattedString[];
Splits this FormattedString into an array of FormattedStrings using a separator
splitByText
splitByText(separator: FormattedString): FormattedString[];
Splits this FormattedString into an array of FormattedStrings using a separator, ignoring inequalities in rawEntities. Only uses rawText to determine if this is a valid position to split.
slice
slice(start?: number, end?: number): FormattedString;
Returns a copy of a portion of this FormattedString
_findMatches
_findMatches(pattern: FormattedString, options: {
findAll?: boolean; allowOverlapping?: boolean; matchByTextOnly?: boolean
}): number[];
Protected method that finds pattern matches within this FormattedString.
find
find(pattern: FormattedString): number;
Finds the first occurrence of a FormattedString pattern within this FormattedString that matches both the raw text and raw entities exactly.
findAll
findAll(pattern: FormattedString, allowOverlapping: boolean): number[];
Finds all occurrences of a FormattedString pattern within this FormattedString that match both the raw text and raw entities exactly.
replaceMatches
replaceMatches(
pattern: FormattedString,
replacement: FormattedString,
matchOffsets: number[],
): FormattedString;
Protected method to replace matches at given offsets with a replacement.
replace
replace(pattern: FormattedString, replacement: FormattedString): FormattedString;
Returns a new FormattedString with the first occurrence of pattern replaced by replacement. Both the raw text and raw entities must match exactly for replacement to occur.
replaceAll
replaceAll(pattern: FormattedString, replacement: FormattedString): FormattedString;
Returns a new FormattedString with all occurrences of pattern replaced by replacement. Both the raw text and raw entities must match exactly for replacement to occur.
concat
concat(...formattedStrings: FormattedString[]): FormattedString;
Concatenates this FormattedString with one or more other FormattedStrings
startsWith
startsWith(pattern: FormattedString): boolean;
Checks whether this FormattedString starts with the specified pattern. Both the raw text and raw entities must match exactly.
endsWith
endsWith(pattern: FormattedString): boolean;
Checks whether this FormattedString ends with the specified pattern. Both the raw text and raw entities must match exactly.
Static Methods
b
b(text: Stringable);
Creates a bold formatted string
bold
bold(text: Stringable);
Creates a bold formatted string
i
i(text: Stringable);
Creates an italic formatted string
italic
italic(text: Stringable);
Creates an italic formatted string
s
s(text: Stringable);
Creates a strikethrough formatted string
strikethrough
strikethrough(text: Stringable);
Creates a strikethrough formatted string
u
u(text: Stringable);
Creates an underline formatted string
underline
underline(text: Stringable);
Creates an underline formatted string
a
a(text: Stringable, url: string);
Creates a link formatted string
link
link(text: Stringable, url: string);
Creates a link formatted string
code
code(text: Stringable);
Creates a code formatted string
pre
pre(text: Stringable, language: string);
Creates a pre formatted string (code block)
spoiler
spoiler(text: Stringable);
Creates a spoiler formatted string
blockquote
blockquote(text: Stringable);
Creates a blockquote formatted string
expandableBlockquote
expandableBlockquote(text: Stringable);
Creates an expandable blockquote formatted string
mentionUser
mentionUser(text: Stringable, userId: number);
Creates a user mention formatted string
customEmoji
customEmoji(placeholder: Stringable, emoji: string);
Creates a custom emoji formatted string
linkMessage
linkMessage(
text: Stringable,
chatId: number,
messageId: number,
);
Creates a message link formatted string
join
join(items: (Stringable | TextWithEntities | CaptionWithEntities | string)[], separator?: Stringable | TextWithEntities | CaptionWithEntities | string);
Joins an array of formatted strings or plain text into a single FormattedString
_split
_split(
text: FormattedString,
separator: FormattedString,
isSplitByTextOnly: boolean,
): FormattedString[];
Internal method that implements the shared splitting logic for both split and splitByText methods
split
split(text: FormattedString, separator: FormattedString): FormattedString[];
Splits a FormattedString into an array of FormattedStrings using a separator
splitByText
splitByText(text: FormattedString, separator: FormattedString): FormattedString[];
Splits a FormattedString into an array of FormattedStrings using a separator, ignoring inequalities in rawEntities. Only uses rawText to determine if this is a valid position to split.
startsWith
startsWith(source: FormattedString, pattern: FormattedString): boolean;
Static method to check whether a FormattedString starts with the specified pattern. Both the raw text and raw entities must match exactly.
endsWith
endsWith(source: FormattedString, pattern: FormattedString): boolean;
Static method to check whether a FormattedString ends with the specified pattern. Both the raw text and raw entities must match exactly.