ptbmod


Nameptbmod JSON
Version 0.4 PyPI version JSON
download
home_pagehttps://github.com/AshokShau/ptbmod
SummaryPatch for python-telegram-bot providing decorator support for easy command handling and admin verification.
upload_time2024-11-10 13:02:36
maintainerNone
docs_urlNone
authorAshokShau (Ashok)
requires_python>=3.8
licenseMIT
keywords telegram telegram-bot bot python-telegram-bot decorators command handling admin verification
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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:
```bash
pip install ptbmod
```
Or, to install with additional dependencies:
```bash
pip install ptbmod[all]
```
### Usage

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

1. Import the necessary modules:

```python
from ptbmod import TelegramHandler, verifyAnonymousAdmin, Admins
```

2. Create a `TelegramHandler` instance:
```python
Cmd = TelegramHandler(application).command
Msg = TelegramHandler(application).message
```

3. Define your bot commands and message handlers using the `Cmd` and `Msg` shortcuts:
```python
@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](#example) below.

### Requirements

* `python-telegram-bot`
* `cachetools`
* `python-dotenv`

### License

Ptbmod is licensed under the [MIT License](/LICENSE).

### Contributing

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

### Example

```python
import logging

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

from ptbmod import TelegramHandler, verifyAnonymousAdmin, Admins
from ptbmod.decorator.cache import is_admin

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

application = (
    ApplicationBuilder()
    .token('TOKEN')
    .arbitrary_callback_data(True)
    .defaults(Defaults(allow_sending_without_reply=True))
    .build()
)

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


@Cmd(command=["start", "help"])
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    """
    Send a message when the command /start or /help is issued.
    """
    await context.bot.send_message(
        chat_id=update.effective_chat.id,
        text="Hello! I am a bot.\nUse /kick to kick a user from the chat."
    )


@Cmd(command=["kick"])
@Admins(permissions="can_restrict_members", is_both=True, allow_pm=False)
async def ban(update: Update, _: ContextTypes.DEFAULT_TYPE) -> Message:
    """
    Kick a user from the chat.
    """
    msg = update.effective_message
    reply = msg.reply_to_message
    chat = update.effective_chat
    if not reply:
        return await msg.reply_text("Please reply to a user to kick them.")
    
    user = reply.from_user
    if await is_admin(chat.id, user.id):
        return await msg.reply_text("You can't kick an admin.")
    
    await chat.unban_member(user.id)
    return await msg.reply_text(f"Kicked user {user.full_name}")


