teledzik 1.0.4 → 1.0.5
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/README.md +37 -0
- package/lib/context.js +34 -9
- package/lib/index.js +6 -1
- package/lib/telegram.js +90 -11
- package/package.json +2 -2
- package/src/context.ts +60 -20
- package/src/core/types/rich-message.ts +15 -0
- package/src/index.ts +1 -0
- package/src/telegram.ts +117 -13
- package/typings/context.d.ts +4 -2
- package/typings/core/types/rich-message.d.ts +20 -0
- package/typings/index.d.ts +1 -0
- package/typings/telegram.d.ts +4 -2
package/README.md
CHANGED
|
@@ -588,6 +588,43 @@ await ctx.sendRichMessage(msg, {
|
|
|
588
588
|
|
|
589
589
|
---
|
|
590
590
|
|
|
591
|
+
### Universal `editRichMessage` (Omni-Format Support)
|
|
592
|
+
|
|
593
|
+
Update existing messages or media captions in-place with **any** content format. It automatically detects Text vs Media (Photo/Video/Audio/Doc) and silently handles Telegram's `message is not modified` without throwing errors:
|
|
594
|
+
|
|
595
|
+
```ts
|
|
596
|
+
import { Telegraf, RichHTMLBuilder, Markup } from 'teledzik'
|
|
597
|
+
|
|
598
|
+
bot.action('btn:update', async (ctx) => {
|
|
599
|
+
// 1. Using RichHTMLBuilder instance directly
|
|
600
|
+
const rich = new RichHTMLBuilder()
|
|
601
|
+
.heading(1, '⚡ Server Stats Updated')
|
|
602
|
+
.table([
|
|
603
|
+
['Node', 'RAM', 'Status'],
|
|
604
|
+
['VPS-01', '3.8 GB', 'ONLINE 🟢'],
|
|
605
|
+
['VPS-02', '7.2 GB', 'ONLINE 🟢']
|
|
606
|
+
], { bordered: true, striped: true, hasHeader: true })
|
|
607
|
+
|
|
608
|
+
await ctx.editRichMessage(rich, {
|
|
609
|
+
reply_markup: Markup.inlineKeyboard([
|
|
610
|
+
[Markup.button.primary('🔄 Refresh', 'btn:update')]
|
|
611
|
+
]).reply_markup
|
|
612
|
+
})
|
|
613
|
+
|
|
614
|
+
// 2. Or using an Object
|
|
615
|
+
await ctx.editRichMessage({
|
|
616
|
+
html: '<b>New Content</b>',
|
|
617
|
+
reply_markup: { inline_keyboard: [] }
|
|
618
|
+
})
|
|
619
|
+
|
|
620
|
+
// 3. Or using Raw HTML String
|
|
621
|
+
await ctx.editRichMessage('<b>Updated via raw string</b>')
|
|
622
|
+
|
|
623
|
+
// 4. Or specifying a custom messageId
|
|
624
|
+
await ctx.editRichMessage(12345, '<b>Edit specific message ID</b>')
|
|
625
|
+
})
|
|
626
|
+
```
|
|
627
|
+
|
|
591
628
|
### Inline / Web App Queries
|
|
592
629
|
|
|
593
630
|
Use `InputRichMessageContent` as `input_message_content` in inline query results:
|
package/lib/context.js
CHANGED
|
@@ -1069,12 +1069,39 @@ class Context {
|
|
|
1069
1069
|
this.assert(this.chat, 'sendRichMessageDraft');
|
|
1070
1070
|
return this.telegram.sendRichMessageDraft(this.chat.id, draftId, richMessage, extra);
|
|
1071
1071
|
}
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1072
|
+
async editRichMessage(a, b, c) {
|
|
1073
|
+
let targetMsgId;
|
|
1074
|
+
let content;
|
|
1075
|
+
let extra = {};
|
|
1076
|
+
if (typeof a === 'number' ||
|
|
1077
|
+
(typeof a === 'string' && !isNaN(Number(a)) && b !== undefined)) {
|
|
1078
|
+
targetMsgId = a;
|
|
1079
|
+
content = b;
|
|
1080
|
+
extra = c || {};
|
|
1081
|
+
}
|
|
1082
|
+
else {
|
|
1083
|
+
targetMsgId =
|
|
1084
|
+
this.callbackQuery?.message?.message_id ??
|
|
1085
|
+
(this.message && 'message_id' in this.message ? this.message.message_id : undefined) ??
|
|
1086
|
+
(this.update && 'edited_message' in this.update && this.update.edited_message
|
|
1087
|
+
? this.update.edited_message.message_id
|
|
1088
|
+
: undefined) ??
|
|
1089
|
+
this.msgId;
|
|
1090
|
+
content = a;
|
|
1091
|
+
extra = b || {};
|
|
1092
|
+
}
|
|
1093
|
+
const chatId = this.chat?.id ??
|
|
1094
|
+
(this.callbackQuery?.message && 'chat' in this.callbackQuery.message
|
|
1095
|
+
? this.callbackQuery.message.chat.id
|
|
1096
|
+
: undefined);
|
|
1097
|
+
const inlineMsgId = this.inlineMessageId ?? extra?.inline_message_id;
|
|
1098
|
+
if (!inlineMsgId && (!chatId || targetMsgId == null)) {
|
|
1099
|
+
throw new Error('[teledzik] Tidak dapat menemukan chatId atau messageId dari konteks pesan saat ini.');
|
|
1100
|
+
}
|
|
1101
|
+
if (inlineMsgId && !extra.inline_message_id) {
|
|
1102
|
+
extra.inline_message_id = inlineMsgId;
|
|
1103
|
+
}
|
|
1104
|
+
return this.telegram.editRichMessage(chatId, targetMsgId, content, extra);
|
|
1078
1105
|
}
|
|
1079
1106
|
/**
|
|
1080
1107
|
* Enqueue a Telegram API call for the current chat to prevent 429 Too Many Requests errors.
|
|
@@ -1241,9 +1268,7 @@ const Msg = {
|
|
|
1241
1268
|
return 'date' in this && this.date !== 0;
|
|
1242
1269
|
},
|
|
1243
1270
|
has(...keys) {
|
|
1244
|
-
return keys.some((key) =>
|
|
1245
|
-
// @ts-expect-error TS doesn't understand key
|
|
1246
|
-
this[key] != undefined);
|
|
1271
|
+
return keys.some((key) => this[key] != undefined);
|
|
1247
1272
|
},
|
|
1248
1273
|
};
|
|
1249
1274
|
function getMessageFromAnySource(ctx) {
|
package/lib/index.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.InputBox = exports.FormScene = exports.PaginatedBuilder = exports.sanitizeRichHtml = exports.AIStreamAdapter = exports.SmartQueue = exports.RichMessage = exports.Scenes = exports.MemorySessionStore = exports.session = exports.deunionize = exports.Format = exports.Input = exports.Markup = exports.Types = exports.Telegram = exports.TelegramError = exports.Router = exports.Composer = exports.Context = exports.Telegraf = void 0;
|
|
26
|
+
exports.InputBox = exports.FormScene = exports.PaginatedBuilder = exports.sanitizeRichHtml = exports.AIStreamAdapter = exports.SmartQueue = exports.MD = exports.HTML = exports.RichMarkdownBuilder = exports.RichHTMLBuilder = exports.RichMessage = exports.Scenes = exports.MemorySessionStore = exports.session = exports.deunionize = exports.Format = exports.Input = exports.Markup = exports.Types = exports.Telegram = exports.TelegramError = exports.Router = exports.Composer = exports.Context = exports.Telegraf = void 0;
|
|
27
27
|
var telegraf_1 = require("./telegraf");
|
|
28
28
|
Object.defineProperty(exports, "Telegraf", { enumerable: true, get: function () { return telegraf_1.Telegraf; } });
|
|
29
29
|
var context_1 = require("./context");
|
|
@@ -47,6 +47,11 @@ Object.defineProperty(exports, "session", { enumerable: true, get: function () {
|
|
|
47
47
|
Object.defineProperty(exports, "MemorySessionStore", { enumerable: true, get: function () { return session_1.MemorySessionStore; } });
|
|
48
48
|
exports.Scenes = __importStar(require("./scenes"));
|
|
49
49
|
exports.RichMessage = __importStar(require("./core/types/rich-message"));
|
|
50
|
+
var rich_message_1 = require("./core/types/rich-message");
|
|
51
|
+
Object.defineProperty(exports, "RichHTMLBuilder", { enumerable: true, get: function () { return rich_message_1.RichHTMLBuilder; } });
|
|
52
|
+
Object.defineProperty(exports, "RichMarkdownBuilder", { enumerable: true, get: function () { return rich_message_1.RichMarkdownBuilder; } });
|
|
53
|
+
Object.defineProperty(exports, "HTML", { enumerable: true, get: function () { return rich_message_1.RichHTMLBuilder; } });
|
|
54
|
+
Object.defineProperty(exports, "MD", { enumerable: true, get: function () { return rich_message_1.MD; } });
|
|
50
55
|
var smart_queue_1 = require("./core/helpers/smart-queue");
|
|
51
56
|
Object.defineProperty(exports, "SmartQueue", { enumerable: true, get: function () { return smart_queue_1.SmartQueue; } });
|
|
52
57
|
var ai_stream_1 = require("./core/helpers/ai-stream");
|
package/lib/telegram.js
CHANGED
|
@@ -1271,18 +1271,97 @@ class Telegram extends client_1.default {
|
|
|
1271
1271
|
...extra,
|
|
1272
1272
|
});
|
|
1273
1273
|
}
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1274
|
+
async editRichMessage(chatId, messageId, contentOrInline, contentOrExtra, maybeExtra) {
|
|
1275
|
+
let inlineMessageId;
|
|
1276
|
+
let content;
|
|
1277
|
+
let extra = {};
|
|
1278
|
+
if (typeof contentOrInline === 'string' &&
|
|
1279
|
+
(typeof contentOrExtra === 'object' || typeof contentOrExtra === 'string') &&
|
|
1280
|
+
maybeExtra !== undefined) {
|
|
1281
|
+
inlineMessageId = contentOrInline;
|
|
1282
|
+
content = contentOrExtra;
|
|
1283
|
+
extra = maybeExtra || {};
|
|
1284
|
+
}
|
|
1285
|
+
else {
|
|
1286
|
+
content = contentOrInline;
|
|
1287
|
+
extra = contentOrExtra || {};
|
|
1288
|
+
inlineMessageId = extra?.inline_message_id;
|
|
1289
|
+
}
|
|
1290
|
+
let htmlText = '';
|
|
1291
|
+
let replyMarkup = extra?.reply_markup;
|
|
1292
|
+
// 1. Omni-format input extraction
|
|
1293
|
+
if (content && typeof content.build === 'function') {
|
|
1294
|
+
const built = content.build();
|
|
1295
|
+
if (typeof built === 'object' && built !== null) {
|
|
1296
|
+
htmlText = built.html || built.text || built.caption || '';
|
|
1297
|
+
if (built.reply_markup) {
|
|
1298
|
+
replyMarkup = replyMarkup || built.reply_markup;
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
else {
|
|
1302
|
+
htmlText = String(built || '');
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
else if (typeof content === 'object' && content !== null) {
|
|
1306
|
+
htmlText = content.html || content.text || content.caption || content.markdown || '';
|
|
1307
|
+
if (content.reply_markup) {
|
|
1308
|
+
replyMarkup = replyMarkup || content.reply_markup;
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
else if (typeof content === 'string') {
|
|
1312
|
+
htmlText = content;
|
|
1313
|
+
}
|
|
1314
|
+
// Sanitize rich HTML formatting (tables, headers, details, etc.)
|
|
1315
|
+
htmlText = (0, rich_sanitizer_1.sanitizeRichHtml)(htmlText);
|
|
1316
|
+
// 2. Prepare Base Payload
|
|
1317
|
+
const isInline = Boolean(inlineMessageId || extra?.inline_message_id);
|
|
1318
|
+
const basePayload = {
|
|
1319
|
+
parse_mode: 'HTML',
|
|
1284
1320
|
...extra,
|
|
1285
|
-
}
|
|
1321
|
+
};
|
|
1322
|
+
if (isInline) {
|
|
1323
|
+
basePayload.inline_message_id = inlineMessageId || extra.inline_message_id;
|
|
1324
|
+
}
|
|
1325
|
+
else {
|
|
1326
|
+
basePayload.chat_id = chatId;
|
|
1327
|
+
basePayload.message_id = messageId != null ? Number(messageId) : undefined;
|
|
1328
|
+
}
|
|
1329
|
+
if (replyMarkup) {
|
|
1330
|
+
basePayload.reply_markup = replyMarkup;
|
|
1331
|
+
}
|
|
1332
|
+
// 3. Execution with Auto-Detect (Text -> Caption Fallback) and Silent Error Handling
|
|
1333
|
+
try {
|
|
1334
|
+
return await this.callApi('editMessageText', {
|
|
1335
|
+
...basePayload,
|
|
1336
|
+
text: htmlText,
|
|
1337
|
+
});
|
|
1338
|
+
}
|
|
1339
|
+
catch (err) {
|
|
1340
|
+
const desc = String(err?.description || err?.message || '');
|
|
1341
|
+
// Fallback: If target is a Media message (Photo, Video, Audio, Document, Animation)
|
|
1342
|
+
if (desc.includes('no text in the message') ||
|
|
1343
|
+
desc.includes('there is no text in the message to edit') ||
|
|
1344
|
+
desc.includes('message to edit not found')) {
|
|
1345
|
+
try {
|
|
1346
|
+
return await this.callApi('editMessageCaption', {
|
|
1347
|
+
...basePayload,
|
|
1348
|
+
caption: htmlText,
|
|
1349
|
+
});
|
|
1350
|
+
}
|
|
1351
|
+
catch (captionErr) {
|
|
1352
|
+
const capDesc = String(captionErr?.description || captionErr?.message || '');
|
|
1353
|
+
if (capDesc.includes('message is not modified')) {
|
|
1354
|
+
return false;
|
|
1355
|
+
}
|
|
1356
|
+
throw captionErr;
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
// Gracefully handle 'message is not modified'
|
|
1360
|
+
if (desc.includes('message is not modified')) {
|
|
1361
|
+
return false;
|
|
1362
|
+
}
|
|
1363
|
+
throw err;
|
|
1364
|
+
}
|
|
1286
1365
|
}
|
|
1287
1366
|
}
|
|
1288
1367
|
exports.Telegram = Telegram;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "teledzik",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Modern Telegram Bot Framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"telekaf",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"botapi",
|
|
13
13
|
"bot framework",
|
|
14
14
|
"rich messages",
|
|
15
|
-
"bot api 1.0.
|
|
15
|
+
"bot api 1.0.5"
|
|
16
16
|
],
|
|
17
17
|
"homepage": "https://telegraf.js.org",
|
|
18
18
|
"bugs": {
|
package/src/context.ts
CHANGED
|
@@ -1407,17 +1407,61 @@ export class Context<U extends Deunionize<tg.Update> = tg.Update> {
|
|
|
1407
1407
|
}
|
|
1408
1408
|
|
|
1409
1409
|
/**
|
|
1410
|
-
*
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1410
|
+
* Universal Context shortcut to edit message with Omni-Format support (RichHTMLBuilder, Object, or String),
|
|
1411
|
+
* auto-detection of Text vs Media Caption, and silent 'not modified' error handling.
|
|
1412
|
+
*/
|
|
1413
|
+
editRichMessage(
|
|
1414
|
+
content: tg.RichContentInput,
|
|
1415
|
+
extra?: tg.EditRichOptions
|
|
1416
|
+
): Promise<any>
|
|
1417
|
+
editRichMessage(
|
|
1418
|
+
messageId: number | string,
|
|
1419
|
+
content: tg.RichContentInput,
|
|
1420
|
+
extra?: tg.EditRichOptions
|
|
1421
|
+
): Promise<any>
|
|
1422
|
+
async editRichMessage(a: any, b?: any, c?: any): Promise<any> {
|
|
1423
|
+
let targetMsgId: number | string | undefined
|
|
1424
|
+
let content: any
|
|
1425
|
+
let extra: any = {}
|
|
1426
|
+
|
|
1427
|
+
if (
|
|
1428
|
+
typeof a === 'number' ||
|
|
1429
|
+
(typeof a === 'string' && !isNaN(Number(a)) && b !== undefined)
|
|
1430
|
+
) {
|
|
1431
|
+
targetMsgId = a
|
|
1432
|
+
content = b
|
|
1433
|
+
extra = c || {}
|
|
1434
|
+
} else {
|
|
1435
|
+
targetMsgId =
|
|
1436
|
+
this.callbackQuery?.message?.message_id ??
|
|
1437
|
+
(this.message && 'message_id' in this.message ? this.message.message_id : undefined) ??
|
|
1438
|
+
(this.update && 'edited_message' in this.update && this.update.edited_message
|
|
1439
|
+
? this.update.edited_message.message_id
|
|
1440
|
+
: undefined) ??
|
|
1441
|
+
this.msgId
|
|
1442
|
+
content = a
|
|
1443
|
+
extra = b || {}
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
const chatId =
|
|
1447
|
+
this.chat?.id ??
|
|
1448
|
+
(this.callbackQuery?.message && 'chat' in this.callbackQuery.message
|
|
1449
|
+
? this.callbackQuery.message.chat.id
|
|
1450
|
+
: undefined)
|
|
1451
|
+
|
|
1452
|
+
const inlineMsgId = this.inlineMessageId ?? extra?.inline_message_id
|
|
1453
|
+
|
|
1454
|
+
if (!inlineMsgId && (!chatId || targetMsgId == null)) {
|
|
1455
|
+
throw new Error(
|
|
1456
|
+
'[teledzik] Tidak dapat menemukan chatId atau messageId dari konteks pesan saat ini.'
|
|
1457
|
+
)
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
if (inlineMsgId && !extra.inline_message_id) {
|
|
1461
|
+
extra.inline_message_id = inlineMsgId
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
return this.telegram.editRichMessage(chatId, targetMsgId, content, extra)
|
|
1421
1465
|
}
|
|
1422
1466
|
|
|
1423
1467
|
/**
|
|
@@ -1650,18 +1694,14 @@ interface Msg {
|
|
|
1650
1694
|
): this is MaybeMessage<Keyed<tg.Message, Ks[number]>>
|
|
1651
1695
|
}
|
|
1652
1696
|
|
|
1653
|
-
const Msg
|
|
1654
|
-
isAccessible() {
|
|
1697
|
+
const Msg = {
|
|
1698
|
+
isAccessible(this: any) {
|
|
1655
1699
|
return 'date' in this && this.date !== 0
|
|
1656
1700
|
},
|
|
1657
|
-
has(...keys) {
|
|
1658
|
-
return keys.some(
|
|
1659
|
-
(key) =>
|
|
1660
|
-
// @ts-expect-error TS doesn't understand key
|
|
1661
|
-
this[key] != undefined
|
|
1662
|
-
)
|
|
1701
|
+
has(this: any, ...keys: string[]) {
|
|
1702
|
+
return keys.some((key) => this[key] != undefined)
|
|
1663
1703
|
},
|
|
1664
|
-
}
|
|
1704
|
+
} as Msg
|
|
1665
1705
|
|
|
1666
1706
|
export type MaybeMessage<
|
|
1667
1707
|
M extends tg.MaybeInaccessibleMessage = tg.MaybeInaccessibleMessage,
|
|
@@ -32,6 +32,21 @@ export interface InputRichMessageContent {
|
|
|
32
32
|
rich_message: InputRichMessage
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
export interface EditRichOptions {
|
|
36
|
+
reply_markup?: any
|
|
37
|
+
disable_web_page_preview?: boolean
|
|
38
|
+
link_preview_options?: any
|
|
39
|
+
inline_message_id?: string
|
|
40
|
+
[key: string]: any
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type RichContentInput =
|
|
44
|
+
| string
|
|
45
|
+
| { html?: string; text?: string; caption?: string; markdown?: string; reply_markup?: any }
|
|
46
|
+
| { build(): string | InputRichMessage | { html?: string; text?: string; reply_markup?: any } }
|
|
47
|
+
| InputRichMessage
|
|
48
|
+
|
|
49
|
+
|
|
35
50
|
// ---------------------------------------------------------------------------
|
|
36
51
|
// HTML Builder
|
|
37
52
|
// ---------------------------------------------------------------------------
|
package/src/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ export { session, MemorySessionStore, SessionStore } from './session'
|
|
|
16
16
|
|
|
17
17
|
export * as Scenes from './scenes'
|
|
18
18
|
export * as RichMessage from './core/types/rich-message'
|
|
19
|
+
export { RichHTMLBuilder, RichMarkdownBuilder, RichHTMLBuilder as HTML, MD } from './core/types/rich-message'
|
|
19
20
|
|
|
20
21
|
export { SmartQueue } from './core/helpers/smart-queue'
|
|
21
22
|
export { AIStreamAdapter } from './core/helpers/ai-stream'
|
package/src/telegram.ts
CHANGED
|
@@ -1680,23 +1680,127 @@ export class Telegram extends ApiClient {
|
|
|
1680
1680
|
}
|
|
1681
1681
|
|
|
1682
1682
|
/**
|
|
1683
|
-
*
|
|
1683
|
+
* Universal editRichMessage to update text or media captions in-place with Omni-Format support.
|
|
1684
|
+
* Auto-detects Text vs Media Caption and gracefully handles 'message is not modified'.
|
|
1684
1685
|
*/
|
|
1685
|
-
editRichMessage(
|
|
1686
|
+
async editRichMessage(
|
|
1686
1687
|
chatId: number | string | undefined,
|
|
1687
|
-
messageId: number | undefined,
|
|
1688
|
+
messageId: number | string | undefined,
|
|
1689
|
+
content: any,
|
|
1690
|
+
extra?: any
|
|
1691
|
+
): Promise<any>
|
|
1692
|
+
async editRichMessage(
|
|
1693
|
+
chatId: number | string | undefined,
|
|
1694
|
+
messageId: number | string | undefined,
|
|
1688
1695
|
inlineMessageId: string | undefined,
|
|
1689
|
-
|
|
1690
|
-
extra?:
|
|
1691
|
-
)
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1696
|
+
content: any,
|
|
1697
|
+
extra?: any
|
|
1698
|
+
): Promise<any>
|
|
1699
|
+
async editRichMessage(
|
|
1700
|
+
chatId: number | string | undefined,
|
|
1701
|
+
messageId: number | string | undefined,
|
|
1702
|
+
contentOrInline: any,
|
|
1703
|
+
contentOrExtra?: any,
|
|
1704
|
+
maybeExtra?: any
|
|
1705
|
+
): Promise<any> {
|
|
1706
|
+
let inlineMessageId: string | undefined
|
|
1707
|
+
let content: any
|
|
1708
|
+
let extra: any = {}
|
|
1709
|
+
|
|
1710
|
+
if (
|
|
1711
|
+
typeof contentOrInline === 'string' &&
|
|
1712
|
+
(typeof contentOrExtra === 'object' || typeof contentOrExtra === 'string') &&
|
|
1713
|
+
maybeExtra !== undefined
|
|
1714
|
+
) {
|
|
1715
|
+
inlineMessageId = contentOrInline
|
|
1716
|
+
content = contentOrExtra
|
|
1717
|
+
extra = maybeExtra || {}
|
|
1718
|
+
} else {
|
|
1719
|
+
content = contentOrInline
|
|
1720
|
+
extra = contentOrExtra || {}
|
|
1721
|
+
inlineMessageId = extra?.inline_message_id
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
let htmlText = ''
|
|
1725
|
+
let replyMarkup = extra?.reply_markup
|
|
1726
|
+
|
|
1727
|
+
// 1. Omni-format input extraction
|
|
1728
|
+
if (content && typeof content.build === 'function') {
|
|
1729
|
+
const built = content.build()
|
|
1730
|
+
if (typeof built === 'object' && built !== null) {
|
|
1731
|
+
htmlText = built.html || built.text || built.caption || ''
|
|
1732
|
+
if (built.reply_markup) {
|
|
1733
|
+
replyMarkup = replyMarkup || built.reply_markup
|
|
1734
|
+
}
|
|
1735
|
+
} else {
|
|
1736
|
+
htmlText = String(built || '')
|
|
1737
|
+
}
|
|
1738
|
+
} else if (typeof content === 'object' && content !== null) {
|
|
1739
|
+
htmlText = content.html || content.text || content.caption || content.markdown || ''
|
|
1740
|
+
if (content.reply_markup) {
|
|
1741
|
+
replyMarkup = replyMarkup || content.reply_markup
|
|
1742
|
+
}
|
|
1743
|
+
} else if (typeof content === 'string') {
|
|
1744
|
+
htmlText = content
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
// Sanitize rich HTML formatting (tables, headers, details, etc.)
|
|
1748
|
+
htmlText = sanitizeRichHtml(htmlText)
|
|
1749
|
+
|
|
1750
|
+
// 2. Prepare Base Payload
|
|
1751
|
+
const isInline = Boolean(inlineMessageId || extra?.inline_message_id)
|
|
1752
|
+
const basePayload: any = {
|
|
1753
|
+
parse_mode: 'HTML',
|
|
1698
1754
|
...extra,
|
|
1699
|
-
}
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
if (isInline) {
|
|
1758
|
+
basePayload.inline_message_id = inlineMessageId || extra.inline_message_id
|
|
1759
|
+
} else {
|
|
1760
|
+
basePayload.chat_id = chatId
|
|
1761
|
+
basePayload.message_id = messageId != null ? Number(messageId) : undefined
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
if (replyMarkup) {
|
|
1765
|
+
basePayload.reply_markup = replyMarkup
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
// 3. Execution with Auto-Detect (Text -> Caption Fallback) and Silent Error Handling
|
|
1769
|
+
try {
|
|
1770
|
+
return await this.callApi('editMessageText' as never, {
|
|
1771
|
+
...basePayload,
|
|
1772
|
+
text: htmlText,
|
|
1773
|
+
} as never)
|
|
1774
|
+
} catch (err: any) {
|
|
1775
|
+
const desc = String(err?.description || err?.message || '')
|
|
1776
|
+
|
|
1777
|
+
// Fallback: If target is a Media message (Photo, Video, Audio, Document, Animation)
|
|
1778
|
+
if (
|
|
1779
|
+
desc.includes('no text in the message') ||
|
|
1780
|
+
desc.includes('there is no text in the message to edit') ||
|
|
1781
|
+
desc.includes('message to edit not found')
|
|
1782
|
+
) {
|
|
1783
|
+
try {
|
|
1784
|
+
return await this.callApi('editMessageCaption' as never, {
|
|
1785
|
+
...basePayload,
|
|
1786
|
+
caption: htmlText,
|
|
1787
|
+
} as never)
|
|
1788
|
+
} catch (captionErr: any) {
|
|
1789
|
+
const capDesc = String(captionErr?.description || captionErr?.message || '')
|
|
1790
|
+
if (capDesc.includes('message is not modified')) {
|
|
1791
|
+
return false
|
|
1792
|
+
}
|
|
1793
|
+
throw captionErr
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
// Gracefully handle 'message is not modified'
|
|
1798
|
+
if (desc.includes('message is not modified')) {
|
|
1799
|
+
return false
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
throw err
|
|
1803
|
+
}
|
|
1700
1804
|
}
|
|
1701
1805
|
}
|
|
1702
1806
|
|
package/typings/context.d.ts
CHANGED
|
@@ -591,9 +591,11 @@ export declare class Context<U extends Deunionize<tg.Update> = tg.Update> {
|
|
|
591
591
|
*/
|
|
592
592
|
sendRichMessageDraft(draftId: number, richMessage: tg.InputRichMessage, extra?: tt.ExtraSendRichMessageDraft): Promise<never>;
|
|
593
593
|
/**
|
|
594
|
-
*
|
|
594
|
+
* Universal Context shortcut to edit message with Omni-Format support (RichHTMLBuilder, Object, or String),
|
|
595
|
+
* auto-detection of Text vs Media Caption, and silent 'not modified' error handling.
|
|
595
596
|
*/
|
|
596
|
-
editRichMessage(
|
|
597
|
+
editRichMessage(content: tg.RichContentInput, extra?: tg.EditRichOptions): Promise<any>;
|
|
598
|
+
editRichMessage(messageId: number | string, content: tg.RichContentInput, extra?: tg.EditRichOptions): Promise<any>;
|
|
597
599
|
/**
|
|
598
600
|
* Enqueue a Telegram API call for the current chat to prevent 429 Too Many Requests errors.
|
|
599
601
|
*/
|
|
@@ -24,6 +24,26 @@ export interface InputRichMessage {
|
|
|
24
24
|
export interface InputRichMessageContent {
|
|
25
25
|
rich_message: InputRichMessage;
|
|
26
26
|
}
|
|
27
|
+
export interface EditRichOptions {
|
|
28
|
+
reply_markup?: any;
|
|
29
|
+
disable_web_page_preview?: boolean;
|
|
30
|
+
link_preview_options?: any;
|
|
31
|
+
inline_message_id?: string;
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
}
|
|
34
|
+
export type RichContentInput = string | {
|
|
35
|
+
html?: string;
|
|
36
|
+
text?: string;
|
|
37
|
+
caption?: string;
|
|
38
|
+
markdown?: string;
|
|
39
|
+
reply_markup?: any;
|
|
40
|
+
} | {
|
|
41
|
+
build(): string | InputRichMessage | {
|
|
42
|
+
html?: string;
|
|
43
|
+
text?: string;
|
|
44
|
+
reply_markup?: any;
|
|
45
|
+
};
|
|
46
|
+
} | InputRichMessage;
|
|
27
47
|
/**
|
|
28
48
|
* Fluent builder for Rich Messages using HTML format.
|
|
29
49
|
*
|
package/typings/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export { deunionize } from './core/helpers/deunionize';
|
|
|
13
13
|
export { session, MemorySessionStore, SessionStore } from './session';
|
|
14
14
|
export * as Scenes from './scenes';
|
|
15
15
|
export * as RichMessage from './core/types/rich-message';
|
|
16
|
+
export { RichHTMLBuilder, RichMarkdownBuilder, RichHTMLBuilder as HTML, MD } from './core/types/rich-message';
|
|
16
17
|
export { SmartQueue } from './core/helpers/smart-queue';
|
|
17
18
|
export { AIStreamAdapter } from './core/helpers/ai-stream';
|
|
18
19
|
export { sanitizeRichHtml } from './core/helpers/rich-sanitizer';
|
package/typings/telegram.d.ts
CHANGED
|
@@ -690,8 +690,10 @@ export declare class Telegram extends ApiClient {
|
|
|
690
690
|
*/
|
|
691
691
|
sendRichMessageDraft(chatId: number, draftId: number, richMessage: tg.InputRichMessage, extra?: tt.ExtraSendRichMessageDraft): Promise<never>;
|
|
692
692
|
/**
|
|
693
|
-
*
|
|
693
|
+
* Universal editRichMessage to update text or media captions in-place with Omni-Format support.
|
|
694
|
+
* Auto-detects Text vs Media Caption and gracefully handles 'message is not modified'.
|
|
694
695
|
*/
|
|
695
|
-
editRichMessage(chatId: number | string | undefined, messageId: number |
|
|
696
|
+
editRichMessage(chatId: number | string | undefined, messageId: number | string | undefined, content: any, extra?: any): Promise<any>;
|
|
697
|
+
editRichMessage(chatId: number | string | undefined, messageId: number | string | undefined, inlineMessageId: string | undefined, content: any, extra?: any): Promise<any>;
|
|
696
698
|
}
|
|
697
699
|
export default Telegram;
|