@mks2508/telegram-message-builder 0.2.0 → 0.3.1

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.
Files changed (57) hide show
  1. package/dist/builder/builder.d.ts +87 -0
  2. package/dist/builder/index.d.ts +2 -0
  3. package/dist/builder/media.d.ts +351 -0
  4. package/dist/formatters/index.d.ts +86 -0
  5. package/dist/formatters/markdown.d.ts +178 -0
  6. package/dist/formatters/markdownv2.d.ts +183 -0
  7. package/dist/index.cjs +1057 -12
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +11 -0
  10. package/dist/index.js +1022 -13
  11. package/dist/index.js.map +1 -1
  12. package/dist/keyboard/index.d.ts +113 -0
  13. package/dist/types/constants.d.ts +13 -0
  14. package/dist/types/core.types.d.ts +74 -0
  15. package/dist/types/index.d.ts +4 -0
  16. package/dist/types/keyboard-types.index.d.ts +1 -0
  17. package/dist/types/keyboard.types.d.ts +95 -0
  18. package/dist/types/main.types.d.ts +22 -0
  19. package/dist/types/media.types.d.ts +157 -0
  20. package/package.json +1 -1
  21. package/src/builder/builder.d.ts +55 -0
  22. package/src/builder/builder.d.ts.map +1 -1
  23. package/src/builder/builder.ts +145 -10
  24. package/src/builder/index.d.ts +2 -1
  25. package/src/builder/index.d.ts.map +1 -1
  26. package/src/builder/index.ts +2 -1
  27. package/src/builder/media.d.ts +352 -0
  28. package/src/builder/media.d.ts.map +1 -0
  29. package/src/builder/media.test.ts +664 -0
  30. package/src/builder/media.ts +484 -0
  31. package/src/builder.test.ts +465 -0
  32. package/src/escaping.test.ts +2 -2
  33. package/src/formatters/index.d.ts +47 -0
  34. package/src/formatters/index.d.ts.map +1 -1
  35. package/src/formatters/index.ts +92 -1
  36. package/src/formatters/markdown.d.ts +179 -0
  37. package/src/formatters/markdown.d.ts.map +1 -0
  38. package/src/formatters/markdown.test.ts +417 -0
  39. package/src/formatters/markdown.ts +220 -0
  40. package/src/formatters/markdownv2.d.ts +184 -0
  41. package/src/formatters/markdownv2.d.ts.map +1 -0
  42. package/src/formatters/markdownv2.ts +235 -0
  43. package/src/formatters.test.ts +17 -7
  44. package/src/index.d.ts +2 -0
  45. package/src/index.d.ts.map +1 -1
  46. package/src/index.ts +12 -0
  47. package/src/integration.test.ts +523 -0
  48. package/src/media-integration.test.ts +384 -0
  49. package/src/types/index.d.ts +1 -0
  50. package/src/types/index.d.ts.map +1 -1
  51. package/src/types/index.ts +1 -0
  52. package/src/types/media.types.d.ts +158 -0
  53. package/src/types/media.types.d.ts.map +1 -0
  54. package/src/types/media.types.ts +178 -0
  55. package/src/types.test.ts +539 -0
  56. package/src/utils/index.d.ts +1 -1
  57. package/src/utils/index.ts +0 -5
