Fórum Estou desenvolvendo um bot pro Telegram para armazenar e exibir cartas virtuais mas estou tendo problemas para configurar a paginação corretamente. #622283
21/05/2024
0
1 2 3 4 5 6 7 8 9 10 11 12 13 | const { Telegraf } = require( 'telegraf' ); const fs = require( 'fs' ); const axios = require( 'axios' ); const { Octokit } = require( "@octokit/rest" ); //Campos vazios por motivos óbvios mas devidamente preenchidos no arquivo. js const BOT_TOKEN = '' ; const GITHUB_TOKEN = '' ; const GITHUB_REPO_OWNER = '' ; const GITHUB_REPO_NAME = '' ; const bot = new Telegraf(BOT_TOKEN); const octokit = new Octokit({ auth: GITHUB_TOKEN }); |
Aqui está um dos comandos que usa o esquema de paginação que eu escrevi:
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | bot.command( 'clc' , async (ctx) => { const input = ctx.message.text.split( ' ' ); const userInputCategory = input.slice(1).join( ' ' ); if (!userInputCategory) { ctx.reply( 'Por favor, forneça o nome ou ID de uma coleção.' , { reply_to_message_id: ctx.message.message_id }); return ; } const categoryDetails = getCategoryByInput(userInputCategory); if (!categoryDetails) { ctx.reply( 'Categoria não encontrada.' , { reply_to_message_id: ctx.message.message_id }); return ; } const category = categoryDetails.name; const categoryId = categoryDetails.id; const categoryCards = Object.values(cards) .filter((card) => card.category === category) .sort((a, b) => { const rarityOrder = { 'Ouro' : 3, 'Prata' : 2, 'Bronze' : 1 }; const rarityComparison = rarityOrder[b.rarity] - rarityOrder[a.rarity]; if (rarityComparison !== 0) { return rarityComparison; } return a.name.localeCompare(b.name); }); const pageSize = 20; const totalPages = Math.ceil(categoryCards.length / pageSize); let clcPage = 1; const sendPage = async (ctx, edit = false ) => { const startIndex = (clcPage - 1) * pageSize; const endIndex = startIndex + pageSize; const currentCards = categoryCards.slice(startIndex, endIndex); let replyMessage = `${subcategoryEmojiMap[currentCards[0].subcategory]} \`$\`. *$*\n🎲 \`${categoryCards.length}\` cartas no total.\n\n`; currentCards.forEach((card) => { const maxIdLength = 2; const formattedId = formatCardId(card.id, maxIdLength); const rarityText = replaceRarityWithEmoji(card.rarity); replyMessage += `$ \`$\`. *${card.name}*\n`; }); replyMessage += `\n📄 Página \`$\` de \`$\``; const buttons = []; if (clcPage > 1) buttons.push( '⏪' ); if (clcPage > 1) buttons.push( '⬅️' ); if (clcPage < totalPages) buttons.push( '➡️' ); if (clcPage < totalPages) buttons.push( '⏩' ); const categoryInfo = categories[categoryId] || {}; const imageUrl = categoryInfo.imageUrl || '' ; if (edit) { await ctx.editMessageMedia({ type: 'photo' , media: imageUrl, caption: replyMessage, parse_mode: 'Markdown' }, { reply_markup: { inline_keyboard: [buttons.map(button => ({ text: button, callback_data: `clc-$` }))] } }). catch ((err) => console.log( "Edit message error:" , err)); } else { await ctx.replyWithPhoto({ url: imageUrl }, { caption: replyMessage, parse_mode: 'Markdown' , reply_to_message_id: ctx.message.message_id, reply_markup: { inline_keyboard: [buttons.map(button => ({ text: button, callback_data: `clc-$` }))] } }); } }; sendPage(ctx); bot.action([ 'clc-⏪' , 'clc-⬅️' , 'clc-➡️' , 'clc-⏩' ], async (ctx) => { const action = ctx.callbackQuery.data; switch (action) { case 'clc-⏪' : clcPage = 1; break ; case 'clc-⬅️' : if (clcPage > 1) clcPage--; break ; case 'clc-➡️' : if (clcPage < totalPages) clcPage++; break ; case 'clc-⏩' : clcPage = totalPages; break ; } await sendPage(ctx, true ); ctx.answerCbQuery(); }); }); |
O problema é que se um usuário enviar o comando no Telegram e depois enviar o mesmo comando em seguida e passar de página, a mensagem do comando anterior é copiada para a mensagem da nova página.

Librious
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)