-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
74 lines (67 loc) · 2.61 KB
/
index.js
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
const { Telegraf } = require('telegraf')
const request = require('request-promise');
const cheerio = require('cheerio');
const dotenv = require('dotenv');
dotenv.config();
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.start((ctx) => {
ctx.reply('Use me inLine to search GSMArena inside <a href="https://t.me/c/1244642299/139660">@Telegram</a>\n\nDev: <a href="https://t.me/JinsoRaj">Jinso Raj</a>\n⭐️ <a href="https://github.com/JinsoRaj/GSMArenaBot">Source Code</a>',
{
parse_mode: "HTML",
disable_web_page_preview: true,
reply_to_message_id: ctx.message.message_id,
reply_markup:{
inline_keyboard: [
[
{text: '👇 Search 👇', switch_inline_query_current_chat:'xperia'}
]
]
}
});
})
bot.on('inline_query',async ctx => {
let query = ctx.inlineQuery.query;
// full results array
let enq = encodeURIComponent(query);
const url = "https://www.gsmarena.com/res.php3?sSearch=" + enq;
const scraperesults = [];
try{
const html = await request.get(url);
const $ = await cheerio.load(html);
$("#review-body > div > ul > li >a").each((index, element) => {
const mname = $(element).text();
const npurl = "https://www.gsmarena.com/" + $(element).attr("href");
const imgurl = $("#review-body > div > ul > li:nth-child(" + ++index + ") > a > img").attr("src");
const scraperesult = { mname, npurl, imgurl};
scraperesults.push(scraperesult);
});
//console.log(scraperesults);
}catch (err) {
bot.telegram.sendMessage(process.env.OWNER_ID, `Ooops, encountered an error for ${ctx.updateType} :`+ err)
}
let results = scraperesults.map((item, index) => {
return {
type: 'article',
id: String(index),
title: item.mname,
input_message_content: {
message_text: `Device: [${item.mname}](${item.npurl})`,
parse_mode: "Markdown"
},
reply_markup:{
inline_keyboard: [
[
{text: 'Search Again 🔎', switch_inline_query_current_chat: query}
]
]
},
description: "Instant View ",
thumb_url: item.imgurl
}
})
ctx.answerInlineQuery(results);
})
bot.catch((err, ctx) => {
bot.telegram.sendMessage(process.env.OWNER_ID, `Ooops, encountered an error for ${ctx.updateType} :`+ err)
})
bot.launch();