How to monetize a Telegram bot with a force-subscribe gate and Reply Ads (aiogram 3, grammY, Telegraf)

2026-12-05来源 dev.toEnglish

Most Telegram bots stay free because charging users for a utility bot rarely works. The usual way to make a bot pay for itself is advertising inside the bot itself. Two formats are common:

  1. A force-subscribe gate (sponsor tasks). Before the bot answers, the user is asked to join a sponsor's channel or start a sponsor's bot. Telegram confirms the action, and the bot owner gets paid for each confirmed subscription or start.
  2. Reply Ads. After the bot gives a useful answer, it shows one short ad. The owner is paid per delivered impression.

This post shows how to add both to an existing bot with MGRAM, an ad network inside Telegram, using the open-source SDKs for Python and JavaScript. The owner gets 70% of every placement, and the gate works from the first user.

How the flow looks

Step The user sees API call
1 Writes to your bot —
2 Sponsor buttons and a "Check" button POST /tasks
3 Taps "Check", access opens POST /tasks/check
4 Your bot's normal answer —
5 A short ad from your bot POST /reply-ads/show

If MGRAM is unreachable or has no tasks for this user, the API answers skip: true and the user goes straight through. The SDKs treat every network or API error the same way, so ads can never break your bot.

Get a key

  1. Open @mgram in Telegram and connect your bot by sending its token from @botfather (the message is deleted right away).
  2. In @mgram open bot stats → Settings → API access and copy the key. Keep it on the server only.

Python: aiogram 3

pip install "mgram-sdk[aiogram]" 
import asyncio import os from aiogram import Bot, Dispatcher from aiogram.types import Message from mgram_sdk import AsyncMgram from mgram_sdk.aiogram import MgramGate bot = Bot(os.environ["BOT_TOKEN"]) dp = Dispatcher() gate = MgramGate(AsyncMgram(os.environ["MGRAM_API_KEY"])) gate.setup(dp) # sponsor tasks before your handlers + the "Check" button @dp.message() async def handle(message: Message): await message.answer(f"Done: {message.text}") await gate.show_ad(message) # a short ad after the useful answer asyncio.run(dp.start_polling(bot)) 

gate.setup(dp) registers an outer middleware. Until the user completes the tasks, your handlers are not called and the bot answers with the task window. A user who has passed is not checked again for ten minutes (skip_ttl), and /help is never gated (skip_commands). For python-telegram-bot 20+ install mgram-sdk[ptb].

JavaScript: grammY or Telegraf

npm i mgram-sdk 
import { Bot } from "grammy"; import { Mgram, mgramGate } from "mgram-sdk"; const bot = new Bot(process.env.BOT_TOKEN); const gate = mgramGate(new Mgram(process.env.MGRAM_API_KEY)); bot.use(gate.middleware); // sponsor tasks before your handlers + the "Check" button bot.on("message:text", async (ctx) => { await ctx.reply("Here is your result"); await gate.showAd(ctx); // a short ad after the useful answer }); bot.start(); 

The same middleware works with Telegraf: bot.use(gate.middleware). The package has no dependencies and ships TypeScript types. It runs on Node 18+, Bun and Deno.

Any framework, or a Mini App

Under the hood it is plain JSON over HTTPS with a Bearer key:

curl -X POST https://api.mgram.biz/tasks \ -H "Authorization: Bearer $MGRAM_API_KEY" \ -H "Content-Type: application/json" \ -d '{"user_id": 123456789, "language_code": "en"}' 

The answer has skip and a list of tasks with button_text and link. Show them as URL buttons, then call POST /tasks/check when the user taps "Check". In a Mini App, send Telegram.WebApp.initData from the page to your backend and pass it as init_data; MGRAM verifies the Telegram signature. The full reference is at api.mgram.biz, and there is an OpenAPI 3.1 description.

Webhooks instead of polling

To know when a task is completed or refunded (for example, the user left the channel), turn on a webhook once:

from mgram_sdk import Mgram print(Mgram(KEY).set_webhook("https://example.com/mgram/tasks")) # the answer has the secret 

Each request carries X-MGRAM-Timestamp and X-MGRAM-Signature (HMAC-SHA256 of timestamp + "." + body). Both SDKs have verify_signature / verifySignature and a parser for the event.

What you earn

Format From bot size Paid to you
Sponsor tasks the first user per subscription or bot start that Telegram confirms
Reply Ads 50 users per 1,000 delivered impressions

Farms and repeated actions are never paid. Payouts go in USDT to CryptoBot or a TON wallet. Current rates are on the pricing page.

Things to keep in mind

  • Show Reply Ads after a useful answer, not on payment or sign-up screens.
  • Keep the API key server-side. A Mini App should call your backend, never MGRAM directly.
  • Treat the gate as optional for important commands: add them to skip_commands.

Links

Questions: support@mgram.biz or @mgram in Telegram.

原文:https://www.a8q9.cn/en/article/how-to-monetize-a-telegram-bot-with-a-force-subscribe-gate-and-reply-ads-aiogram-7acc2c.html

相关文章