Metadata-Version: 2.5
Name: mgram-sdk
Version: 1.0.0
Summary: Monetize Telegram bots with MGRAM: sponsor force-subscribe tasks and Reply Ads for aiogram 3 and python-telegram-bot
Project-URL: Homepage, https://mgram.biz/en/developers?utm_source=pypi&utm_medium=mgram-sdk
Project-URL: Documentation, https://api.mgram.biz
Project-URL: Source, https://github.com/mgram-web/mgram-sdk
Project-URL: Telegram bot, https://t.me/mgram
Project-URL: MCP server, https://github.com/mgram-web/mgram-sdk/tree/main/mcp
Project-URL: Bot templates, https://github.com/mgram-web/telegram-bot-monetization
Author-email: MGRAM <support@mgram.biz>
License: MIT
License-File: LICENSE
Keywords: ads,aiogram,force-subscribe,mgram,mini-app,monetization,python-telegram-bot,telegram,telegram-bot
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
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
Requires-Python: >=3.10
Requires-Dist: httpx>=0.24
Provides-Extra: aiogram
Requires-Dist: aiogram>=3.0; extra == 'aiogram'
Provides-Extra: dev
Requires-Dist: aiogram>=3.0; extra == 'dev'
Requires-Dist: anyio>=4; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: python-telegram-bot>=20.0; extra == 'dev'
Requires-Dist: ruff>=0.4; extra == 'dev'
Provides-Extra: ptb
Requires-Dist: python-telegram-bot>=20.0; extra == 'ptb'
Description-Content-Type: text/markdown

<p align="center">
  <img src="https://raw.githubusercontent.com/mgram-web/mgram-sdk/main/assets/banner.png" alt="mgram-sdk — Telegram bot monetization: sponsor tasks and Reply Ads" width="100%">
</p>

<p align="center">
  <a href="https://github.com/mgram-web/mgram-sdk/actions/workflows/ci.yml"><img alt="tests" src="https://github.com/mgram-web/mgram-sdk/actions/workflows/ci.yml/badge.svg"></a>
  <img alt="Python 3.10+" src="https://img.shields.io/badge/python-3.10%2B-f2c94c?labelColor=111111">
  <img alt="aiogram 3" src="https://img.shields.io/badge/aiogram-3.x-f2c94c?labelColor=111111">
  <img alt="python-telegram-bot" src="https://img.shields.io/badge/python--telegram--bot-20%2B-f2c94c?labelColor=111111">
  <a href="LICENSE"><img alt="MIT" src="https://img.shields.io/badge/license-MIT-f2c94c?labelColor=111111"></a>
  <a href="https://t.me/mgram?start=gh_s_en_badge"><img alt="@mgram" src="https://img.shields.io/badge/Telegram-@mgram-f2c94c?labelColor=111111&logo=telegram&logoColor=white"></a>
</p>

# mgram-sdk

