Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telegram bot command to filter favorites #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config.defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"showDecrease": false,
"showDecreaseToZero": false,
"showIncrease": false,
"showIncreaseFromZero": true
"showIncreaseFromZero": true,
"wordFilter": true,
"wordsList": []
}
}
13 changes: 12 additions & 1 deletion lib/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand All @@ -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();
Expand Down
97 changes: 97 additions & 0 deletions lib/telegram-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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}
`);
}

Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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");
Expand Down