diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de6d36d --- /dev/null +++ b/.gitignore @@ -0,0 +1,202 @@ +# Created by https://www.toptal.com/developers/gitignore/api/venv,python,visualstudiocode +# Edit at https://www.toptal.com/developers/gitignore?templates=venv,python,visualstudiocode + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### Python Patch ### +# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration +poetry.toml + + +### venv ### +# Virtualenv +# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ +[Bb]in +[Ii]nclude +[Ll]ib +[Ll]ib64 +[Ll]ocal +[Ss]cripts +pyvenv.cfg +pip-selfcheck.json + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# End of https://www.toptal.com/developers/gitignore/api/venv,python,visualstudiocode \ No newline at end of file diff --git a/like_db.json b/like_db.json new file mode 100644 index 0000000..e0d58dc --- /dev/null +++ b/like_db.json @@ -0,0 +1,10 @@ +{ + "677038439": { + "like": 1, + "dislike": 0 + }, + "1066527771": { + "like": 1, + "dislike": 0 + } +} \ No newline at end of file diff --git a/like_db.py b/like_db.py index 4b87a76..7ff18c1 100644 --- a/like_db.py +++ b/like_db.py @@ -12,11 +12,11 @@ def __init__(self, db_path): self.db = {} #Save the database to the database file with open(db_path, 'w') as f: - json.dump(self.db, f) + json.dump(self.db, f, indent=4) def save(self): with open(self.db_path, 'w') as f: - json.dump(self.db, f) + json.dump(self.db, f, indent=4) def all_likes(self): """Counts all users likes @@ -25,8 +25,8 @@ def all_likes(self): """ likes = 0 if self.db: - for user in self.db: - likes += self.db[user]['like'] + for user, data in self.db.items(): + likes += data['like'] return likes @@ -37,8 +37,8 @@ def all_dislikes(self): """ dislikes = 0 if self.db: - for user in self.db: - dislikes += self.db[user]['dislike'] + for user_id, data in self.db.items(): + dislikes += data['dislike'] return dislikes @@ -53,17 +53,14 @@ def add_like(self, user_id:str)->dict: The number of likes and dislikes for the post ''' if self.db.get(user_id): - if self.db[user_id]['like'] == 0: - self.db[user_id]['like'] = 1 - if self.db[user_id]['dislike'] == 1: + if self.db[user_id]['like'] == 0: + self.db[user_id]['like'] = 1 + self.db[user_id]['dislike'] = 0 + elif self.db[user_id]['like'] == 1: + self.db[user_id]['like'] = 0 self.db[user_id]['dislike'] = 0 - elif self.db[user_id]['like'] == 1: - self.db[user_id]['like'] = 0 else: - self.db[user_id] = { - 'like': 1, - 'dislike': 0 - } + self.db[user_id] = {'like': 1, 'dislike': 0} self.save() @@ -79,13 +76,14 @@ def add_dislike(self, user_id:str)->dict: if self.db.get(user_id): if self.db[user_id]['dislike'] == 0: self.db[user_id]['dislike'] = 1 - if self.db[user_id]['like'] == 1: - self.db[user_id]['like'] = 0 + self.db[user_id]['like'] = 0 elif self.db[user_id]['dislike'] == 1: self.db[user_id]['dislike'] = 0 + self.db[user_id]['like'] = 0 else: - self.db[user_id] = { - 'like': 0, - 'dislike': 1 - } + self.db[user_id] = {'like': 0, 'dislike': 1} + self.save() + + def add_user(self, user_id): + self.db[user_id] = {'like': 0, 'dislike': 0} self.save() \ No newline at end of file diff --git a/likebot.py b/likebot.py index 396ae49..19566f0 100644 --- a/likebot.py +++ b/likebot.py @@ -10,15 +10,60 @@ #Create start command handler def start(update:Update, context:CallbackContext): """Starts with picture all likes and all dislikes""" - pass + data = LikeDB('like_db.json') + like = data.all_likes() + liked = data.all_dislikes() + user_id = str(update.message.from_user.id) + dat_db = data.db + data.add_user(user_id=user_id) + user_like = dat_db[user_id]['like'] + user_dislike = dat_db[user_id]['dislike'] + + inlineKeyboard = InlineKeyboardButton(f'👎{liked}',callback_data='dislike') + inlineKeyboard1 = InlineKeyboardButton(f'👍{like}',callback_data='like') + reply_markup = InlineKeyboardMarkup([[inlineKeyboard1, inlineKeyboard]]) + update.message.reply_photo( 'https://www.drupal.org/files/project-images/like_and_dislike.png',reply_markup=reply_markup, caption=f'You have {user_like} likes and {user_dislike} dislikes') def like(update:Update, context:CallbackContext): """Send the message with the number of likes and dislikes""" - pass + query = update.callback_query + user_id = query.from_user.id + + data = LikeDB('like_db.json') + data.add_like(str(user_id)) + user_id2 = str(query.from_user.id) + dat_db = data.db + user_like = dat_db[user_id2]['like'] + user_dislike = dat_db[user_id2]['dislike'] + + like = data.all_likes() + liked = data.all_dislikes() + + inlineKeyboard = InlineKeyboardButton(f'👎{liked}',callback_data='dislike') + inlineKeyboard1 = InlineKeyboardButton(f'👍{like}',callback_data='like') + reply_markup = InlineKeyboardMarkup([[inlineKeyboard1,inlineKeyboard]]) + query.edit_message_caption(reply_markup=reply_markup, caption=f'You have {user_like} likes and {user_dislike} dislikes') def dislike(update:Update, context:CallbackContext): """Send the message with the number of likes and dislikes""" - pass + query = update.callback_query + user_id = query.from_user.id + + data = LikeDB('like_db.json') + data.add_dislike(str(user_id)) + user_id2 = str(query.from_user.id) + dat_db = data.db + user_like = dat_db[user_id2]['like'] + user_dislike = dat_db[user_id2]['dislike'] + + like = data.all_likes() + liked = data.all_dislikes() + print(like, liked) + + inlineKeyboard = InlineKeyboardButton(f'👎{liked}',callback_data='dislike') + inlineKeyboard1 = InlineKeyboardButton(f'👍{like}',callback_data='like') + reply_markup = InlineKeyboardMarkup([[inlineKeyboard1,inlineKeyboard]]) + query.edit_message_caption(reply_markup=reply_markup, caption=f'You have {user_like} likes and {user_dislike} dislikes') #Create updater and dispatcher updater = Updater(TOKEN) @@ -30,7 +75,4 @@ def dislike(update:Update, context:CallbackContext): #Start the bot updater.start_polling() -updater.idle() - - - +updater.idle() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 11a50f9..02548cd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -python-telegram-bot \ No newline at end of file +python-telegram-bot==13.13 \ No newline at end of file