mgram-sdk
A Python module that turns a Telegram bot into a source of income. It plugs your bot into the MGRAM 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.
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
pip install "mgram-sdk[aiogram]"
# python-telegram-bot: mgram-sdk[ptb]
API key in a minute
- Open @mgram and send your bot's token from @BotFather — the message is deleted at once.
- In @mgram: bot stats → Settings → API access — copy the key.
- Put it into the
MGRAM_API_KEYenvironment variable. Keep the key on your server only.
aiogram 3
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:
MgramGate(mgram, text="Join our sponsors:", check_text="✅ Done", skip_ttl=900, skip_commands=("/help", "/privacy"))
python-telegram-bot
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.
No framework
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:
tasks = await mgram.get_tasks(user_id, init_data=init_data_from_page)
Webhooks instead of polling
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.
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. 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/.
Bot templates in other languages
| Language | Repository | Inside |
|---|---|---|
| 🇷🇺 Русский | monetizaciya-telegram-bota | aiogram 3 bot template, force subscribe, Reply Ads, tests |
| 🇬🇧 English | telegram-bot-monetization | aiogram 3 bot template, force subscribe, Reply Ads, tests |
| 🇪🇸 Español | monetizar-bot-telegram | aiogram 3 bot template, force subscribe, Reply Ads, tests |
| 🇧🇷 Português | ganhar-dinheiro-bot-telegram | aiogram 3 bot template, force subscribe, Reply Ads, tests |
| 🇮🇩 Bahasa Indonesia | monetisasi-bot-telegram | aiogram 3 bot template, force subscribe, Reply Ads, tests |
| 🇹🇷 Türkçe | telegram-bot-para-kazanma | aiogram 3 bot template, force subscribe, Reply Ads, tests |
Contacts
| Bot | @mgram |
| Support | support@mgram.biz |
Links
- Website: mgram.biz · Bot: @mgram · News: @mgram_channel · Chat: @mgram_chat
- API reference: api.mgram.biz · OpenAPI: mgram.biz/openapi.json
- Node.js (grammY, Telegraf) and Mini App examples: mgram.biz/en/developers
MIT © MGRAM
Metadata
Release files for mgram-sdk 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mgram_sdk-1.0.0.tar.gz | 138.2 kB | Without provenance | Details for mgram_sdk-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mgram_sdk-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for mgram_sdk-1.0.0-py3-none-any.whl |
Total release size: 152.7 kB
Release files / mgram_sdk-1.0.0.tar.gz
| Download URL | mgram_sdk-1.0.0.tar.gz |
|---|---|
| Size | 138.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3230c82a37d98044141ecc36bbc45ebf1e7dfe952da5f668f5d4c5e0dfe8a211
|
|
BLAKE2b-256 checksum How to use checksums |
ef31ad80d3917eda974501c90660b246c2721fb2bf4a95e10bf7b04f1aecafef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / mgram_sdk-1.0.0-py3-none-any.whl
| Download URL | mgram_sdk-1.0.0-py3-none-any.whl |
|---|---|
| Size | 14.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
de07f3141932ccca72083a3633cbd71afab97314d02076e4b4e71824266bfd33
|
|
BLAKE2b-256 checksum How to use checksums |
1242236d204317e771fb5a56c5213b652f9a78728883036814876102cbbc7c3c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for mgram-sdk 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.