**A Python module that turns a Telegram bot into a source of income.** It plugs your bot into the [MGRAM](https://mgram.biz/en?utm_source=pypi&utm_medium=mgram-sdk&utm_campaign=en&utm_content=lead) ad network: sponsor tasks before your answer (a force-subscribe gate) and an ad after it. The bot owner gets **70% of every placement** — from the first user, paid out in USDT.

```python
gate = MgramGate(AsyncMgram(MGRAM_API_KEY))
gate.setup(dp)              # sponsor tasks + a “Check” button — one line
await gate.show_ad(message) # an ad after a useful answer
```

---

## Features

| | |
|---|---|
| **Sponsor tasks** | Before answering, the bot shows “join the channel / start the bot” buttons. You earn for every completion Telegram confirms. |
| **Reply Ads** | After a useful answer MGRAM sends a short ad from your bot itself. Paid per impression. |
| **Never breaks your bot** | If the API is down or there are no tasks, the user simply goes through. |
| **No extra calls** | Users who passed are not re-checked for 10 minutes (configurable). |
| **Webhooks** | `task.completed` and `task.refunded` to your server; HMAC-SHA256 signature check in one call. |
| **Any stack** | aiogram 3, python-telegram-bot 20+, a sync client for Flask, Django and scripts. |

## Install

```bash
pip install "mgram-sdk[aiogram]"
# python-telegram-bot:  mgram-sdk[ptb]
```

## API key in a minute

1. Open [@mgram](https://t.me/mgram?start=add_token-gh_s_en) and send your bot's token from @BotFather — the message is deleted at once.
2. In @mgram: **bot stats → Settings → API access** — copy the key.
3. Put it into the `MGRAM_API_KEY` environment variable. Keep the key on your server only.

## aiogram 3

```python
import asyncio, os
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from mgram_sdk import AsyncMgram
from mgram_sdk.aiogram import MgramGate

bot, dp = Bot(os.environ["BOT_TOKEN"]), Dispatcher()
gate = MgramGate(AsyncMgram(os.environ["MGRAM_API_KEY"]), text="Join our sponsors to continue:")
gate.setup(dp)  # every message goes through sponsor tasks


@dp.message(F.text)
async def handle(message: Message):
    await message.answer("Done!")   # your bot's useful answer
    await gate.show_ad(message)       # then an ad


asyncio.run(dp.start_polling(bot))
```

Window texts, “passed — don't ask again” time, commands without the gate:

```python
MgramGate(mgram, text="Join our sponsors:", check_text="✅ Done", skip_ttl=900, skip_commands=("/help", "/privacy"))
```

## python-telegram-bot

```python
from mgram_sdk.ptb import gate_or_none, keyboard

async def on_text(update, context):
    tasks = await gate_or_none(mgram, update.effective_user)
    if tasks:
        await update.message.reply_text("Join our sponsors to continue:", reply_markup=keyboard(tasks))
        return
    await update.message.reply_text("Done!")
    await mgram.show_ad(update.effective_user.id, update.message.message_id)
```

Full example with the “Check” button — [examples/ptb_bot.py](https://github.com/mgram-web/mgram-sdk/blob/main/examples/ptb_bot.py).

## No framework

```python
from mgram_sdk import Mgram

with Mgram(os.environ["MGRAM_API_KEY"]) as mgram:
    tasks = mgram.get_tasks(user_id, language_code="en")
    if tasks.skip:
        ...  # let the user through
    for task in tasks.pending:
        print(task.button_text, task.link)
```

## Mini App

Never put the key into the page. The page sends `Telegram.WebApp.initData` to your server, and the server passes it to MGRAM:

```python
tasks = await mgram.get_tasks(user_id, init_data=init_data_from_page)
```

## Webhooks instead of polling

```python
from mgram_sdk import verify_signature, parse_event

if verify_signature(SECRET, headers["X-MGRAM-Timestamp"], body, headers["X-MGRAM-Signature"]):
    event = parse_event(body)   # event.completed / event.refunded / event.task
```

Turn on: `Mgram(key).set_webhook("https://example.com/mgram/tasks")` — the response has the `secret`. FastAPI example — [examples/webhook_fastapi.py](https://github.com/mgram-web/mgram-sdk/blob/main/examples/webhook_fastapi.py).

## How much it pays

Income depends on your bot's live audience and on how many campaigns fit it right now — we don't promise a monthly figure. What is paid:

| Format | From bot size | To the owner |
|---|---|---|
| Sponsor tasks (force subscribe) | the first user | per confirmed subscription and bot start |
| Reply Ads | 50 users | per 1,000 impressions |
| Broadcasts (set up in @mgram) | 50 users | per delivered message |

Current rates — [mgram.biz/en/monetize-telegram-bot](https://mgram.biz/en/monetize-telegram-bot?utm_source=pypi&utm_medium=mgram-sdk&utm_campaign=en&utm_content=rates). Only actions Telegram confirms are paid; farms and repeats are not. Payouts to CryptoBot or USDT (TON), no documents. Most campaigns today come from Russian-speaking advertisers.

## MCP server for AI assistants

For advertisers: MGRAM connects to Claude, ChatGPT, Cursor and VS Code as an MCP server at `https://mcp.mgram.biz/mcp`. The assistant quotes, launches and tracks campaigns, and money is spent only after your explicit yes. Setup and the tool list: [mcp/](https://github.com/mgram-web/mgram-sdk/tree/main/mcp).

## Bot templates in other languages

| Language | Repository | Inside |
|---|---|---|
| 🇷🇺 Русский | [monetizaciya-telegram-bota](https://github.com/mgram-web/monetizaciya-telegram-bota) | aiogram 3 bot template, force subscribe, Reply Ads, tests |
| 🇬🇧 English | [telegram-bot-monetization](https://github.com/mgram-web/telegram-bot-monetization) | aiogram 3 bot template, force subscribe, Reply Ads, tests |
| 🇪🇸 Español | [monetizar-bot-telegram](https://github.com/mgram-web/monetizar-bot-telegram) | aiogram 3 bot template, force subscribe, Reply Ads, tests |
| 🇧🇷 Português | [ganhar-dinheiro-bot-telegram](https://github.com/mgram-web/ganhar-dinheiro-bot-telegram) | aiogram 3 bot template, force subscribe, Reply Ads, tests |
| 🇮🇩 Bahasa Indonesia | [monetisasi-bot-telegram](https://github.com/mgram-web/monetisasi-bot-telegram) | aiogram 3 bot template, force subscribe, Reply Ads, tests |
| 🇹🇷 Türkçe | [telegram-bot-para-kazanma](https://github.com/mgram-web/telegram-bot-para-kazanma) | aiogram 3 bot template, force subscribe, Reply Ads, tests |

## Contacts

| | |
|---|---|
| Bot | [@mgram](https://t.me/mgram?start=gh_s_en_contacts) |
| Support | [support@mgram.biz](mailto:support@mgram.biz) |

## Links

- Website: [mgram.biz](https://mgram.biz/en?utm_source=pypi&utm_medium=mgram-sdk&utm_campaign=en&utm_content=links) · Bot: [@mgram](https://t.me/mgram?start=gh_s_en_links) · News: [@mgram_channel](https://t.me/mgram_channel) · Chat: [@mgram_chat](https://t.me/mgram_chat)
- API reference: [api.mgram.biz](https://api.mgram.biz/?utm_source=pypi&utm_medium=mgram-sdk&utm_campaign=en&utm_content=links) · OpenAPI: [mgram.biz/openapi.json](https://mgram.biz/openapi.json)
- Node.js (grammY, Telegraf) and Mini App examples: [mgram.biz/en/developers](https://mgram.biz/en/developers?utm_source=pypi&utm_medium=mgram-sdk&utm_campaign=en&utm_content=links)

<sub>MIT © MGRAM</sub>
