Skip to content

Commit 0ac490f

Browse files
author
AffanTheBest
committed
Add some more features
2 parents c78be24 + 8228ba9 commit 0ac490f

File tree

2 files changed

+84
-70
lines changed

2 files changed

+84
-70
lines changed

bot.js

Lines changed: 65 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { Bot } = require("grammy");
33
const { BOT_TOKEN, ADMINS, LIMIT } = require("./config");
44
const { isUrl, getRandomId, getProductDetails } = require("./utils");
55
const { manageProducts, manageUsers } = require("./db");
6+
const unshort = require("./unshort");
67

78
const bot = new Bot(BOT_TOKEN); // Initialize bot
89

@@ -20,20 +21,63 @@ const reply_markup = {
2021
],
2122
};
2223

24+
const processUrl = async (msg, ctx) => {
25+
try {
26+
const url = await unshort(msg);
27+
const productUrl = "http" + url.split("http")[1].split(" ")[0].replace("dl.", "www.")
28+
if (isUrl(productUrl)) {
29+
const merchant = productUrl.replace("www.", "").split("//")[1].split(".")[0];
30+
if (merchant.match(/amazon|flipkart/gi)) {
31+
const noOfProducts = (
32+
await manageProducts({ userId: ctx.from.id }, "read")
33+
)?.result?.length;
34+
if (noOfProducts < LIMIT) {
35+
const sentMsg = await ctx.reply(`Tracking ${merchant} product...`, { reply_to_message_id: ctx.message.message_id });
36+
const details = await getProductDetails(productUrl, merchant);
37+
if (details.ok) {
38+
try {
39+
const tracking_id = getRandomId();
40+
await manageProducts(
41+
{ tracking_id, userId: ctx.from.id, merchant, title: details.title, link: details.link, initPrice: details.price, price: details.price, },
42+
"update"
43+
);
44+
await ctx.api.editMessageText(
45+
ctx.chat.id, sentMsg.message_id,
46+
`<a href="${details.image}"> </a>\nTracking <b>${details.title}</b>\n\nCurrent Price: <b>${details.price}</b>\nLink: <a href="${details.link}">${merchant}</a>\n\nTo stop tracking send /stop_${tracking_id}`,
47+
{ parse_mode: "HTML", reply_markup }
48+
);
49+
} catch (e) { }
50+
} else {
51+
await ctx.api.editMessageText(
52+
ctx.chat.id, sentMsg.message_id,
53+
`Sorry, I couldn't track this product. Make sure you've sent correct product link.`,
54+
{ parse_mode: "Markdown", reply_markup }
55+
);
56+
}
57+
} else {
58+
ctx.reply( "I'm sorry, but you can't add more products as you've already reached the maximum limit.\n\nPlease delete atleast one product. And try again.\n\nTo get list send /list",
59+
{ reply_to_message_id: ctx.message.message_id } );
60+
}
61+
} else {
62+
ctx.reply( `Sorry, I can't track this product. Cuz the link you sent is not a amazon or flipkart product link.` );
63+
}
64+
} else {
65+
ctx.reply( `Sorry ${ctx.message.chat.first_name}, I can't track this product. Make sure you've sent correct product link.` );
66+
}
67+
} catch(e){
68+
console.error(e)
69+
}
70+
}
71+
72+
2373
bot.command("start", (ctx) => {
2474
// start command
2575
try {
2676
ctx.reply(
2777
`Hello ${ctx.message.chat.first_name}, I can track price for Amazon & Flipkart products (Soon more).\n\nCheck /help to get started.\n`,
28-
{
29-
reply_to_message_id: ctx.message.message_id,
30-
reply_markup,
31-
}
32-
);
33-
manageUsers(
34-
{ id: ctx.message.from.id, name: ctx.message.from.first_name },
35-
"update"
78+
{ reply_to_message_id: ctx.message.message_id, reply_markup, }
3679
);
80+
manageUsers( { id: ctx.message.from.id, name: ctx.message.from.first_name }, "update" );
3781
} catch (e) {
3882
console.log("Error", e);
3983
}
@@ -52,68 +96,15 @@ bot.command("help", (ctx) => {
5296
} catch (e) { }
5397
});
5498

99+
55100
bot.command("track", async (ctx) => {
56-
const productUrl = ctx.message.text.replace("/track ", "");
57-
if (isUrl(productUrl)) {
58-
const merchant = productUrl
59-
.replace("www.", "")
60-
.split("//")[1]
61-
.split(".")[0];
62-
if (merchant.match(/amazon|flipkart/gi)) {
63-
const noOfProducts = (
64-
await manageProducts({ userId: ctx.from.id }, "read")
65-
)?.result?.length;
66-
if (noOfProducts < LIMIT) {
67-
const sentMsg = await ctx.reply(`Tracking ${merchant} product...`, {
68-
reply_to_message_id: ctx.message.message_id,
69-
});
70-
const details = await getProductDetails(productUrl, merchant);
71-
if (details.ok) {
72-
try {
73-
const tracking_id = getRandomId();
74-
await manageProducts(
75-
{
76-
tracking_id,
77-
userId: ctx.from.id,
78-
merchant,
79-
title: details.title,
80-
link: details.link,
81-
initPrice: details.price,
82-
price: details.price,
83-
},
84-
"update"
85-
);
86-
await ctx.api.editMessageText(
87-
ctx.chat.id,
88-
sentMsg.message_id,
89-
`<a href="${details.image}"> </a>\nTracking <b>${details.title}</b>\n\nCurrent Price: <b>${details.price}</b>\nLink: <a href="${details.link}">${merchant}</a>\n\nTo stop tracking send /stop_${tracking_id}`,
90-
{ parse_mode: "HTML", reply_markup }
91-
);
92-
} catch (e) { }
93-
} else {
94-
await ctx.api.editMessageText(
95-
ctx.chat.id,
96-
sentMsg.message_id,
97-
`Sorry, I couldn't track this product. Make sure you've sent correct product link.`,
98-
{ parse_mode: "Markdown", reply_markup }
99-
);
100-
}
101-
} else {
102-
ctx.reply(
103-
"I'm sorry, but you can't add more products as you've already reached the maximum limit.\n\nPlease delete atleast one product. And try again.\n\nTo get list send /list",
104-
{ reply_to_message_id: ctx.message.message_id }
105-
);
106-
}
107-
} else {
108-
ctx.reply(
109-
`Sorry, I can't track this product. Cuz the link you sent is not a amazon or flipkart product link.`
110-
);
111-
}
112-
} else {
113-
ctx.reply(
114-
`Sorry ${ctx.message.chat.first_name}, I can't track this product. Make sure you've sent correct product link.`
115-
);
116-
}
101+
const message = ctx.message.text.replace("/track ", "");
102+
processUrl(message, ctx);
103+
});
104+
105+
bot.on('::url', async ctx => {
106+
const message = ctx.message.text;
107+
processUrl(message, ctx);
117108
});
118109

119110
bot.command("list", async (ctx) => {
@@ -232,7 +223,11 @@ const track = async () => {
232223
await manageProducts({ tracking_id: product.tracking_id, userId: product.userId, merchant: product.merchant, title: details.title, link: product.link, initPrice: product.price, price: details.price, }, "update");
233224
bot.api.sendMessage(
234225
product.userId,
235-
`<a href="${details.image}"> </a><b>Price has been ${details.price > product.price ? "increased" : "decreased"} by ${Math.abs(product.price - details.price)}</b>. \n\n<b>${details.title}</b>\n\nCurrent Price: <b>${details.price}</b>\nLink: <a href="${details.link}">${product.merchant}</a>\n\nTo stop tracking send /stop_${product.tracking_id}`,
226+
`<a href="${details.image}"> </a><b>Price has been ${details.price > product.price ? "increased" : "decreased"
227+
} by ${Math.abs(product.price - details.price)}</b>. \n\n<b>${details.title
228+
}</b>\n\nCurrent Price: <b>${details.price}</b>\nLink: <a href="${details.link
229+
}">${product.merchant}</a>\n\nTo stop tracking send /stop_${product.tracking_id
230+
}`,
236231
{
237232
parse_mode: "HTML",
238233
reply_markup: {

unshort.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const axios = require('axios');
2+
const unshort = async (url) => {
3+
const extractUrl = req => req?.request?.res?.responseUrl ||
4+
req?.request?._redirectable?._currentUrl ||
5+
req?.request?._currentUrl ||
6+
req?.request?._options?.href ||
7+
'https://' + req?.request?.host + req?.request?.path;
8+
try {
9+
const req = await axios.get(url);
10+
const result = extractUrl(req);
11+
var longUrl = result ? result : url;
12+
} catch (err) {
13+
const result = extractUrl(err);
14+
var longUrl = result ? result : url;
15+
}
16+
return longUrl;
17+
}
18+
19+
module.exports = unshort;

0 commit comments

Comments
 (0)