diff --git a/packages/examples/src/example-bot.tsx b/packages/examples/src/example-bot.tsx index dba5f85..8f8f8b8 100644 --- a/packages/examples/src/example-bot.tsx +++ b/packages/examples/src/example-bot.tsx @@ -65,6 +65,55 @@ const TodoApp = () => { ); }; +const ButtonLimitTestApp = () => { + return ( + <> + 🚫 Button Limit Test +
+
+ This command creates too many buttons to test the button limit error. +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ); +}; + const HelpApp = () => { const [section, setSection] = useState<'main' | 'formatting' | 'features'>('main'); @@ -142,6 +191,8 @@ const HelpApp = () => {
/todo - Todo list manager
+ /buttontest - Test button limit error +
/help - This help message

@@ -175,6 +226,7 @@ async function main() { adapter.onCommand('help', () => ); adapter.onCommand('counter', () => ); adapter.onCommand('todo', () => ); + adapter.onCommand('buttontest', () => ); // Start the bot await adapter.start(config.botToken); diff --git a/packages/mtcute-adapter/src/mtcute-adapter.ts b/packages/mtcute-adapter/src/mtcute-adapter.ts index 7e56de1..1e43567 100644 --- a/packages/mtcute-adapter/src/mtcute-adapter.ts +++ b/packages/mtcute-adapter/src/mtcute-adapter.ts @@ -225,11 +225,19 @@ export class MtcuteAdapter { if (rows.length === 0) return undefined; const keyboard: tl.TypeKeyboardButton[][] = rows.map(row => - row.children.map(button => ({ + { + const rowButtons = row.children.map(button => ({ _: 'keyboardButtonCallback', text: button.text, data: Buffer.from(`${containerId}:${button.id}`) }) as tl.TypeKeyboardButton) + + if (rowButtons.length >8) { + throw new Error('Row has more than 8 buttons'); + }; + + return rowButtons; + } ); return { _: 'replyInlineMarkup', rows: keyboard.map(row => ({ _: 'keyboardButtonRow', buttons: row })) }; @@ -248,23 +256,28 @@ export class MtcuteAdapter { // Set up re-render callback container.container.onRenderContainer = async (root) => { - const textWithEntities = this.rootNodeToTextWithEntities(root); - const replyMarkup = this.rootNodeToInlineKeyboard(root, containerId); - - if (messageId === null) { - // First render: send a new message - const sentMessage = await this.client.sendText(chatId, textWithEntities, { - replyMarkup - }); - messageId = sentMessage.id; - } else { - // Subsequent renders: edit the existing message - await this.client.editMessage({ - chatId, - message: messageId, - text: textWithEntities, - replyMarkup - }); + try { + const textWithEntities = this.rootNodeToTextWithEntities(root); + const replyMarkup = this.rootNodeToInlineKeyboard(root, containerId); + + if (messageId === null) { + // First render: send a new message + const sentMessage = await this.client.sendText(chatId, textWithEntities, { + replyMarkup + }); + messageId = sentMessage.id; + } else { + // Subsequent renders: edit the existing message + await this.client.editMessage({ + chatId, + message: messageId, + text: textWithEntities, + replyMarkup + }); + } + } catch (err) { + console.error('Error sending message:', err); + await this.client.sendText(chatId, 'Error sending message, please try again later.'); } };