Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pawamoy/privibot
A Python library to add a privilege/permission system to your Telegram bot.
https://github.com/pawamoy/privibot
permissions privilege telegram-bot
Last synced: 3 months ago
JSON representation
A Python library to add a privilege/permission system to your Telegram bot.
- Host: GitHub
- URL: https://github.com/pawamoy/privibot
- Owner: pawamoy
- License: isc
- Created: 2019-08-11T22:52:51.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-08T23:05:23.000Z (over 4 years ago)
- Last Synced: 2024-09-15T09:51:15.386Z (4 months ago)
- Topics: permissions, privilege, telegram-bot
- Language: Python
- Size: 47.9 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# privibot
[![ci](https://github.com/pawamoy/privibot/workflows/ci/badge.svg)](https://github.com/pawamoy/privibot/actions?query=workflow%3Aci)
[![documentation](https://img.shields.io/badge/docs-mkdocs%20material-blue.svg?style=flat)](https://pawamoy.github.io/privibot/)
[![pypi version](https://img.shields.io/pypi/v/privibot.svg)](https://pypi.org/project/privibot/)Privilege system for Telegram bots.
This library provides decorators to restrict access to your Telegram bot handlers based on privileges given to users.
The privileges are stored in a database through SQLAlchemy (SQLite, Postgres, etc.).## Requirements
privibot requires Python 3.6 or above.
To install Python 3.6, I recommend using
pyenv
.```bash
# install pyenv
git clone https://github.com/pyenv/pyenv ~/.pyenv# setup pyenv (you should also put these three lines in .bashrc or similar)
export PATH="${HOME}/.pyenv/bin:${PATH}"
export PYENV_ROOT="${HOME}/.pyenv"
eval "$(pyenv init -)"# install Python 3.6
pyenv install 3.6.12# make it available globally
pyenv global system 3.6.12
```## Installation
With `pip`:
```bash
python3.6 -m pip install privibot
```With [`pipx`](https://github.com/pipxproject/pipx):
```bash
python3.6 -m pip install --user pipxpipx install --python python3.6 privibot
```## Usage
To restrict access to a handler, decorate your callback functions like following:
```python
from privibot import require_access, require_admin@require_access
def callback_for_registered_users(update, context):
pass
@require_admin
def callback_for_admins_only(update, context):
pass
```To use custom privileges, define them like so:
```python
# privileges.py
from privibot import Privilege, Privileges as Psclass Privileges(Ps):
MEDIA_MANAGER = Privilege(
name="media_manager",
verbose_name="Media Manager",
description="This privilege allows users to act (accept or reject) on media-related requests.",
)
USER_MANAGER = Privilege(
"user_manager", "User Manager", "This privilege allows users to manage access of other users to the bot."
)
TESTER = Privilege("tester", "Tester", "This privilege allows users to test new things.")
```Now simply use these privileges with the decorator:
```python
from privibot import require_privilegesfrom .privileges import Privileges
@require_privileges([Privileges.USER_MANAGER])
def callback_for_user_managers_only(update, context):
pass
```You can also manually check for privileges like so:
```python
from privibot import Userfrom .privileges import Privileges
def some_callback(update, context):
telegram_user = update.effective_user
db_user = User.get_with_id(telegram_user.id)
if db_user.has_privilege(Privileges.TESTER):
# do something
elif db_user.has_privileges([Privileges.MEDIA_MANAGER, Privileges.USER_MANAGER]):
# do something else
```Users who do not pass the privilege test will receive a message saying they have been denied access.
### Built-in handlers
This library also provides handlers and their callbacks for the following commands:
- /start
- /help
- /requestAccess
- /myPrivileges
- /grant
- /revoke