File size: 3,385 Bytes
00eae7f
3655d67
00eae7f
255ac8a
 
 
505fc77
255ac8a
 
 
 
 
 
 
 
 
 
 
c4ec111
 
 
 
 
 
255ac8a
 
 
00eae7f
255ac8a
00eae7f
 
 
255ac8a
 
 
00eae7f
 
255ac8a
 
3655d67
00eae7f
 
3655d67
 
 
 
255ac8a
3655d67
 
00eae7f
 
 
255ac8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00eae7f
 
 
3655d67
 
00eae7f
 
 
 
 
 
255ac8a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const { Api } = require("telegram");
const { _parseMessageText } = require("telegram/client/messageParse");

function buildReplyMarkup(client, buttons) {
    if (buttons === null || buttons === undefined) return new Api.ReplyInlineMarkup({ rows: [] });
    if (Array.isArray(buttons) && buttons.length > 0) return client.buildReplyMarkup(buttons);
    if (buttons && typeof buttons === 'object') return buttons;
    return undefined;
}

async function editInlineMsg(client, inlineMsgId, text, buttons) {
    const [parsedText, entities] = await _parseMessageText(client, text, 'html');
    const params = {
        id: inlineMsgId,
        message: parsedText,
        entities,
        noWebpage: true,
    };
    // For inline-sent messages, omitting replyMarkup on final-result edits is
    // more reliable than sending an empty inline keyboard.
    if (!(buttons === null || buttons === undefined)) {
        const replyMarkup = buildReplyMarkup(client, buttons);
        if (replyMarkup) params.replyMarkup = replyMarkup;
    }
    await client.invoke(new Api.messages.EditInlineBotMessage(params));
}

/**
 * Edit normal chat message or inline-sent message with HTML parse mode.
 */
async function editMsg(client, peer, msgId, text, buttons) {
    try {
        if ((!peer || peer === null) && msgId && typeof msgId === 'object') {
            await editInlineMsg(client, msgId, text, buttons);
            return;
        }

        const [parsedText, entities] = await _parseMessageText(client, text, 'html');
        const inputPeer = await client.getInputEntity(peer);
        const params = {
            peer: inputPeer,
            id: msgId,
            message: parsedText,
            entities: entities,
            noWebpage: true,
        };
        const replyMarkup = buildReplyMarkup(client, buttons);
        if (replyMarkup) params.replyMarkup = replyMarkup;
        await client.invoke(new Api.messages.EditMessage(params));
    } catch (e) {
        if (e.message && e.message.includes('REPLY_MARKUP_INVALID')) {
            try {
                if ((!peer || peer === null) && msgId && typeof msgId === 'object') {
                    const [parsedText, entities] = await _parseMessageText(client, text, 'html');
                    await client.invoke(new Api.messages.EditInlineBotMessage({
                        id: msgId,
                        message: parsedText,
                        entities,
                        noWebpage: true,
                    }));
                } else {
                    const [parsedText, entities] = await _parseMessageText(client, text, 'html');
                    const inputPeer = await client.getInputEntity(peer);
                    await client.invoke(new Api.messages.EditMessage({
                        peer: inputPeer,
                        id: msgId,
                        message: parsedText,
                        entities: entities,
                        noWebpage: true,
                    }));
                }
            } catch (e2) {
                console.error('[editMsg] fallback failed:', e2.message);
            }
        } else if (e.message && e.message.includes('MESSAGE_NOT_MODIFIED')) {
            // same content, ignore
        } else {
            console.error('[editMsg] error:', e.message);
        }
    }
}

module.exports = { editMsg, editInlineMsg };