i tried to make a scraping telegram bot using node and telegraf
i did try replying message with the function but it replied the [promise object ] even though I used return in the function i tried replying inside if function and the bot didn’t replied at all
all the things i have tried are temporarily removed with comments so you can see them
here is the code :
import puppeteer from "puppeteer";
import { Telegraf } from "telegraf";
let input = "zesht"
// let textReply = "martike"
const bot = new Telegraf("bot token here")
const getData = async function() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`https://fa.wikipedia.org/w/index.php?search=${input}&title=%D9%88%DB%8C%DA%98%D9%87%3A%D8%AC%D8%B3%D8%AA%D8%AC%D9%88&ns0=1` , { timeout : 0});
const searchResult = await page.$(".mw-search-result-heading > a[data-serp-pos='0']" , {timeout : 0})
// const maghaleLink = await searchResult.evaluate(el => el.href)
if(searchResult == null){
const maghale = await page.$('p');
const textReply = await maghale.evaluate( el => el.textContent);
// console.log(matn)
// return matn
// return await maghale.evaluate(el => el.textContent)
bot.on("message" , async ctx=> { input = ctx.message.text , await ctx.reply(textReply)});
}
else{
const maghaleLink = await searchResult.evaluate(el => el.href);
await page.goto(maghaleLink);
const maghale1 = await page.$('p');
const textReply = await maghale1.evaluate( el => el.textContent);
// await maghale1.evaluate(el => el.textContent)
// console.log(matn1)
bot.on("message" , async ctx => { input = ctx.message.text ,await ctx.reply(textReply) }) ;
}
browser.close()
}
// const bot = new Telegraf("bot token here")
bot.start(ctx => ctx.reply("salam mashti"))
// bot.on("message" , async ctx => { await getData()} )
bot.on("message" , getData())
// bot.on("message" , async ctx=> { input = ctx.message.text; getData() ; await ctx.reply(textReply) })
bot.launch()
expected
replying with a text scraped from wikipedia
what happened
bot replies nothing or replies a text that is [promise object] or gives an error saying : unhandled error while processing message
aghasibil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.