Add sendMessage function for Discord notifications
This commit is contained in:
17
index.js
17
index.js
@@ -18,6 +18,12 @@ const priceSelectors = {
|
|||||||
|
|
||||||
const excludePage = ["allegro", "homla.com.pl", "home-you.com"];
|
const excludePage = ["allegro", "homla.com.pl", "home-you.com"];
|
||||||
|
|
||||||
|
function sendMessage(message) {
|
||||||
|
axios.post(discordWebhook, {
|
||||||
|
content: message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function compareAndSave(productsPrice) {
|
async function compareAndSave(productsPrice) {
|
||||||
const productsPriceJson =
|
const productsPriceJson =
|
||||||
fs.readFileSync("productsPrice.json", "utf8") || "[]";
|
fs.readFileSync("productsPrice.json", "utf8") || "[]";
|
||||||
@@ -41,15 +47,13 @@ async function compareAndSave(productsPrice) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const product of diffProducts) {
|
for (const product of diffProducts) {
|
||||||
await axios.post(discordWebhook, {
|
sendMessage(
|
||||||
content: `Zmiana ceny **${product.name}**:\nCena: ${product.oldPrice} -> ${product.newPrice}\nLink: ${product.link}`,
|
`Zmiana ceny **${product.name}**:\nCena: ${product.oldPrice} -> ${product.newPrice}\nLink: ${product.link}`
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (diffProducts.length === 0) {
|
if (diffProducts.length === 0) {
|
||||||
await axios.post(discordWebhook, {
|
sendMessage("Brak zmian w cenach");
|
||||||
content: "Brak zmian w cenach",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
@@ -137,6 +141,7 @@ async function init() {
|
|||||||
console.log("Done! Check productsPrice.json");
|
console.log("Done! Check productsPrice.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendMessage("Zaczynam monitoring cen");
|
||||||
cron.schedule("* */3 * * *", async () => {
|
cron.schedule("* */3 * * *", async () => {
|
||||||
await init();
|
await init();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user