Skip to content

Commit e181bfe

Browse files
author
AffanTheBest
committed
Add error handling
1 parent dba2761 commit e181bfe

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

bot.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ bot.command('start', (ctx) => { // start command
2929
});
3030

3131
bot.command('help', (ctx) => { // help command
32-
ctx.reply(`/start - Start the bot\n/help - get this message.\n/track {Product Link} - Add product to tracking list.\n/stop_{Tracking ID} - Stop tracking.\n/list - Get list of products that are being tracked.\n\nFor more help join @@assupportchat.`,
33-
{
34-
reply_to_message_id: ctx.message.message_id,
35-
reply_markup
36-
});
32+
try {
33+
ctx.reply(`/start - Start the bot\n/help - get this message.\n/track {Product Link} - Add product to tracking list.\n/stop_{Tracking ID} - Stop tracking.\n/list - Get list of products that are being tracked.\n\nFor more help join @@assupportchat.`,
34+
{
35+
reply_to_message_id: ctx.message.message_id,
36+
reply_markup
37+
});
38+
} catch (e) { }
3739
});
3840

3941
bot.command('track', async ctx => {
@@ -44,14 +46,14 @@ bot.command('track', async ctx => {
4446
const sentMsg = await ctx.reply(`Tracking ${merchant} product...`, { reply_to_message_id: ctx.message.message_id });
4547
const details = await getProductDetails(productUrl, merchant);
4648
if (details.ok) {
47-
try{
49+
try {
4850
const tracking_id = getRandomId();
4951
await manageProducts({ tracking_id, userId: ctx.from.id, merchant, title: details.title, link: details.link, initPrice: details.price, price: details.price }, 'update');
5052
await ctx.api.editMessageText(ctx.chat.id, sentMsg.message_id,
5153
`<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}`,
5254
{ parse_mode: "HTML", reply_markup }
5355
);
54-
}catch(e){}
56+
} catch (e) { }
5557
} else {
5658
await ctx.api.editMessageText(ctx.chat.id, sentMsg.message_id, `Sorry, I couldn't track this product. Make sure you've sent correct product link.`, { parse_mode: "Markdown", reply_markup });
5759
}
@@ -119,23 +121,25 @@ bot.callbackQuery('stopTracking', async ctx => {
119121
})
120122

121123
const track = async () => {
122-
const products = await manageProducts({}, 'read');
123-
await Promise.all(products.result.map(async product => {
124-
const details = await getProductDetails(product.link, product.merchant);
125-
if (details.price !== product.price) {
126-
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');
127-
bot.api.sendMessage(product.userId, `<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}`,
128-
{
129-
parse_mode: "HTML", reply_markup: {
130-
inline_keyboard: [
131-
[{ text: 'Buy Now', url: details.link }],
132-
[{ text: 'Stop Tracking - ' + product.tracking_id, callback_data: `stopTracking` }]
133-
]
124+
try {
125+
const products = await manageProducts({}, 'read');
126+
await Promise.all(products.result.map(async product => {
127+
const details = await getProductDetails(product.link, product.merchant);
128+
if (details.price !== product.price) {
129+
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');
130+
bot.api.sendMessage(product.userId, `<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}`,
131+
{
132+
parse_mode: "HTML", reply_markup: {
133+
inline_keyboard: [
134+
[{ text: 'Buy Now', url: details.link }],
135+
[{ text: 'Stop Tracking - ' + product.tracking_id, callback_data: `stopTracking` }]
136+
]
137+
}
134138
}
135-
}
136-
);
137-
}
138-
}));
139+
);
140+
}
141+
}));
142+
} catch (e) { }
139143
}
140144

141145
setInterval(track, 3600000); //Track every hr.

0 commit comments

Comments
 (0)