Skip to main content

mgram-sdk — Telegram bot monetization: sponsor tasks and Reply Ads

tests Python 3.10+ aiogram 3 python-telegram-bot MIT @mgram

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

  1. Open @mgram 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

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

MIT © MGRAM

Metadata

Release files for mgram-sdk 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mgram-sdk 1.0.1
File Size Uploaded Provenance Actions
mgram_sdk-1.0.1.tar.gz 256.8 kB Without provenance Details for mgram_sdk-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mgram-sdk 1.0.1
File Interpreter ABI Platform Actions
mgram_sdk-1.0.1-py3-none-any.whl Python 3 none any Details for mgram_sdk-1.0.1-py3-none-any.whl

Total release size: 271.3 kB

Release files / mgram_sdk-1.0.1.tar.gz

Download URL mgram_sdk-1.0.1.tar.gz
Size 256.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2c2d1ab62cd4af227ea7e13224c831bbd0e9c798394bfbc7ecb477b8d4a1bf5a
BLAKE2b-256 checksum
How to use checksums
743c476e70ecd9663313a0feea82cc27d732e1ab539ed9456c759bdc0a1d9a7d
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.1-py3-none-any.whl

Download URL mgram_sdk-1.0.1-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
434ced42d7ebef41e43b7bf4ed33b0cbb9e8590ded9ad84d6798a2565a6354c8
BLAKE2b-256 checksum
How to use checksums
a80dfdda0b5fb84b3de47bc78130899dfc917417f56c271a347eb68249d9b038
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.1

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.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page