Metadata-Version: 2.4
Name: brizahub
Version: 0.1.0
Summary: Briza Hub API for Telegram bots: earn from your force-sub block. Turnkey check, your own sponsors block, aiogram middleware, webhooks.
Author: Briza Hub
License-Expression: MIT
Project-URL: Homepage, https://brizahub.com/
Project-URL: Documentation, https://brizahub.com/docs/
Project-URL: Changelog, https://brizahub.com/docs/#changelog
Project-URL: Support, https://t.me/BrizaSupport
Keywords: telegram,telegram-bot,aiogram,force-sub,mandatory-subscription,sponsors,monetization,usdt
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Communications :: Chat
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: aiohttp>=3.8
Provides-Extra: aiogram
Requires-Dist: aiogram>=3.0; extra == "aiogram"
Dynamic: license-file

<p align="center">
  <a href="https://brizahub.com/"><img src="https://brizahub.com/sdk/python-banner.png?v=1" width="800"
     alt="brizahub — Python SDK of Briza Hub: force-sub that pays"></a>
</p>

<p align="center">
  <a href="https://pypi.org/project/brizahub/"><img src="https://img.shields.io/pypi/v/brizahub?color=1f8bf0&label=pypi" alt="PyPI"></a>
  <a href="https://pypi.org/project/brizahub/"><img src="https://img.shields.io/pypi/pyversions/brizahub?color=22d3ee" alt="Python versions"></a>
  <a href="https://brizahub.com/docs/"><img src="https://img.shields.io/badge/docs-brizahub.com-1d3fd6" alt="Docs"></a>
  <a href="https://t.me/BrizaSupport"><img src="https://img.shields.io/badge/support-%40BrizaSupport-26a5e4?logo=telegram&logoColor=white" alt="Support"></a>
  <img src="https://img.shields.io/badge/license-MIT-3ddc84" alt="MIT License">
</p>

# brizahub

Earn from your Telegram bot's force-sub (mandatory subscription) block. [Briza Hub](https://brizahub.com/) fills it
with paying sponsors — channels and bots from advertisers all over the world — and pays you in USDT for every
subscriber who stays.

```bash
pip install brizahub              # aiohttp only
pip install "brizahub[aiogram]"   # with the aiogram 3 middleware
```

**Key:** [@BrizaHubBot](https://t.me/BrizaHubBot) → Sell traffic → Connect a bot.

```python
from brizahub import Briza

briza = Briza("your_key_here")

if not await briza.check(message.from_user):        # Briza has shown the sponsors as your bot
    return
```

## Three methods

| Method | What it does | Returns |
|---|---|---|
| `check(user)` | Briza checks the user and, if needed, shows the force-sub block **as your bot** | `True` — let the user in, `False` — the block is shown, stop |
| `get_sponsors(user)` | sponsors for **your own** block | `list[Sponsor]`; `[]` — let the user in |
| `check_sponsors(user)` | after «Check» under your block: who is not joined yet | `list[Sponsor]`; `[]` — let the user in |

None of them ever raises or makes the user wait because of Briza: no key, an outage or a timeout — the user is let in.

## Turnkey: `check`

```python
from brizahub import Briza

briza = Briza("your_key_here")                      # or the BRIZA_KEY environment variable

async def on_message(message):
    if not await briza.check(message.from_user):    # False — Briza has shown the block as your bot
        return
    ...
```

`check` also takes just the id — `await briza.check(user_id)` — or the message / button press itself. With the id
alone we don't know the user's language and Premium: Briza guesses the language, and Premium-only orders (they pay
more) come less often — so pass `message.from_user` when you have it.

Needs your bot token in @BrizaHubBot: with it we only send, update and delete our block message and read public
profiles — never `getUpdates` or `setWebhook`, your updates stay yours. The block updates live: a joined sponsor
disappears from it, and when none are left the block is deleted.

### Your design

Write the block text in Telegram — bold, links, quotes, premium emoji — send it to your bot once and keep it:

```python
from brizahub import message_from

design = message_from(msg, button_join="Join", button_icon="5368324170671202286", button_style="success")
# a plain dict: store it as JSON, then
await briza.check(message.from_user, message=design)
```

Or by hand: `message={"text": '<tg-emoji emoji-id="5368324170671202286">⭐</tg-emoji> <b>$name</b>, join our
sponsors', "rows": 2}`.

| Key | |
|---|---|
| `text` | up to 4096: HTML by default, `parse_mode="MarkdownV2"`, or `entities` (what `message_from` gives); `$name` — the user's first name |
| `rows` | sponsor buttons per row, 1–3 |
| `button_join`, or per type `button_channel` / `button_bot` / `button_url` | button labels, up to 64 |
| `button_icon`, or per type `button_channel_icon` / `button_bot_icon` / `button_url_icon` | premium-emoji icon on the buttons (custom emoji id) |
| `button_style` | `primary`, `success` or `danger` |

Premium emoji in the text and on the buttons show if your bot's owner has Telegram Premium (Telegram's rule). If
Telegram refuses the block, Briza sends it with regular emoji, then as plain text — users never see raw tags.