@@ -0,0 +1,352 @@
1
+ /**
2
+ * @fileoverview Media Builder
3
+ * @description Fluent API for building media messages for Telegram Bot API
4
+ * @module telegram-message-builder/builder
5
+ *
6
+ * @see {@link https://core.telegram.org/bots/api#sendphoto | Telegram Bot API - sendPhoto}
7
+ * @see {@link https://core.telegram.org/bots/api#sendvideo | Telegram Bot API - sendVideo}
8
+ * @see {@link https://core.telegram.org/bots/api#senddocument | Telegram Bot API - sendDocument}
9
+ * @see {@link https://core.telegram.org/bots/api#sendaudio | Telegram Bot API - sendAudio}
10
+ * @see {@link https://core.telegram.org/bots/api#sendvoice | Telegram Bot API - sendVoice}
11
+ */
12
+ import type { MediaSource, MediaType, IMediaCommonOptions, IMediaBuildResult, ParseMode } from "../types";
13
+ /**
14
+ * Telegram Media Builder - Fluent API for building media messages
15
+ *
16
+ * Provides a type-safe, fluent interface for constructing media messages
17
+ * compatible with Telegram Bot API v9.3+.
18
+ *
19
+ * Supports all three Telegram parse modes (HTML, Markdown, MarkdownV2) for caption formatting.
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * // Simple photo with caption
24
+ * const photo = TelegramMediaBuilder.photo("photo.jpg")
25
+ * .caption("My photo")
26
+ * .build();
27
+ *
28
+ * // Video with caption and options
29
+ * const video = TelegramMediaBuilder.video("video.mp4")
30
+ * .caption("My video")
31
+ * .duration(120)
32
+ * .thumbnail("thumb.jpg")
33
+ * .setParseMode("markdown")
34
+ * .build();
35
+ *
36
+ * // Document with custom options
37
+ * const doc = TelegramMediaBuilder.document("report.pdf")
38
+ * .caption("Q4 Report")
39
+ * .fileName("q4_2024.pdf")
40
+ * .mimeType("application/pdf")
41
+ * .build();
42
+ * ```
43
+ */
44
+ export declare class TelegramMediaBuilder<T extends MediaType> {
45
+ protected source: MediaSource;
46
+ protected mediaType: T;
47
+ protected _caption?: string;
48
+ protected parseMode: ParseMode;
49
+ protected options: IMediaCommonOptions;
50
+ protected constructor(source: MediaSource, mediaType: T);
51
+ /**
52
+ * Create a photo message builder
53
+ *
54
+ * @param source - File ID, URL, Buffer, or file path
55
+ * @returns Photo media builder with thumbnail() method
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * const photo = TelegramMediaBuilder.photo("https://example.com/photo.jpg")
60
+ * .caption("Beautiful sunset")
61
+ * .thumbnail("thumb.jpg")
62
+ * .build();
63
+ * ```
64
+ */
65
+ static photo(source: MediaSource): PhotoBuilder;
66
+ /**
67
+ * Create a video message builder
68
+ *
69
+ * @param source - File ID, URL, Buffer, or file path
70
+ * @returns Video media builder with duration(), width(), height(), thumbnail(), enableStreaming() methods
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const video = TelegramMediaBuilder.video("video.mp4")
75
+ * .caption("My video")
76
+ * .duration(120)
77
+ * .width(1920)
78
+ * .height(1080)
79
+ * .build();
80
+ * ```
81
+ */
82
+ static video(source: MediaSource): VideoBuilder;
83
+ /**
84
+ * Create a document message builder
85
+ *
86
+ * @param source - File ID, URL, Buffer, or file path
87
+ * @returns Document media builder with thumbnail(), fileName(), mimeType(), disableContentTypeDetection() methods
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * const doc = TelegramMediaBuilder.document("report.pdf")
92
+ * .caption("Q4 Report")
93
+ * .fileName("q4_2024.pdf")
94
+ * .mimeType("application/pdf")
95
+ * .build();
96
+ * ```
97
+ */
98
+ static document(source: MediaSource): DocumentBuilder;
99
+ /**
100
+ * Create an audio message builder
101
+ *
102
+ * @param source - File ID, URL, Buffer, or file path
103
+ * @returns Audio media builder with duration(), performer(), title(), thumbnail() methods
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * const audio = TelegramMediaBuilder.audio("song.mp3")
108
+ * .caption("My favorite song")
109
+ * .duration(180)
110
+ * .performer("Artist Name")
111
+ * .title("Song Title")
112
+ * .build();
113
+ * ```
114
+ */
115
+ static audio(source: MediaSource): AudioBuilder;
116
+ /**
117
+ * Create a voice message builder
118
+ *
119
+ * @param source - File ID, URL, Buffer, or file path
120
+ * @returns Voice media builder with duration() method
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * const voice = TelegramMediaBuilder.voice("voice.ogg")
125
+ * .caption("Listen to this")
126
+ * .duration(30)
127
+ * .build();
128
+ * ```
129
+ */
130
+ static voice(source: MediaSource): VoiceBuilder;
131
+ /**
132
+ * Set caption for the media
133
+ *
134
+ * @param text - Caption text (0-1024 characters)
135
+ * @returns This builder for chaining
136
+ */
137
+ caption(text: string): this;
138
+ /**
139
+ * Set parse mode for caption formatting
140
+ *
141
+ * @param mode - Parse mode (html, markdown, or markdownv2)
142
+ * @returns This builder for chaining
143
+ */
144
+ setParseMode(mode: ParseMode): this;
145
+ /**
146
+ * Set a custom option
147
+ *
148
+ * @param key - Option name
149
+ * @param value - Option value
150
+ * @returns This builder for chaining
151
+ */
152
+ setOption<K extends keyof IMediaCommonOptions>(key: K, value: IMediaCommonOptions[K]): this;
153
+ /**
154
+ * Set multiple options at once
155
+ *
156
+ * @param opts - Object with options to set
157
+ * @returns This builder for chaining
158
+ */
159
+ setOptions(opts: Partial<IMediaCommonOptions>): this;
160
+ /**
161
+ * Build the media message object
162
+ *
163
+ * @returns Complete media message ready for Telegram Bot API
164
+ */
165
+ build(): IMediaBuildResult;
166
+ /**
167
+ * Format caption based on current parse mode
168
+ *
169
+ * @param text - Caption text to format
170
+ * @returns Formatted caption
171
+ */
172
+ private formatCaption;
173
+ }
174
+ /**
175
+ * Photo-specific builder with convenience methods
176
+ *
177
+ * @example
178
+ * ```typescript
179
+ * const photo = TelegramMediaBuilder.photo("photo.jpg")
180
+ * .caption("My photo")
181
+ * .thumbnail("thumb.jpg")
182
+ * .build();
183
+ * ```
184
+ */
185
+ export declare class PhotoBuilder extends TelegramMediaBuilder<"photo"> {
186
+ /**
187
+ * Set thumbnail for the photo
188
+ *
189
+ * @param thumb - Thumbnail as Buffer or file path
190
+ * @returns This builder for chaining
191
+ */
192
+ thumbnail(thumb: Buffer | string): this;
193
+ }
194
+ /**
195
+ * Video-specific builder with convenience methods
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * const video = TelegramMediaBuilder.video("video.mp4")
200
+ * .caption("My video")
201
+ * .duration(120)
202
+ * .width(1920)
203
+ * .height(1080)
204
+ * .thumbnail("thumb.jpg")
205
+ * .enableStreaming()
206
+ * .build();
207
+ * ```
208
+ */
209
+ export declare class VideoBuilder extends TelegramMediaBuilder<"video"> {
210
+ /**
211
+ * Set video duration
212
+ *
213
+ * @param seconds - Duration in seconds
214
+ * @returns This builder for chaining
215
+ */
216
+ duration(seconds: number): this;
217
+ /**
218
+ * Set video width
219
+ *
220
+ * @param px - Width in pixels
221
+ * @returns This builder for chaining
222
+ */
223
+ width(px: number): this;
224
+ /**
225
+ * Set video height
226
+ *
227
+ * @param px - Height in pixels
228
+ * @returns This builder for chaining
229
+ */
230
+ height(px: number): this;
231
+ /**
232
+ * Set thumbnail for the video
233
+ *
234
+ * @param thumb - Thumbnail as Buffer or file path
235
+ * @returns This builder for chaining
236
+ */
237
+ thumbnail(thumb: Buffer | string): this;
238
+ /**
239
+ * Enable streaming for the video
240
+ *
241
+ * @returns This builder for chaining
242
+ */
243
+ enableStreaming(): this;
244
+ }
245
+ /**
246
+ * Document-specific builder with convenience methods
247
+ *
248
+ * @example
249
+ * ```typescript
250
+ * const doc = TelegramMediaBuilder.document("report.pdf")
251
+ * .caption("Q4 Report")
252
+ * .thumbnail("thumb.jpg")
253
+ * .fileName("q4_2024.pdf")
254
+ * .mimeType("application/pdf")
255
+ * .disableContentTypeDetection()
256
+ * .build();
257
+ * ```
258
+ */
259
+ export declare class DocumentBuilder extends TelegramMediaBuilder<"document"> {
260
+ /**
261
+ * Set thumbnail for the document
262
+ *
263
+ * @param thumb - Thumbnail as Buffer or file path
264
+ * @returns This builder for chaining
265
+ */
266
+ thumbnail(thumb: Buffer | string): this;
267
+ /**
268
+ * Set original filename
269
+ *
270
+ * @param name - File name
271
+ * @returns This builder for chaining
272
+ */
273
+ fileName(name: string): this;
274
+ /**
275
+ * Set MIME type
276
+ *
277
+ * @param type - MIME type string
278
+ * @returns This builder for chaining
279
+ */
280
+ mimeType(type: string): this;
281
+ /**
282
+ * Disable automatic file type detection
283
+ *
284
+ * @returns This builder for chaining
285
+ */
286
+ disableContentTypeDetection(): this;
287
+ }
288
+ /**
289
+ * Audio-specific builder with convenience methods
290
+ *
291
+ * @example
292
+ * ```typescript
293
+ * const audio = TelegramMediaBuilder.audio("song.mp3")
294
+ * .caption("My favorite song")
295
+ * .duration(180)
296
+ * .performer("Artist Name")
297
+ * .title("Song Title")
298
+ * .thumbnail("album_art.jpg")
299
+ * .build();
300
+ * ```
301
+ */
302
+ export declare class AudioBuilder extends TelegramMediaBuilder<"audio"> {
303
+ /**
304
+ * Set audio duration
305
+ *
306
+ * @param seconds - Duration in seconds
307
+ * @returns This builder for chaining
308
+ */
309
+ duration(seconds: number): this;
310
+ /**
311
+ * Set performer name
312
+ *
313
+ * @param name - Performer name
314
+ * @returns This builder for chaining
315
+ */
316
+ performer(name: string): this;
317
+ /**
318
+ * Set track title
319
+ *
320
+ * @param name - Track title
321
+ * @returns This builder for chaining
322
+ */
323
+ title(name: string): this;
324
+ /**
325
+ * Set thumbnail (album cover)
326
+ *
327
+ * @param thumb - Thumbnail as Buffer or file path
328
+ * @returns This builder for chaining
329
+ */
330
+ thumbnail(thumb: Buffer | string): this;
331
+ }
332
+ /**
333
+ * Voice-specific builder with convenience methods
334
+ *
335
+ * @example
336
+ * ```typescript
337
+ * const voice = TelegramMediaBuilder.voice("voice.ogg")
338
+ * .caption("Listen to this")
339
+ * .duration(30)
340
+ * .build();
341
+ * ```
342
+ */
343
+ export declare class VoiceBuilder extends TelegramMediaBuilder<"voice"> {
344
+ /**
345
+ * Set voice duration
346
+ *
347
+ * @param seconds - Duration in seconds
348
+ * @returns This builder for chaining
349
+ */
350
+ duration(seconds: number): this;
351
+ }
352
+ //# sourceMappingURL=media.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"media.d.ts","sourceRoot":"","sources":["media.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,SAAS,EACT,mBAAmB,EACnB,iBAAiB,EACjB,SAAS,EACV,MAAM,UAAU,CAAC;AAGlB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBAAa,oBAAoB,CAAC,CAAC,SAAS,SAAS;IACnD,SAAS,CAAC,MAAM,EAAE,WAAW,CAAC;IAC9B,SAAS,CAAC,SAAS,EAAE,CAAC,CAAC;IACvB,SAAS,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC5B,SAAS,CAAC,SAAS,EAAE,SAAS,CAAU;IACxC,SAAS,CAAC,OAAO,EAAE,mBAAmB,CAAM;IAE5C,SAAS,aAAa,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,EAGtD;IAED;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,YAAY,CAE9C;IAED;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,YAAY,CAE9C;IAED;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,WAAW,GA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