yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.35k stars 1.52k forks source link

[Bug] Callback Query replies previous message #1057

Closed caiocezartg closed 1 year ago

caiocezartg commented 1 year ago

Hello everyone I'm creating a bot with this library that scrappes a manga website and return details about the manga with their chapters (name, date and number)

So, first step is sending '/manga' with name of manga you wanna know about, ex: /manga chainsaw man, it returns details about it and a inline keybutton to list their chapters

The problem is: if i search a new manga and press this button of chapters, the code return the array of actual manga and previous manga, example: if i search chainsaw man first and after it I search boku no hero manga, it returns me chainsaw man and boku no hero chapters

I tried reseting array always the scrapping function is called, but is not working... what can I do? pls

the code:

main.js export async function mangaCommand(message, name) { let chatId = message.chat.id; let messageId = message.message_id; const manga = await mangaController(name);

if (manga.error) { return bot.sendMessage(chatId, manga.error); }

const options = { parse_mode: "HTML", reply_markup: JSON.stringify({ inline_keyboard: [ [{ text: "đź“– Leia os capĂ­tulos", callback_data: "first_page" }], ], }), };

bot.sendPhoto(chatId, manga.poster);

bot.sendMessage( chatId, <b>Nome:</b> ${manga.name} \n + <b>Autor:</b> ${manga.author} \n + <b>Artista:</b> ${manga.artist} \n + <b>Categorias:</b> ${manga.categories} \n + <b>Descrição:</b> ${manga.description} \n + <b>Número de Capítulos:</b> ${manga.chapters_count} \n + <b>Nota:</b> ${transformScore(manga.score)} \n + <b>Link do mangá:</b> ${manga.link} \n, options );

bot.on("callback_query", async (callbackQuery) => { const action = callbackQuery.data; let setupInlineKeyboard = {};

if (action === "first_page") {
  chaptersPage = 1;
}

if (action === "next_page") {
  chaptersPage++;
}

if (action === "previous_page") {
  chaptersPage === 1 ? chaptersPage : chaptersPage--;
}

setupInlineKeyboard = await inlineKeyboardChapters(
  manga.id_serie,
  messageId,
  chaptersPage
);

bot.sendMessage(chatId, "Selecione um capitulo", setupInlineKeyboard);

}); }

let chaptersPage = 1;

async function inlineKeyboardChapters(id_serie, message_id, page) { let idReleaseManga = id_serie; let arrayChaptersManga = [];

arrayChaptersManga = await getChaptersController(idReleaseManga, page);

let buttonsChaptersManga = arrayChaptersManga.map((chapter) => { return [ { text: #${chapter.number} - ${chapter.chapter_name}, callback_data: "1", }, ]; });

buttonsChaptersManga.push([ { text: "Página anterior", callback_data: "previous_page" }, { text: "Próxima página", callback_data: "next_page" }, ]);

let optionsChapters = { reply_markup: JSON.stringify({ inline_keyboard: buttonsChaptersManga, }), };

return optionsChapters; }

getChaptersController.js

export async function getChaptersController(manga_id, page) { // let chapters = [];

try { const response = await api.get( https://mangalivre.net/series/chapters_list.json?page=${page}&id_serie=${manga_id} );

let chaptersResponse = response.data.chapters;

if (chaptersResponse) {
  chaptersResponse.map((chapter) => {
    return {
      chapter_name: chapter.chapter_name,
      number: chapter.number,
      date: chapter.date,
      id_release:
        chapter.releases[Object.keys(chapter.releases)[0]].id_release,
    };
  });
}

return chaptersResponse;

} catch (error) { console.log(error); } }

caiocezartg commented 1 year ago

I fixed with removeListener(), thx.