0

If you just parse the page and output the answer to the console, then everything works correctly and the desired answer comes.

enter image description here

if this answer is added to the telegram bot's response using the await ctx.reply telegraph, an error occurs.

Unhandled error while processing..

node_modules\telegraf\lib\core\network\client.js:293
            throw new error_1.default(data, { method, payload });
response: {
    ok: false,
    error_code: 400,
    description: 'Bad Request: message text is empty'
  },
  on: {
    method: 'sendMessage',
    payload: { chat_id: 27190544, message_thread_id: undefined }
  }
}

Node.js v18.12.1
[nodemon] app crashed - waiting for file changes before starting...

how do I add a parser response to a bot? help me please

const { Telegraf, Markup } = require('telegraf');
const BOT_TOKEN = "*****************************************";
const bot = new Telegraf(BOT_TOKEN);
const axios = require("axios");
const cheerio = require("cheerio");

async function getString() {
    await axios.get("https://www.federalreserve.gov/releases/h10/current/").then((response) => {
      const $ = cheerio.load(response.data);
      const table = $("table");

      for (let i = 0; i < table.length; i++) {
        let tr = $(table[i]).find("tr")[4];
        console.log(tr.tostring());
        return $(tr).text();
      }
    })
    .catch((err) => console.log("Fetch error " + err));
  }
  
bot.start((ctx) => {
    ctx.reply('restart!')
});

bot.command('test', async (ctx) => {
    ctx.reply("Click it", Markup.inlineKeyboard(
        [
            [Markup.button.callback('PEACE', 'PEACE')]
        ]
    ))
});

bot.action('PEACE', async (ctx) => {
    await ctx.answerCbQuery()
    await ctx.reply(getString())
});


bot.launch();
DreamBold
  • 2,727
  • 1
  • 9
  • 24
  • 1
    `return await axios.get(` and `await ctx.reply(await getString())`? – ggorlen Dec 20 '22 at 16:37
  • Does this answer your question? [Returning data from Axios API](https://stackoverflow.com/questions/48980380/returning-data-from-axios-api) – ggorlen Dec 20 '22 at 20:38

0 Answers0