@sunafterrainwm/telegram-entities-builder 0.1.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/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, sunafterrainwm <sunafterrainwm@gmail.com>
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,240 @@
1
+ import * as TT from '@grammyjs/types';
2
+
3
+ type Telegram = TT.ApiMethods<never>;
4
+ type ApiParameters<T extends keyof Telegram> = Parameters<Telegram[T]>[0];
5
+ /**
6
+ * Represents a Telegram MessageEntity without the offset and length properties.
7
+ */
8
+ type PartialEntity<E extends TT.MessageEntity = TT.MessageEntity> = Omit<E, 'offset' | 'length'>;
9
+ /**
10
+ * Represents any draft entity that can be used before calculating the final offset and length.
11
+ */
12
+ type AnyDraftEntity = PartialEntity<TT.MessageEntity.CommonMessageEntity> | PartialEntity<TT.MessageEntity.CustomEmojiMessageEntity> | PartialEntity<TT.MessageEntity.PreMessageEntity> | PartialEntity<TT.MessageEntity.TextLinkMessageEntity> | PartialEntity<TT.MessageEntity.TextMentionMessageEntity>;
13
+ /**
14
+ * Represents a segment of text which may contain one or multiple entities.
15
+ */
16
+ type TextSegment = TextSegment.SingleTextSegment | TextSegment.MultiTextSegment;
17
+ /**
18
+ * Namespace for TextSegment types.
19
+ */
20
+ declare namespace TextSegment {
21
+ interface SingleTextSegment {
22
+ text: string;
23
+ entity?: AnyDraftEntity;
24
+ entities?: never;
25
+ }
26
+ interface MultiTextSegment {
27
+ text: string;
28
+ entity?: never;
29
+ entities: AnyDraftEntity[];
30
+ }
31
+ }
32
+ type PartialRequired<T, REQUIRED extends keyof T, OPTIONAL extends keyof T> = Required<Pick<T, REQUIRED>> & Pick<T, OPTIONAL>;
33
+ /**
34
+ * Base interface for entity builders, providing fundamental operations for appending text and managing builder state.
35
+ */
36
+ interface IEntityBuilderBase {
37
+ /**
38
+ * Appends plain text.
39
+ */
40
+ addText(text: string): this;
41
+ /**
42
+ * Appends text with a single entity.
43
+ */
44
+ addTextEntity(text: string, entity: AnyDraftEntity): this;
45
+ /**
46
+ * Appends text with multiple entities.
47
+ */
48
+ addTextEntities(text: string, entities: AnyDraftEntity[]): this;
49
+ /**
50
+ * Appends a list of text segments.
51
+ */
52
+ addTextSegmentList(segments: TextSegment[]): this;
53
+ /**
54
+ * Creates a deep clone of this builder.
55
+ */
56
+ clone(): IEntityBuilderBase;
57
+ /**
58
+ * Creates a child builder that forks from this instance.
59
+ */
60
+ fork(): IEntityBuilderBase;
61
+ /**
62
+ * Merges a forked builder back into its parent.
63
+ */
64
+ merge(wrapperEntities?: AnyDraftEntity[]): void;
65
+ }
66
+ /**
67
+ * Advanced entity builder interface supporting string manipulation, entity sorting, and payload generation.
68
+ */
69
+ interface IEntityBuilder extends IEntityBuilderBase {
70
+ /**
71
+ * Trims or slices the string in-place, and automatically recalculates, filters, or truncates the affected entities.
72
+ */
73
+ sliceInplace(start?: number, end?: number): this;
74
+ /**
75
+ * Returns a new sliced instance.
76
+ */
77
+ slice(start?: number, end?: number): IEntityBuilder;
78
+ /**
79
+ * Removes leading whitespace in-place.
80
+ */
81
+ trimStart(): this;
82
+ /**
83
+ * Removes trailing whitespace in-place.
84
+ */
85
+ trimEnd(): this;
86
+ /**
87
+ * Removes leading and trailing whitespace in-place.
88
+ */
89
+ trim(): this;
90
+ /**
91
+ * Sorts the entities based on their offset and length.
92
+ */
93
+ sortEntities(): this;
94
+ /**
95
+ * Builds the payload for sending a text message.
96
+ */
97
+ buildTextPayload(): PartialRequired<ApiParameters<'sendMessage'>, 'text', 'entities' | 'link_preview_options'>;
98
+ /**
99
+ * Builds the payload for sending a document with a caption.
100
+ */
101
+ buildCaptionPayload(): PartialRequired<ApiParameters<'sendDocument'>, 'caption', 'caption_entities'>;
102
+ /**
103
+ * Builds the payload for inline query results.
104
+ */
105
+ buildInlinePayload(): PartialRequired<TT.InputTextMessageContent, 'message_text', 'entities' | 'link_preview_options'>;
106
+ clone(): IEntityBuilder;
107
+ fork(): IEntityBuilder;
108
+ }
109
+ /**
110
+ * Main implementation of IEntityBuilder that eagerly evaluates and stores entities.
111
+ */
112
+ declare class EntityBuilder implements IEntityBuilder {
113
+ #private;
114
+ addText(text: string): this;
115
+ addTextEntity(text: string, entity: AnyDraftEntity): this;
116
+ addTextEntities(text: string, entities: AnyDraftEntity[]): this;
117
+ addTextSegmentList(segments: TextSegment[]): this;
118
+ buildTextPayload(): {
119
+ text: string;
120
+ entities: TT.MessageEntity[];
121
+ };
122
+ buildCaptionPayload(): {
123
+ caption: string;
124
+ caption_entities: TT.MessageEntity[];
125
+ };
126
+ buildInlinePayload(): {
127
+ message_text: string;
128
+ entities: TT.MessageEntity[];
129
+ };
130
+ sortEntities(): this;
131
+ sliceInplace(start?: number, end?: number): this;
132
+ slice(start?: number, end?: number): EntityBuilder;
133
+ trimStart(): this;
134
+ trimEnd(): this;
135
+ trim(): this;
136
+ clone(): EntityBuilder;
137
+ fork(): EntityBuilder;
138
+ /**
139
+ * Merges a payload text and its entities into this builder, optionally wrapping them with additional entities.
140
+ */
141
+ mergePayload(text: string, entities: TT.MessageEntity[], wrappers?: AnyDraftEntity[]): void;
142
+ merge(wrapperEntities?: AnyDraftEntity[]): void;
143
+ }
144
+ /**
145
+ * A lazy implementation of IEntityBuilderBase that stores segments and flattens them into an EntityBuilder only when needed.
146
+ */
147
+ declare class LazyEntityBuilder implements IEntityBuilderBase {
148
+ #private;
149
+ /**
150
+ * Flattens the lazy segments into a new EntityBuilder instance.
151
+ */
152
+ flatten(): EntityBuilder;
153
+ addText(text: string): this;
154
+ addTextEntity(text: string, entity: AnyDraftEntity): this;
155
+ addTextEntities(text: string, entities: AnyDraftEntity[]): this;
156
+ addTextSegmentList(segments: TextSegment[]): this;
157
+ clone(): LazyEntityBuilder;
158
+ fork(): LazyEntityBuilder;
159
+ merge(wrapperEntities?: AnyDraftEntity[]): void;
160
+ }
161
+ /**
162
+ * Abstract proxy class that delegates all IEntityBuilder operations to an underlying builder instance.
163
+ */
164
+ declare abstract class EntityBuilderProxy<THIS extends IEntityBuilder = IEntityBuilder> implements IEntityBuilder {
165
+ protected _entities: IEntityBuilder;
166
+ protected constructor(_entities: IEntityBuilder);
167
+ get entities(): IEntityBuilder;
168
+ addText(...args: Parameters<IEntityBuilder['addText']>): this;
169
+ addTextEntity(...args: Parameters<IEntityBuilder['addTextEntity']>): this;
170
+ addTextEntities(...args: Parameters<IEntityBuilder['addTextEntities']>): this;
171
+ addTextSegmentList(...args: Parameters<IEntityBuilder['addTextSegmentList']>): this;
172
+ sliceInplace(...args: Parameters<IEntityBuilder['sliceInplace']>): this;
173
+ slice(...args: Parameters<IEntityBuilder['slice']>): THIS;
174
+ trim(): this;
175
+ trimStart(): this;
176
+ trimEnd(): this;
177
+ sortEntities(): this;
178
+ clone(): THIS;
179
+ fork(): IEntityBuilder;
180
+ merge(...args: Parameters<IEntityBuilder['merge']>): void;
181
+ buildTextPayload(): PartialRequired<{
182
+ business_connection_id?: string;
183
+ chat_id: number | string;
184
+ message_thread_id?: number;
185
+ text: string;
186
+ parse_mode?: TT.ParseMode;
187
+ entities?: TT.MessageEntity[];
188
+ link_preview_options?: TT.LinkPreviewOptions;
189
+ disable_notification?: boolean;
190
+ protect_content?: boolean;
191
+ allow_paid_broadcast?: boolean;
192
+ message_effect_id?: string;
193
+ reply_parameters?: TT.ReplyParameters;
194
+ reply_markup?: TT.InlineKeyboardMarkup | TT.ReplyKeyboardMarkup | TT.ReplyKeyboardRemove | TT.ForceReply;
195
+ reply_to_message_id?: number;
196
+ }, "text", "entities" | "link_preview_options">;
197
+ buildCaptionPayload(): {
198
+ caption: string;
199
+ caption_entities: TT.MessageEntity[] | undefined;
200
+ };
201
+ buildInlinePayload(): {
202
+ message_text: string;
203
+ entities: TT.MessageEntity[] | undefined;
204
+ link_preview_options: TT.LinkPreviewOptions | undefined;
205
+ };
206
+ }
207
+ /**
208
+ * Escapes a string to be used as a valid tag by removing or replacing invalid characters.
209
+ */
210
+ declare function escapeTag(input: string): string;
211
+ declare const SymbolMessageComposer: unique symbol;
212
+ /**
213
+ * A high-level composer that wraps an IEntityBuilder and provides additional features like tags and link preview options.
214
+ */
215
+ declare class MessageComposer extends EntityBuilderProxy<MessageComposer> {
216
+ #private;
217
+ protected [SymbolMessageComposer]: true;
218
+ /**
219
+ * Options for link preview generation.
220
+ */
221
+ linkPreviewOptions?: TT.LinkPreviewOptions;
222
+ constructor(entities?: IEntityBuilder & {
223
+ [SymbolMessageComposer]?: never;
224
+ });
225
+ /**
226
+ * Gets the current list of tags.
227
+ */
228
+ get tags(): string[];
229
+ /**
230
+ * Adds one or more tags, escaping them and avoiding duplicates.
231
+ */
232
+ addTags(...tags: string[]): this;
233
+ buildTextPayload(): {
234
+ link_preview_options: TT.LinkPreviewOptions | undefined;
235
+ text: string;
236
+ entities?: TT.MessageEntity[] | undefined;
237
+ };
238
+ }
239
+
240
+ export { type AnyDraftEntity, EntityBuilder, EntityBuilderProxy, type IEntityBuilder, type IEntityBuilderBase, LazyEntityBuilder, MessageComposer, type PartialEntity, TextSegment, escapeTag };
@@ -0,0 +1,240 @@
1
+ import * as TT from '@grammyjs/types';
2
+
3
+ type Telegram = TT.ApiMethods<never>;
4
+ type ApiParameters<T extends keyof Telegram> = Parameters<Telegram[T]>[0];
5
+ /**
6
+ * Represents a Telegram MessageEntity without the offset and length properties.
7
+ */
8
+ type PartialEntity<E extends TT.MessageEntity = TT.MessageEntity> = Omit<E, 'offset' | 'length'>;
9
+ /**
10
+ * Represents any draft entity that can be used before calculating the final offset and length.
11
+ */
12
+ type AnyDraftEntity = PartialEntity<TT.MessageEntity.CommonMessageEntity> | PartialEntity<TT.MessageEntity.CustomEmojiMessageEntity> | PartialEntity<TT.MessageEntity.PreMessageEntity> | PartialEntity<TT.MessageEntity.TextLinkMessageEntity> | PartialEntity<TT.MessageEntity.TextMentionMessageEntity>;
13
+ /**
14
+ * Represents a segment of text which may contain one or multiple entities.
15
+ */
16
+ type TextSegment = TextSegment.SingleTextSegment | TextSegment.MultiTextSegment;
17
+ /**
18
+ * Namespace for TextSegment types.
19
+ */
20
+ declare namespace TextSegment {
21
+ interface SingleTextSegment {
22
+ text: string;
23
+ entity?: AnyDraftEntity;
24
+ entities?: never;
25
+ }
26
+ interface MultiTextSegment {
27
+ text: string;
28
+ entity?: never;
29
+ entities: AnyDraftEntity[];
30
+ }
31
+ }
32
+ type PartialRequired<T, REQUIRED extends keyof T, OPTIONAL extends keyof T> = Required<Pick<T, REQUIRED>> & Pick<T, OPTIONAL>;
33
+ /**
34
+ * Base interface for entity builders, providing fundamental operations for appending text and managing builder state.
35
+ */
36
+ interface IEntityBuilderBase {
37
+ /**
38
+ * Appends plain text.
39
+ */
40
+ addText(text: string): this;
41
+ /**
42
+ * Appends text with a single entity.
43
+ */
44
+ addTextEntity(text: string, entity: AnyDraftEntity): this;
45
+ /**
46
+ * Appends text with multiple entities.
47
+ */
48
+ addTextEntities(text: string, entities: AnyDraftEntity[]): this;
49
+ /**
50
+ * Appends a list of text segments.
51
+ */
52
+ addTextSegmentList(segments: TextSegment[]): this;
53
+ /**
54
+ * Creates a deep clone of this builder.
55
+ */
56
+ clone(): IEntityBuilderBase;
57
+ /**
58
+ * Creates a child builder that forks from this instance.
59
+ */
60
+ fork(): IEntityBuilderBase;
61
+ /**
62
+ * Merges a forked builder back into its parent.
63
+ */
64
+ merge(wrapperEntities?: AnyDraftEntity[]): void;
65
+ }
66
+ /**
67
+ * Advanced entity builder interface supporting string manipulation, entity sorting, and payload generation.
68
+ */
69
+ interface IEntityBuilder extends IEntityBuilderBase {
70
+ /**
71
+ * Trims or slices the string in-place, and automatically recalculates, filters, or truncates the affected entities.
72
+ */
73
+ sliceInplace(start?: number, end?: number): this;
74
+ /**
75
+ * Returns a new sliced instance.
76
+ */
77
+ slice(start?: number, end?: number): IEntityBuilder;
78
+ /**
79
+ * Removes leading whitespace in-place.
80
+ */
81
+ trimStart(): this;
82
+ /**
83
+ * Removes trailing whitespace in-place.
84
+ */
85
+ trimEnd(): this;
86
+ /**
87
+ * Removes leading and trailing whitespace in-place.
88
+ */
89
+ trim(): this;
90
+ /**
91
+ * Sorts the entities based on their offset and length.
92
+ */
93
+ sortEntities(): this;
94
+ /**
95
+ * Builds the payload for sending a text message.
96
+ */
97
+ buildTextPayload(): PartialRequired<ApiParameters<'sendMessage'>, 'text', 'entities' | 'link_preview_options'>;
98
+ /**
99
+ * Builds the payload for sending a document with a caption.
100
+ */
101
+ buildCaptionPayload(): PartialRequired<ApiParameters<'sendDocument'>, 'caption', 'caption_entities'>;
102
+ /**
103
+ * Builds the payload for inline query results.
104
+ */
105
+ buildInlinePayload(): PartialRequired<TT.InputTextMessageContent, 'message_text', 'entities' | 'link_preview_options'>;
106
+ clone(): IEntityBuilder;
107
+ fork(): IEntityBuilder;
108
+ }
109
+ /**
110
+ * Main implementation of IEntityBuilder that eagerly evaluates and stores entities.
111
+ */
112
+ declare class EntityBuilder implements IEntityBuilder {
113
+ #private;
114
+ addText(text: string): this;
115
+ addTextEntity(text: string, entity: AnyDraftEntity): this;
116
+ addTextEntities(text: string, entities: AnyDraftEntity[]): this;
117
+ addTextSegmentList(segments: TextSegment[]): this;
118
+ buildTextPayload(): {
119
+ text: string;
120
+ entities: TT.MessageEntity[];
121
+ };
122
+ buildCaptionPayload(): {
123
+ caption: string;
124
+ caption_entities: TT.MessageEntity[];
125
+ };
126
+ buildInlinePayload(): {
127
+ message_text: string;
128
+ entities: TT.MessageEntity[];
129
+ };
130
+ sortEntities(): this;
131
+ sliceInplace(start?: number, end?: number): this;
132
+ slice(start?: number, end?: number): EntityBuilder;
133
+ trimStart(): this;
134
+ trimEnd(): this;
135
+ trim(): this;
136
+ clone(): EntityBuilder;
137
+ fork(): EntityBuilder;
138
+ /**
139
+ * Merges a payload text and its entities into this builder, optionally wrapping them with additional entities.
140
+ */
141
+ mergePayload(text: string, entities: TT.MessageEntity[], wrappers?: AnyDraftEntity[]): void;
142
+ merge(wrapperEntities?: AnyDraftEntity[]): void;
143
+ }
144
+ /**
145
+ * A lazy implementation of IEntityBuilderBase that stores segments and flattens them into an EntityBuilder only when needed.
146
+ */
147
+ declare class LazyEntityBuilder implements IEntityBuilderBase {
148
+ #private;
149
+ /**
150
+ * Flattens the lazy segments into a new EntityBuilder instance.
151
+ */
152
+ flatten(): EntityBuilder;
153
+ addText(text: string): this;
154
+ addTextEntity(text: string, entity: AnyDraftEntity): this;
155
+ addTextEntities(text: string, entities: AnyDraftEntity[]): this;
156
+ addTextSegmentList(segments: TextSegment[]): this;
157
+ clone(): LazyEntityBuilder;
158
+ fork(): LazyEntityBuilder;
159
+ merge(wrapperEntities?: AnyDraftEntity[]): void;
160
+ }
161
+ /**
162
+ * Abstract proxy class that delegates all IEntityBuilder operations to an underlying builder instance.
163
+ */
164
+ declare abstract class EntityBuilderProxy<THIS extends IEntityBuilder = IEntityBuilder> implements IEntityBuilder {
165
+ protected _entities: IEntityBuilder;
166
+ protected constructor(_entities: IEntityBuilder);
167
+ get entities(): IEntityBuilder;
168
+ addText(...args: Parameters<IEntityBuilder['addText']>): this;
169
+ addTextEntity(...args: Parameters<IEntityBuilder['addTextEntity']>): this;
170
+ addTextEntities(...args: Parameters<IEntityBuilder['addTextEntities']>): this;
171
+ addTextSegmentList(...args: Parameters<IEntityBuilder['addTextSegmentList']>): this;
172
+ sliceInplace(...args: Parameters<IEntityBuilder['sliceInplace']>): this;
173
+ slice(...args: Parameters<IEntityBuilder['slice']>): THIS;
174
+ trim(): this;
175
+ trimStart(): this;
176
+ trimEnd(): this;
177
+ sortEntities(): this;
178
+ clone(): THIS;
179
+ fork(): IEntityBuilder;
180
+ merge(...args: Parameters<IEntityBuilder['merge']>): void;
181
+ buildTextPayload(): PartialRequired<{
182
+ business_connection_id?: string;
183
+ chat_id: number | string;
184
+ message_thread_id?: number;
185
+ text: string;
186
+ parse_mode?: TT.ParseMode;
187
+ entities?: TT.MessageEntity[];
188
+ link_preview_options?: TT.LinkPreviewOptions;
189
+ disable_notification?: boolean;
190
+ protect_content?: boolean;
191
+ allow_paid_broadcast?: boolean;
192
+ message_effect_id?: string;
193
+ reply_parameters?: TT.ReplyParameters;
194
+ reply_markup?: TT.InlineKeyboardMarkup | TT.ReplyKeyboardMarkup | TT.ReplyKeyboardRemove | TT.ForceReply;
195
+ reply_to_message_id?: number;
196
+ }, "text", "entities" | "link_preview_options">;
197
+ buildCaptionPayload(): {
198
+ caption: string;
199
+ caption_entities: TT.MessageEntity[] | undefined;
200
+ };
201
+ buildInlinePayload(): {
202
+ message_text: string;
203
+ entities: TT.MessageEntity[] | undefined;
204
+ link_preview_options: TT.LinkPreviewOptions | undefined;
205
+ };
206
+ }
207
+ /**
208
+ * Escapes a string to be used as a valid tag by removing or replacing invalid characters.
209
+ */
210
+ declare function escapeTag(input: string): string;
211
+ declare const SymbolMessageComposer: unique symbol;
212
+ /**
213
+ * A high-level composer that wraps an IEntityBuilder and provides additional features like tags and link preview options.
214
+ */
215
+ declare class MessageComposer extends EntityBuilderProxy<MessageComposer> {
216
+ #private;
217
+ protected [SymbolMessageComposer]: true;
218
+ /**
219
+ * Options for link preview generation.
220
+ */
221
+ linkPreviewOptions?: TT.LinkPreviewOptions;
222
+ constructor(entities?: IEntityBuilder & {
223
+ [SymbolMessageComposer]?: never;
224
+ });
225
+ /**
226
+ * Gets the current list of tags.
227
+ */
228
+ get tags(): string[];
229
+ /**
230
+ * Adds one or more tags, escaping them and avoiding duplicates.
231
+ */
232
+ addTags(...tags: string[]): this;
233
+ buildTextPayload(): {
234
+ link_preview_options: TT.LinkPreviewOptions | undefined;
235
+ text: string;
236
+ entities?: TT.MessageEntity[] | undefined;
237
+ };
238
+ }
239
+
240
+ export { type AnyDraftEntity, EntityBuilder, EntityBuilderProxy, type IEntityBuilder, type IEntityBuilderBase, LazyEntityBuilder, MessageComposer, type PartialEntity, TextSegment, escapeTag };