Metadata-Version: 2.4
Name: vkworkspace
Version: 1.0.1
Summary: Fully async framework for building VK Teams (VK Workspace) bots, inspired by aiogram 3
Project-URL: Homepage, https://github.com/TimmekHW/vkworkspace
Project-URL: Repository, https://github.com/TimmekHW/vkworkspace
Project-URL: Issues, https://github.com/TimmekHW/vkworkspace/issues
Project-URL: Documentation, https://github.com/TimmekHW/vkworkspace#readme
Author-email: Timerkhan Fattakhov <timmek@users.noreply.github.com>
License: MIT
License-File: LICENSE
Keywords: aiogram,async,bot,framework,httpx,mailru-im-bot,myteam,pydantic,superapp,teams,vk,vk-teams,vk-workspace,vkteams,vkworkspace,workspace
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Communications :: Chat
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: httpx<1.0,>=0.28.1
Requires-Dist: magic-filter<2.0,>=1.0.12
Requires-Dist: pydantic<3.0,>=2.6
Provides-Extra: all
Requires-Dist: mypy>=1.10; extra == 'all'
Requires-Dist: pytest-asyncio>=0.24; extra == 'all'
Requires-Dist: pytest-cov>=5.0; extra == 'all'
Requires-Dist: pytest>=8.0; extra == 'all'
Requires-Dist: redis[hiredis]<6.0,>=5.0; extra == 'all'
Requires-Dist: ruff>=0.5; extra == 'all'
Provides-Extra: dev
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Provides-Extra: redis
Requires-Dist: redis[hiredis]<6.0,>=5.0; extra == 'redis'
Description-Content-Type: text/markdown

# vkworkspace

