diff --git a/config.defaults.json b/config.defaults.json index 9975450..9689211 100644 --- a/config.defaults.json +++ b/config.defaults.json @@ -41,6 +41,8 @@ "showDecrease": false, "showDecreaseToZero": false, "showIncrease": false, - "showIncreaseFromZero": true + "showIncreaseFromZero": true, + "wordFilter": true, + "wordsList": [] } } diff --git a/lib/notifier.js b/lib/notifier.js index b271fe5..b753db0 100644 --- a/lib/notifier.js +++ b/lib/notifier.js @@ -56,9 +56,12 @@ function notifyIfChanged(businesses) { function filterBusinesses(businessesById) { return Object.keys(businessesById) .filter((key) => { + const storeName = businessesById[key].store.store_name; const current = businessesById[key]; const previous = cache.businessesById[key]; - return hasInterestingChange(current, previous); + return ( + hasInterestingChange(current, previous) && isInWordFilter(storeName) + ); }) .map((key) => businessesById[key]); } @@ -82,6 +85,14 @@ function hasInterestingChange(current, previous) { } } +function isInWordFilter(storeName) { + if (!config.get("messageFilter.wordFilter")) return true; + const storeNameKeys = storeName.toLowerCase().split(" "); + const filterWords = config.get("messageFilter.wordsList"); + + return filterWords.some((word) => storeNameKeys.includes(word)); +} + function notifyConsole(message, options) { if (options.clear) { console.clear(); diff --git a/lib/telegram-bot.js b/lib/telegram-bot.js index 57d3ab2..0727913 100644 --- a/lib/telegram-bot.js +++ b/lib/telegram-bot.js @@ -40,6 +40,32 @@ const botCommands = [ command: "config", description: "Show current configuration.", }, + { + command: "filter_set", + description: + "List of general words to add to filter, with a space after it.", + }, + { + command: "filter_unset", + description: + "List of general words to remove from filter, with a space after it.", + }, + { + command: "filter_get", + description: "List filters currently used", + }, + { + command: "filter_clear", + description: "Clear the list.", + }, + { + command: "filter_on", + description: "Activate filter.", + }, + { + command: "filter_off", + description: "Disable filter.", + }, ]; module.exports = { @@ -90,6 +116,12 @@ function createBot() { bot.command("show_decrease_to_zero", showDecreaseToZeroCommand); bot.command("show_increase", showIncreaseCommand); bot.command("show_increase_from_zero", showIncreaseFromZeroCommand); + bot.command("filter_on", filterOnCommand); + bot.command("filter_off", filterOffCommand); + bot.command("filter_clear", filterClearCommand); + bot.command("filter_get", filterGetCommand); + bot.command("filter_set", filterSetCommand); + bot.command("filter_unset", filterUnsetCommand); bot.launch(); return bot; } @@ -137,6 +169,8 @@ Activate alert for increasing stock : ${ Activate alert for new market available : ${ config.messageFilter.showIncreaseFromZero ? "✅" : "🚫" } +Favorites filtering : ${config.messageFilter.wordFilter ? "✅" : "🚫"} +Words list : ${config.messageFilter.wordsList} `); } @@ -190,6 +224,55 @@ function showIncreaseFromZeroCommand(context) { ); } +function filterOnCommand(context) { + activateOption("wordFilter"); + context.reply("✅ Favorites will be filter depends on the word list."); +} + +function filterOffCommand(context) { + disableOption("wordFilter"); + context.reply("❌ Favorites will not be filter depends on the word list."); +} + +function filterClearCommand(context) { + setCustomOption("wordsList", []); + context.reply("✅ Words list has been cleared."); +} + +function filterGetCommand(context) { + context.reply( + `✅ ${config.messageFilter.wordsList} + ` + ); +} + +function filterUnsetCommand(context) { + const oldWords = context.message.text.split(" "); + oldWords.shift(); + const config = getConfig(); + const newList = config.messageFilter.wordsList.filter( + (word) => !oldWords.includes(word) + ); + setCustomOption("wordsList", newList); + context.reply( + `✅ ${newList} + ` + ); +} + +function filterSetCommand(context) { + const newWords = context.message.text.split(" "); + newWords.shift(); + const config = getConfig(); + + const newList = config.messageFilter.wordsList.concat(newWords); + setCustomOption("wordsList", newList); + context.reply( + `✅ ${newList} + ` + ); +} + function addChat(context) { const chats = getChats(); const chat = { @@ -221,6 +304,20 @@ function setOption(option) { return !activate; } +function setCustomOption(option, value) { + config.set("messageFilter." + option, value); +} + +function activateOption(option) { + config.set("messageFilter." + option, true); + return true; +} + +function disableOption(option) { + config.set("messageFilter." + option, false); + return false; +} + function getConfig() { const messageFilter = config.get("messageFilter"); const bot = config.get("notifications.telegram.enabled");