-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to enable/disable specific embeds (#33)
* Add command to enable/disable specific embeds * The new command "/embed" allows users/groups to enable/disable embeds of specific websites, among the supported ones. * The syntax is "embed website on/off". * This new feature was made possible using Redis, where the bot stores the unique chat ID and its respective preferences. * By default all the values are set to "1", which means that all the supported websites are filtered. * The bot also creates a "local" dictionary containing a copy of the most recent data from Redis, so whenever the bot has to decide whether filtering a specific URL or not it doesn't need to query the database. * Whenever the user changes a filter the database is updated alongside the local dictionary. --------- Co-authored-by: Chris Thurber <8137212+skiman6010@users.noreply.github.com>
- Loading branch information
1 parent
019c59c
commit 7b585ac
Showing
11 changed files
with
196 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
python-dotenv | ||
python-telegram-bot~=13.13 | ||
redis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
BOT_TOKEN="TOKEN GOES HERE" | ||
REDIS_HOST="REDIS HOST GOES HERE, REMOVE FOR LOCALHOST" | ||
REDIS_PORT="REDIS PORT GOES HERE, REMOVE FOR DEFAULT" | ||
REDIS_PASSWORD="OPTIONAL REDIS PASSWORD GOES HERE" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Setup a database to permanenly store the bot's settings for each user.""" | ||
import redis | ||
|
||
|
||
class GimmeDB: | ||
"""Class to handle the bot's database.""" | ||
|
||
def __init__( | ||
self, redis_host: str = None, redis_port: int = None, password: str = None | ||
): | ||
self.redis_host = redis_host or "localhost" | ||
self.redis_port = redis_port or 6379 | ||
self.redis_password = password or None | ||
self.redis_db = redis.Redis( | ||
host=self.redis_host, | ||
port=self.redis_port, | ||
db=0, | ||
password=self.redis_password, | ||
) # pylint: disable=line-too-long | ||
|
||
def create_db(self, chat_id): | ||
"""Create a database for the chat.""" | ||
# Check if a group is already in the database and if not, create it using hashes | ||
if not self.redis_db.sismember("groups", chat_id): | ||
self.redis_db.sadd("groups", chat_id) | ||
self.redis_db.hset(f"group_{format(chat_id)}", "twitter", 1) | ||
self.redis_db.hset(f"group_{format(chat_id)}", "instagram", 1) | ||
self.redis_db.hset(f"group_{format(chat_id)}", "tiktok", 1) | ||
return self.redis_db.hgetall(f"group_{format(chat_id)}") | ||
|
||
def edit_db(self, chat_id, website, value): | ||
"""Edit the database for the chat.""" | ||
updated = self.redis_db.hset(f"group_{format(chat_id)}", website, value) | ||
return updated | ||
|
||
def get_db(self, chat_id): | ||
"""Get the database for the chat in a dictionary.""" | ||
# Create a dictionary from the database | ||
dictionay_db = { | ||
k.decode("utf-8"): v.decode("utf-8") | ||
for k, v in self.redis_db.hgetall(f"group_{format(chat_id)}").items() | ||
} | ||
return dictionay_db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.