Spaces:
Sleeping
Sleeping
| 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 }; | |