**aiogram 3 — every handler at once** (optional, instead of `check` in each handler):

```python
from brizahub.aiogram import BrizaMiddleware

dp.message.middleware(BrizaMiddleware(briza))
dp.callback_query.middleware(BrizaMiddleware(briza))
dp.shutdown.register(briza.close)
```

Groups are skipped (`private_only=False` — check them too, the block goes to the group); let admins through with
`skip=lambda event, data: data["event_from_user"].id in ADMINS`; your design — `BrizaMiddleware(briza, message=design)`.

## Your own block: `get_sponsors` + `check_sponsors`

For bots without a token, a custom design, or a block that mixes sponsors from several sources.

```python
from aiogram.types import InlineKeyboardButton as Button, InlineKeyboardMarkup as Markup

def block(sponsors):
    rows = [[Button(text=s.title or "Subscribe", url=s.link)] for s in sponsors]
    rows.append([Button(text="✅ Check", callback_data="briza_check")])
    return Markup(inline_keyboard=rows)

async def on_message(message):
    sponsors = await briza.get_sponsors(message.from_user, limit=3)
    if sponsors:
        await message.answer("Join our sponsors to use the bot:", reply_markup=block(sponsors))
        return
    ...                                             # let the user in

async def on_check(call):                           # callback_data == "briza_check"
    left = await briza.check_sponsors(call.from_user)
    if left:
        await call.message.edit_reply_markup(reply_markup=block(left))
        await call.answer("Join all sponsors first", show_alert=True)
        return
    await call.message.delete()                     # done — let the user in
```

`check_sponsors` takes the user's current sponsors from Briza itself, so you store nothing; pass the list you showed
(`check_sponsors(user, sponsors)`) to save a call. Subscriptions through our links are counted even without
«Check» — the next `get_sponsors` returns only what is left.

## What you pass

Pass the Telegram user object (aiogram, python-telegram-bot, Pyrogram), the message or button press it came with,
or just the id — the package takes what it needs. Values passed by hand win over the object. Something without a
user id (a mistake) lets the user in and is reported by the `brizahub` logger.

| From the user object | By hand | Why |
|---|---|---|
| `user.id` | `user_id=` | required |
| `user.language_code` | `language_code=` | block language, language-targeted orders |
| `user.is_premium` | `is_premium=` | Premium-only orders pay more |
| `user.first_name` | `first_name=` | we compare it with Telegram after the subscription |
| — | `has_photo=` | orders for users with a profile photo |

Options: `limit` — sponsors at a time, 1–10 (default — your bot setting in @BrizaHubBot); `min_price` — only
sponsors paying you at least this, USDT; `smart_links` — `True` recommended: visit tasks and device-targeted orders
pay more. `check` only: `chat_id` — where to show the block (default: the private chat), `message` — your design
(a dict or a Telegram message, see above).

## What you get: `Sponsor`

| Field | |
|---|---|
| `id` | the same as `task_id` in webhooks |
| `type` | `channel`, `group`, `bot` or `link` (a visit) |
| `title` | text for the button |
| `link` | URL for the button |
| `price` | what you earn when the user does it, USDT (`Decimal`) |

## Webhooks

```python
from brizahub import verify_webhook

async def briza_hook(request):                      # aiohttp
    body = await request.read()
    if not verify_webhook(WEBHOOK_SECRET, request.headers, body):
        return web.Response(status=401)
    event = json.loads(body)        # event_id, event (task.pending | task.completed | task.left | ping), task_id, user_id
    ...                             # the same event_id may come again — process it once
    return web.Response(text="ok")  # any 2xx within 5 s
```

## Your numbers

`await briza.me()` — your bot and today's numbers; `await briza.stats(days=7)` — per day; `await briza.balance()` —
earnings and payouts (USDT as decimal strings). These raise `BrizaError` (`.status`, `.error`).

## When Briza is down

- every call fits into `timeout` (3 s); a connection that does not open within 1 s is opened again at once;
- no answer or an error: the user is let in, then no calls for 5 s; key problems (wrong key, paused bot, no bot
  token) go to the `brizahub` logger once a minute;
- a user who passed `check()` is let in for 60 s without a call; one kept-alive connection for all calls.

| `Briza(...)` | Default | |
|---|---|---|
| `key` | `BRIZA_KEY` | your API key |
| `base_url` | `BRIZA_API_URL` or `https://api.brizahub.com` | change only if we announce a new address |
| `timeout` | `3.0` | seconds for a call |
| `cache_ttl` | `60.0` | seconds a passed user is let in without a call; `0` — always ask |
| `fail_pause` | `5.0` | seconds without calls after Briza did not answer |
| `smart_links` | API default | default for every call |
| `session` | own | your `aiohttp.ClientSession` |

[Docs](https://brizahub.com/docs/) (English, Russian, Persian, Arabic) · [@BrizaHubBot](https://t.me/BrizaHubBot) ·
[@BrizaHub](https://t.me/BrizaHub) · support [@BrizaSupport](https://t.me/BrizaSupport) · MIT License