@Msg(filters=filters.ChatType.PRIVATE & ~filters.COMMAND)
async def message(update: Update, context: ContextTypes.DEFAULT_TYPE):
    """
    Send a message with the same text as the user's message in a private chat when the
    message is not a command.
    """
    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(verifyAnonymousAdmin, pattern=r"^anon."))
    application.run_polling()
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/AshokShau/ptbmod",
    "name": "ptbmod",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "telegram telegram-bot bot python-telegram-bot decorators command handling admin verification",
    "author": "AshokShau (Ashok)",
    "author_email": "<abishnoi69@outlook.com>",
    "download_url": "https://files.pythonhosted.org/packages/20/56/715a271c8c267dee3268d20688203f507b73f220ce55714afce02c603b52/ptbmod-0.4.tar.gz",
    "platform": null,
    "description": "Ptbmod\n================\n\nA Python Patch for `python-telegram-bot` with Decorator Support\n---------------------------------------------------------\n\n### Overview\n\nPtbmod 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.\n\n### Key Features\n\n* **Command and Message Handlers**: Simplify the process of registering command and message handlers with the `TelegramHandler`.\n* **Admin Decorators**: Easily check user permissions directly in your command functions with decorators like `@Admins` and `verify_anonymous_admin`.\n\n### Installation\n\nTo install ptbmod, run the following command:\n```bash\npip install ptbmod\n```\nOr, to install with additional dependencies:\n```bash\npip install ptbmod[all]\n```\n### Usage\n\nTo utilize the new decorators and handlers, follow these steps:\n\n1. Import the necessary modules:\n\n```python\nfrom ptbmod import TelegramHandler, verifyAnonymousAdmin, Admins\n```\n\n2. Create a `TelegramHandler` instance:\n```python\nCmd = TelegramHandler(application).command\nMsg = TelegramHandler(application).message\n```\n\n3. Define your bot commands and message handlers using the `Cmd` and `Msg` shortcuts:\n```python\n@Cmd(command=[\"start\", \"help\"])\n@Admins(is_both=True)\nasync def start(update: Update, context: ContextTypes.DEFAULT_TYPE):\n    # Command handler code here\n```\n### Example\n\nFor a complete example, see the [example code](#example) below.\n\n### Requirements\n\n* `python-telegram-bot`\n* `cachetools`\n* `python-dotenv`\n\n### License\n\nPtbmod is licensed under the [MIT License](/LICENSE).\n\n### Contributing\n\nContributions are welcome! Please submit a pull request with your changes.\n\n### Example\n\n```python\nimport logging\n\nfrom telegram import Message, Update\nfrom telegram.ext import ApplicationBuilder, ContextTypes, filters, CallbackQueryHandler, Defaults\n\nfrom ptbmod import TelegramHandler, verifyAnonymousAdmin, Admins\nfrom ptbmod.decorator.cache import is_admin\n\nlogging.basicConfig(\n    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',\n    level=logging.INFO\n)\nlogging.getLogger('httpx').setLevel(logging.WARNING)\n\napplication = (\n    ApplicationBuilder()\n    .token('TOKEN')\n    .arbitrary_callback_data(True)\n    .defaults(Defaults(allow_sending_without_reply=True))\n    .build()\n)\n\nCmd = TelegramHandler(application).command\nMsg = TelegramHandler(application).message\n\n\n@Cmd(command=[\"start\", \"help\"])\nasync def start(update: Update, context: ContextTypes.DEFAULT_TYPE):\n    \"\"\"\n    Send a message when the command /start or /help is issued.\n    \"\"\"\n    await context.bot.send_message(\n        chat_id=update.effective_chat.id,\n        text=\"Hello! I am a bot.\\nUse /kick to kick a user from the chat.\"\n    )\n\n\n@Cmd(command=[\"kick\"])\n@Admins(permissions=\"can_restrict_members\", is_both=True, allow_pm=False)\nasync def ban(update: Update, _: ContextTypes.DEFAULT_TYPE) -> Message:\n    \"\"\"\n    Kick a user from the chat.\n    \"\"\"\n    msg = update.effective_message\n    reply = msg.reply_to_message\n    chat = update.effective_chat\n    if not reply:\n        return await msg.reply_text(\"Please reply to a user to kick them.\")\n    \n    user = reply.from_user\n    if await is_admin(chat.id, user.id):\n        return await msg.reply_text(\"You can't kick an admin.\")\n    \n    await chat.unban_member(user.id)\n    return await msg.reply_text(f\"Kicked user {user.full_name}\")\n\n\n@Msg(filters=filters.ChatType.PRIVATE & ~filters.COMMAND)\nasync def message(update: Update, context: ContextTypes.DEFAULT_TYPE):\n    \"\"\"\n    Send a message with the same text as the user's message in a private chat when the\n    message is not a command.\n    \"\"\"\n    await context.bot.copy_message(\n        chat_id=update.effective_chat.id,\n        from_chat_id=update.effective_chat.id,\n        message_id=update.effective_message.message_id\n    )\n\n\nif __name__ == '__main__':\n    application.add_handler(CallbackQueryHandler(verifyAnonymousAdmin, pattern=r\"^anon.\"))\n    application.run_polling()\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Patch for python-telegram-bot providing decorator support for easy command handling and admin verification.",
    "version": "0.4",
    "project_urls": {
        "Community": "https://t.me/GuardxSupport",
        "Documentation": "https://t.me/GuardxSupport",
        "Download": "https://github.com/AshokShau/ptbmod/releases/latest",
        "Homepage": "https://github.com/AshokShau/ptbmod",
        "Source": "https://github.com/AshokShau/ptbmod",
        "Tracker": "https://github.com/AshokShau/ptbmod/issues"
    },
    "split_keywords": [
        "telegram",
        "telegram-bot",
        "bot",
        "python-telegram-bot",
        "decorators",
        "command",
        "handling",
        "admin",
        "verification"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2056715a271c8c267dee3268d20688203f507b73f220ce55714afce02c603b52",
                "md5": "ce39d240a9660bd9c2860ddbae84be0a",
                "sha256": "fb0bd6768b5ce8930336f11ff4a9d4234b2dbc18e5dcc9aa668685d5f9ed0470"
            },
            "downloads": -1,
            "filename": "ptbmod-0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "ce39d240a9660bd9c2860ddbae84be0a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 11473,
            "upload_time": "2024-11-10T13:02:36",
            "upload_time_iso_8601": "2024-11-10T13:02:36.933316Z",
            "url": "https://files.pythonhosted.org/packages/20/56/715a271c8c267dee3268d20688203f507b73f220ce55714afce02c603b52/ptbmod-0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-10 13:02:36",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "AshokShau",
    "github_project": "ptbmod",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "ptbmod"
}
        
Elapsed time: 0.42699s