Skip to main content

privibot

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.
# 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.8

# make it available globally
pyenv global system 3.6.8

Installation

With pip:

python3.6 -m pip install privibot

With pipx:

python3.6 -m pip install --user pipx

pipx install --python python3.6 privibot

Usage

To restrict access to a handler, decorate your callback functions like following:

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:

# privileges.py
from privibot import Privilege, Privileges as Ps


class 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:

from privibot import require_privileges

from .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:

from privibot import User

from .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

Metadata

Release files for privibot 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for privibot 0.1.1
File Size Uploaded
privibot-0.1.1.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for privibot 0.1.1
File Interpreter ABI Platform
privibot-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.9 kB

Release files / privibot-0.1.1.tar.gz

Download URL privibot-0.1.1.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
01d7320bfa9a7ddd8e86de6b72cdb1872271a94b46779f9d9188a4cd78cfac89
BLAKE2b-256 checksum
How to use checksums
3fcd44e6f6dae156a5b759a049a76898986e970f38b06f6745606ed4a7da4801
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.6.8 Linux/5.3.7-arch1-1-ARCH

Release files / privibot-0.1.1-py3-none-any.whl

Download URL privibot-0.1.1-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa5cd667db385ef09806b29a4f5dcba812652a26b97091ed683221c6d1ecf017
BLAKE2b-256 checksum
How to use checksums
0e98fb51a1afc7a8d5eb4c0e29668763980d7dcd41aa575d8659802cdb3e3d78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.6.8 Linux/5.3.7-arch1-1-ARCH

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page