Skip to main content

Patch for python-telegram-bot providing decorator support for easy command handling and admin verification.

Project description

Ptbmod

A Python Patch for python-telegram-bot with Decorator Support

Overview

Ptbmod is a patch for the popular python-telegram-bot library that introduces decorator support for cleaner and more efficient handling of commands and messages within your Telegram bot applications.

Key Features

  • Command and Message Handlers: Simplify the process of registering command and message handlers with the TelegramHandler.
  • Admin Decorators: Easily check user permissions directly in your command functions with decorators like @Admins and verify_anonymous_admin.

Installation

To install ptbmod, run the following command:

pip install ptbmod

Or, to install with additional dependencies:

pip install ptbmod[all]

Usage

To utilize the new decorators and handlers, follow these steps:

  1. Import the necessary modules:
from ptbmod import TelegramHandler, verify_anonymous_admin, Admins
  1. Create a TelegramHandler instance:
Cmd = TelegramHandler(application).command
Msg = TelegramHandler(application).message
  1. Define your bot commands and message handlers using the Cmd and Msg shortcuts:
@Cmd(command=["start", "help"])
@Admins(is_both=True)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # Command handler code here

Example

For a complete example, see the example code below.

Requirements

  • python-telegram-bot
  • cachetools
  • python-dotenv

License

Ptbmod is licensed under the MIT License.

Contributing

Contributions are welcome! Please submit a pull request with your changes.

Example

import logging

from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, filters, CallbackQueryHandler

from ptbmod.decorator import TelegramHandler
from ptbmod.decorator.admins import verify_anonymous_admin, Admins

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)

application = ApplicationBuilder().token('TOKEN').build()

Cmd = TelegramHandler(application).command
Msg = TelegramHandler(application).message

# Define a command handler
@Cmd(command=["start", "help"])
@Admins(is_both=True)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # Command handler code here
    await context.bot.send_message(
        chat_id=update.effective_chat.id,
        text="I'm a bot, please talk to me!"
    )

# Define a message handler
@Msg(filters=filters.ChatType.PRIVATE & ~filters.COMMAND)
async def message(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # Message handler code here
    await context.bot.copy_message(
        chat_id=update.effective_chat.id,
        from_chat_id=update.effective_chat.id,
        message_id=update.effective_message.message_id
    )

if __name__ == '__main__':
    application.add_handler(CallbackQueryHandler(verify_anonymous_admin, pattern=r"^anon."))
    application.run_polling()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ptbmod-0.1.tar.gz (11.0 kB view details)

Uploaded Source

File details

Details for the file ptbmod-0.1.tar.gz.

File metadata

  • Download URL: ptbmod-0.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for ptbmod-0.1.tar.gz
Algorithm Hash digest
SHA256 2b121c133a8d93ad6bd6bac7c7a59c5e00c3162af444714dbf7af5946b18fd20
MD5 b9df320b4f172026dc2dad7bed70f370
BLAKE2b-256 93a84c6aea1cbc141fa89af2808cd987330c68ae96f82ea73868fb7a8fcfe1b0

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page