**Fully asynchronous** framework for building bots on **VK Teams** (VK Workspace / VK SuperApp), inspired by [aiogram 3](https://github.com/aiogram/aiogram).

A modern replacement for the official [mail-ru-im/bot-python](https://github.com/mail-ru-im/bot-python) (`mailru-im-bot`) library.

[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/TimmekHW/vkworkspace/blob/main/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/vkworkspace.svg)](https://pypi.org/project/vkworkspace/)

> **[README на русском языке](https://github.com/TimmekHW/vkworkspace/blob/main/README_RU.md)**

## vkworkspace vs mailru-im-bot

The official [mailru-im-bot](https://github.com/mail-ru-im/bot-python) library is synchronous, based on `requests`, and has not been updated in years. **vkworkspace** is a ground-up async rewrite with a modern developer experience.

### Feature Comparison

| | mailru-im-bot | vkworkspace |
|---|:---:|:---:|
| **HTTP client** | `requests` (sync) | `httpx` (async) |
| **Models** | raw `dict` | Pydantic v2 |
| **Router / Dispatcher** | — | aiogram-style |
| **Magic filters (`F`)** | — | `F.text`, `F.chat.type == "private"` |
| **FSM (state machine)** | — | Memory + Redis backends |
| **Middleware pipeline** | — | inner/outer, per-event type |
| **Inline keyboard builder** | — | `InlineKeyboardBuilder` |
| **Rate limiter** | — | built-in token-bucket |
| **Proxy support** | — | `proxy=` parameter |
| **Handler DI** | — | auto-inject `bot`, `state`, `command` |
| **Custom command prefixes** | — | `prefix=("/", "!", "")` |
| **Error handlers** | — | `@router.error()` |
| **Type hints** | partial | full |

### Speed Benchmarks

Tested on Python 3.11, VK Teams corporate instance. 5 rounds per discipline, median values:

```
                          mailru-im-bot    vkworkspace     winner
                          (requests sync)  (httpx async)
 Bot Info (self/get)         34 ms            33 ms        ~tie
 Send Text                   82 ms           109 ms        mailru-im-bot
 Send Keyboard               97 ms            90 ms        vkworkspace
 Edit Message                57 ms            44 ms        vkworkspace
 Delete Message              33 ms            38 ms        mailru-im-bot
 Chat Info                   43 ms            42 ms        ~tie
 Send Actions                30 ms            28 ms        vkworkspace
 BURST (10 msgs)           1110 ms           335 ms        vkworkspace (3.3x!)
                          ───────────────────────────────────────────
 Score                        2                4           vkworkspace wins
```

**Key takeaway:** For single sequential requests, both libraries are within network noise (~1 ms difference). But when you need to send multiple messages or handle concurrent users, async wins decisively — **3.3x faster** on burst operations via `asyncio.gather()`.

The framework overhead is **zero** in real conditions. Network latency dominates 99%+ of total time.

## Why async?

VK Teams bots spend 99% of their time waiting for the network. Synchronous frameworks (`requests`) block the entire process on every API call. **vkworkspace** uses `httpx.AsyncClient` and `asyncio` — while one request waits for a response, the bot handles other messages.

## Features

- **100% async** — `httpx` + `asyncio`, zero blocking calls, real concurrency
- **Aiogram-like API** — `Router`, `Dispatcher`, `F` magic filters, middleware, FSM
- **Type-safe** — Pydantic v2 models for all API types, full type hints
- **Flexible filtering** — `Command`, `StateFilter`, `ChatTypeFilter`, `CallbackData`, regex, magic filters
- **Custom command prefixes** — `Command("start", prefix=("/", "!", ""))` for any prefix style
- **FSM** — Finite State Machine with Memory and Redis storage backends
- **Middleware** — inner/outer middleware pipeline for logging, auth, throttling
- **Keyboard builder** — fluent API for inline keyboards with button styles
- **Rate limiter** — built-in request throttling (`rate_limit=5` = max 5 req/sec)
- **Proxy support** — route API requests through corporate proxy
- **Edited message routing** — `handle_edited_as_message=True` to process edits as new messages
- **Lifecycle hooks** — `on_startup` / `on_shutdown` for init/cleanup logic
- **Error handlers** — `@router.error()` to catch exceptions from any handler
- **Multi-bot polling** — `dp.start_polling(bot1, bot2)` to poll multiple bots
- **9 event types** — message, edited, deleted, pinned, unpinned, members join/leave, chat info, callbacks
- **Python 3.11 — 3.14** support (free-threaded / no-GIL build not yet tested)

## Installation

```bash
pip install vkworkspace
```

With Redis FSM storage:

```bash
pip install vkworkspace[redis]
```

## Quick Start

```python
import asyncio
from vkworkspace import Bot, Dispatcher, Router, F
from vkworkspace.filters import Command
from vkworkspace.types import Message

router = Router()

@router.message(Command("start"))
async def cmd_start(message: Message) -> None:
    await message.answer("Hello! I'm your bot.")

@router.message(F.text)
async def echo(message: Message) -> None:
    await message.answer(message.text)

async def main() -> None:
    bot = Bot(token="YOUR_TOKEN", api_url="https://myteam.mail.ru/bot/v1")
    dp = Dispatcher()
    dp.include_router(router)
    await dp.start_polling(bot)

asyncio.run(main())
```

> **API URL:** Each company has its own VK Teams API endpoint. Check with your IT/integrator for the correct URL. Common examples:
> - `https://myteam.mail.ru/bot/v1` (Mail.ru default)
> - `https://api.teams.yourcompany.ru/bot/v1` (on-premise)
> - `https://agent.mail.ru/bot/v1` (SaaS variant)
>
> Even SaaS deployments may have custom URLs — always verify with your administrator.

## Bot Configuration

```python
bot = Bot(
    token="YOUR_TOKEN",
    api_url="https://myteam.mail.ru/bot/v1",
    timeout=30.0,               # Request timeout (seconds)
    poll_time=60,               # Long-poll timeout (seconds)
    rate_limit=5.0,             # Max 5 requests/sec (None = unlimited)
    proxy="http://proxy:8080",  # HTTP proxy for corporate networks
)
```

### Rate Limiter

Built-in token-bucket rate limiter prevents API throttling:

```python
# Max 10 requests per second — framework queues the rest automatically
bot = Bot(token="TOKEN", api_url="URL", rate_limit=10)
```

### Proxy

Corporate networks often require a proxy. The `proxy` parameter applies only to API calls:

```python
bot = Bot(
    token="TOKEN",
    api_url="https://api.internal.corp/bot/v1",
    proxy="http://corp-proxy.internal:3128",
)
```

## Dispatcher

```python
dp = Dispatcher(
    storage=MemoryStorage(),            # FSM storage (default: MemoryStorage)
    fsm_strategy="user_in_chat",        # FSM key strategy
    handle_edited_as_message=False,     # Route edits to @router.message handlers
)

# Lifecycle hooks
@dp.on_startup
async def on_start():
    print("Bot started!")

@dp.on_shutdown
async def on_stop():
    print("Bot stopped!")

# Include routers
dp.include_router(router)
dp.include_routers(admin_router, user_router)

# Start polling (supports multiple bots)
await dp.start_polling(bot)
await dp.start_polling(bot1, bot2, skip_updates=True)
```

### Edited Message Routing

When `handle_edited_as_message=True`, edited messages are routed to `@router.message` handlers instead of `@router.edited_message`. Useful when you don't need to distinguish between new and edited messages:

```python
dp = Dispatcher(handle_edited_as_message=True)

# This handler fires for BOTH new and edited messages
@router.message(F.text)
async def handle_text(message: Message) -> None:
    await message.answer(f"Got: {message.text}")
```

## Router & Event Types

```python
router = Router(name="my_router")

# All 9 supported event types:
@router.message(...)              # New message
@router.edited_message(...)       # Edited message
@router.deleted_message(...)      # Deleted message
@router.pinned_message(...)       # Pinned message
@router.unpinned_message(...)     # Unpinned message
@router.new_chat_members(...)     # Users joined
@router.left_chat_members(...)    # Users left
@router.changed_chat_info(...)    # Chat info changed
@router.callback_query(...)       # Button clicked

# Error handler — catches exceptions from any handler
@router.error()
async def on_error(event, error: Exception) -> None:
    print(f"Error: {error}")

# Sub-routers for modular code
admin_router = Router(name="admin")
user_router = Router(name="user")
main_router = Router()
main_router.include_routers(admin_router, user_router)
```

## Filters

### Command Filter

```python
from vkworkspace.filters import Command

# Basic
@router.message(Command("start"))
@router.message(Command("help", "info"))     # Multiple commands

# Custom prefixes
@router.message(Command("start", prefix="/"))           # Only /start
@router.message(Command("menu", prefix=("/", "!", ""))) # /menu, !menu, menu

# Regex commands
import re
@router.message(Command(re.compile(r"cmd_\d+")))        # /cmd_1, /cmd_42, ...

# Access command arguments in handler
@router.message(Command("ban"))
async def ban_user(message: Message, command: CommandObject) -> None:
    user_to_ban = command.args  # Text after "/ban "
    await message.answer(f"Banned: {user_to_ban}")
```

`CommandObject` injected into handler:
- `prefix` — matched prefix (`"/"`, `"!"`, etc.)
- `command` — command name (`"ban"`)
- `args` — arguments after command (`"user123"`)
- `raw_text` — full message text
- `match` — `re.Match` if regex pattern was used

### Magic Filter (F)

```python
from vkworkspace import F

@router.message(F.text)                              # Has text
@router.message(F.text == "hello")                   # Exact match
@router.message(F.text.startswith("hi"))             # String methods
@router.message(F.from_user.user_id == "admin@co")   # Nested attrs
@router.message(F.chat.type == "private")            # Private chat
@router.message(F.chat.type.in_(["private", "group"]))
@router.callback_query(F.callback_data == "confirm")
```

### State Filter

```python
from vkworkspace.filters.state import StateFilter

@router.message(StateFilter(Form.name))     # Specific state
@router.message(StateFilter("*"))           # Any state (non-None)
@router.message(StateFilter(None))          # No state (default)
```

### Other Filters

```python
from vkworkspace.filters import CallbackData, ChatTypeFilter, RegexpFilter

# Callback data
@router.callback_query(CallbackData("confirm"))
@router.callback_query(CallbackData(re.compile(r"^action_\d+$")))

# Chat type
@router.message(ChatTypeFilter("private"))
@router.message(ChatTypeFilter(["private", "group"]))

# Regex on message text
@router.message(RegexpFilter(r"\d{4}"))

# Combine filters with &, |, ~
@router.message(ChatTypeFilter("private") & Command("secret"))
```

### Custom Filters

```python
from vkworkspace.filters.base import BaseFilter

class IsAdmin(BaseFilter):
    async def __call__(self, event, **kwargs) -> bool:
        admins = await event.bot.get_chat_admins(event.chat.chat_id)
        return any(a.user_id == event.from_user.user_id for a in admins)

@router.message(IsAdmin())
async def admin_only(message: Message) -> None:
    await message.answer("Admin panel")
```

## Inline Keyboards

```python
from vkworkspace.utils.keyboard import InlineKeyboardBuilder
from vkworkspace.enums import ButtonStyle

builder = InlineKeyboardBuilder()
builder.button(text="Yes", callback_data="confirm", style=ButtonStyle.PRIMARY)
builder.button(text="No", callback_data="cancel", style=ButtonStyle.ATTENTION)
builder.button(text="Maybe", callback_data="maybe")
builder.adjust(2, 1)  # Row 1: 2 buttons, Row 2: 1 button

await message.answer("Are you sure?", inline_keyboard_markup=builder.as_markup())

# Copy & modify
builder2 = builder.copy()
builder2.button(text="Extra", callback_data="extra")
```

## FSM (Finite State Machine)

```python
from vkworkspace.fsm import StatesGroup, State, FSMContext
from vkworkspace.filters.state import StateFilter
from vkworkspace.fsm.storage.memory import MemoryStorage

class Form(StatesGroup):
    name = State()
    age = State()

@router.message(Command("start"))
async def start(message: Message, state: FSMContext) -> None:
    await state.set_state(Form.name)
    await message.answer("What is your name?")

@router.message(StateFilter(Form.name), F.text)
async def process_name(message: Message, state: FSMContext) -> None:
    await state.update_data(name=message.text)
    await state.set_state(Form.age)
    await message.answer("How old are you?")

@router.message(StateFilter(Form.age), F.text)
async def process_age(message: Message, state: FSMContext) -> None:
    data = await state.get_data()
    await message.answer(f"Name: {data['name']}, Age: {message.text}")
    await state.clear()

# Storage backends
dp = Dispatcher(storage=MemoryStorage())          # In-memory (dev)

from vkworkspace.fsm.storage.redis import RedisStorage
dp = Dispatcher(storage=RedisStorage())           # Redis (prod)
```

FSMContext methods:
- `await state.get_state()` — current state (or `None`)
- `await state.set_state(Form.name)` — transition to state
- `await state.get_data()` — get stored data dict
- `await state.update_data(key=value)` — merge into data
- `await state.set_data({...})` — replace data entirely
- `await state.clear()` — clear state and data

## Custom Middleware

```python
from vkworkspace import BaseMiddleware

class LoggingMiddleware(BaseMiddleware):
    async def __call__(self, handler, event, data):
        print(f"Event from: {event.from_user.user_id}")
        result = await handler(event, data)
        print(f"Handled OK")
        return result

class ThrottleMiddleware(BaseMiddleware):
    def __init__(self, delay: float = 1.0):
        self.delay = delay
        self.last: dict[str, float] = {}

    async def __call__(self, handler, event, data):
        import time
        uid = getattr(event, "from_user", None)
        uid = uid.user_id if uid else "unknown"
        now = time.monotonic()
        if now - self.last.get(uid, 0) < self.delay:
            return None  # Skip handler — too fast
        self.last[uid] = now
        return await handler(event, data)

# Register on specific event observer
router.message.middleware.register(LoggingMiddleware())
router.message.middleware.register(ThrottleMiddleware(delay=0.5))
router.callback_query.middleware.register(LoggingMiddleware())
```

## Message Methods

```python
# In handler:
await message.answer("Hello!")                                # Send to same chat
await message.reply("Reply to you!")                          # Reply to this message
await message.edit_text("Updated text")                       # Edit this message
await message.delete()                                        # Delete this message
await message.pin()                                           # Pin this message
await message.unpin()                                         # Unpin this message
await message.answer_file(file=InputFile("photo.jpg"))        # Send file
await message.answer_voice(file=InputFile("audio.ogg"))       # Send voice
```

## Bot API Methods

```python
bot = Bot(token="TOKEN", api_url="URL")

# Self
await bot.get_me()

# Messages
await bot.send_text(chat_id, "Hello!", parse_mode="HTML")
await bot.edit_text(chat_id, msg_id, "Updated")
await bot.delete_messages(chat_id, msg_id)
await bot.send_file(chat_id, file=InputFile("photo.jpg"), caption="Look!")
await bot.send_voice(chat_id, file=InputFile("voice.ogg"))
await bot.answer_callback_query(query_id, "Done!", show_alert=True)

# Chat management
await bot.get_chat_info(chat_id)
await bot.get_chat_admins(chat_id)
await bot.get_chat_members(chat_id)
await bot.get_blocked_users(chat_id)
await bot.get_pending_users(chat_id)
await bot.set_chat_title(chat_id, "New Title")
await bot.set_chat_about(chat_id, "Description")
await bot.set_chat_rules(chat_id, "Rules")
await bot.block_user(chat_id, user_id, del_last_messages=True)
await bot.unblock_user(chat_id, user_id)
await bot.resolve_pending(chat_id, approve=True, user_id=uid)
await bot.delete_chat_members(chat_id, members=[uid1, uid2])
await bot.pin_message(chat_id, msg_id)
await bot.unpin_message(chat_id, msg_id)
await bot.send_actions(chat_id, "typing")

# Files & Threads
await bot.get_file_info(file_id)
await bot.threads_add(chat_id, msg_id)
await bot.threads_get_subscribers(thread_id)
await bot.threads_autosubscribe(chat_id, enable=True)
```

## Handler Dependency Injection

Handlers receive only the parameters they declare. The framework inspects the signature and injects available values:

```python
@router.message(Command("info"))
async def full_handler(
    message: Message,              # The message object
    bot: Bot,                      # Bot instance
    state: FSMContext,             # FSM context (if storage configured)
    command: CommandObject,        # Parsed command (from Command filter)
    raw_event: dict,               # Raw VK Teams event dict
    event_type: str,               # "message", "callback_query", etc.
) -> None:
    ...

# Minimal — only take what you need
@router.message(F.text)
async def simple(message: Message) -> None:
    await message.answer(message.text)
```

## Examples

| Example | Description |
|---------|-------------|
| [echo_bot.py](https://github.com/TimmekHW/vkworkspace/blob/main/examples/echo_bot.py) | Basic commands + text echo |
| [keyboard_bot.py](https://github.com/TimmekHW/vkworkspace/blob/main/examples/keyboard_bot.py) | Inline keyboards + callback handling |
| [fsm_bot.py](https://github.com/TimmekHW/vkworkspace/blob/main/examples/fsm_bot.py) | Multi-step dialog with FSM |
| [middleware_bot.py](https://github.com/TimmekHW/vkworkspace/blob/main/examples/middleware_bot.py) | Custom middleware (logging, access control) |
| [proxy_bot.py](https://github.com/TimmekHW/vkworkspace/blob/main/examples/proxy_bot.py) | Corporate proxy + rate limiter |
| [error_handling_bot.py](https://github.com/TimmekHW/vkworkspace/blob/main/examples/error_handling_bot.py) | Error handlers, lifecycle hooks, edited message routing |
| [custom_prefix_bot.py](https://github.com/TimmekHW/vkworkspace/blob/main/examples/custom_prefix_bot.py) | Custom command prefixes, regex commands, argument parsing |
| [multi_router_bot.py](https://github.com/TimmekHW/vkworkspace/blob/main/examples/multi_router_bot.py) | Modular sub-routers, chat events (join/leave/pin) |

## Project Structure

```
vkworkspace/
├── client/          # Async HTTP client (httpx), rate limiter, proxy
├── types/           # Pydantic v2 models (Message, Chat, User, CallbackQuery, ...)
├── enums/           # EventType, ChatType, ParseMode, ButtonStyle, ...
├── dispatcher/      # Dispatcher, Router, EventObserver, Middleware pipeline
├── filters/         # Command, StateFilter, CallbackData, ChatType, Regexp
├── fsm/             # StatesGroup, State, FSMContext, Memory/Redis storage
└── utils/           # InlineKeyboardBuilder, magic filter (F)
```

## Requirements

- Python 3.11+
- httpx >= 0.28.1
- pydantic >= 2.6
- magic-filter >= 1.0.12

## License

MIT [LICENSE](https://github.com/TimmekHW/vkworkspace/blob/main/LICENSE).

---

<sub>**Keywords:** VK Teams bot, VK Workspace bot, VK WorkSpace, VK SuperApp, vkteams, workspace.vk.ru, workspacevk, vkworkspace, myteam.mail.ru, agent.mail.ru, mail-ru-im-bot, mailru-im-bot, bot-python, VK Teams API, VK Teams framework, async bot framework, Python bot VK Teams, aiogram VK Teams, httpx bot, Pydantic bot framework</sub>
