Demo project

Subscription bot for a private channel: Telegram Stars and automatic removal

A Telegram bot that sells access to a private channel: plans, payment in Stars, a one-time invite link, reminders before expiry, renewal from the current end date and removal from the channel when time runs out.

backend and bot · Go · PostgreSQL · Telegram Bot API · Telegram Stars · Docker

1 stardemo plan, the star is refunded automatically
3 days and 1 dayreminders with a renew button
1 personcan join through one invite link
149automated tests, including end-to-end ones on a real database
Demo first screen: Subscription bot for a private channel: Telegram Stars and automatic removal

The problem

The author of a private channel sells access by subscription and does everything by hand: takes the payment, sends the link, remembers whose access ends when, writes reminders and removes those who did not renew. Links get passed around, renewals are forgotten, and after a couple of months nobody knows who in the channel is actually paying.

The company is fictional: the private channel “Volokno. Club” of the Volokno woodworking shop, with drawings, video breakdowns and answers from the master. The bot says plainly that it is a demo. The bot speaks Russian, so the screenshots are in Russian.

The solution

  • Plans live in a config file: 1 month, 3 months and a 15-minute demo. Price, length and the demo limit change without touching the code, and the bot refuses to start on a broken config.
  • Payment in Telegram Stars right in the chat, with no contract with a payment provider. Before the charge the bot checks the plan, the payer, the price and the demo rules, so a stale invoice or a changed price will not go through.
  • After payment the buyer gets a personal invite link: only one person can join with it, it lives for a day but never longer than the subscription. Someone already in the channel gets no link, only a longer term.
  • 3 days and 1 day before the end the bot sends a reminder with a renew button. Renewal is added to the current end date: renewing early loses nothing.
  • When time is up, the bot removes the person from the channel and revokes their unused links. The removal is soft: after paying again they come back through a new link.
  • The admin sees a summary right in the bot: active subscriptions, how many end this week, revenue for today, the week and the month, a breakdown by plan and the latest payments. Access can be granted by hand, and for showing the bot to a client there are commands that end a subscription or move its end date at once.

No money gets lost

  • A repeated payment notification does not extend the subscription twice: the idempotency key is the Telegram charge id, enforced by the table’s primary key rather than a check in code.
  • If a payment cannot be matched to a plan or written to the database, the stars are refunded at once. The demo is always refunded, even if the invite link could not be created.
  • If a refund fails or the bot restarts between payment and refund, the scheduler finishes the refund itself, up to five attempts, and then calls the admin.

Details that are easy to miss

  • A forwarded link is not a pass: if someone without a subscription joins through a subscriber’s link, the bot removes them and offers the subscriber a new link.
  • Payment, manual grants and removal for one person run strictly one after another, and the subscription is re-read before the ban: someone who renewed in the last second does not get kicked out.
  • A reminder for one term never arrives twice, and after a renewal the reminders for the new term come again. Someone who blocked the bot is not retried every minute.
  • On start the bot checks its own rights in the channel and tells the admin if it cannot invite or ban. The bot cannot remove the channel owner or an admin, and that also goes to the admin instead of looping in endless retries.
  • The bot token never reaches the logs: the logger strips it from any line, and a dedicated test fails every request to Telegram and checks that the token did not leak.
  • The bot and the database come up with one Docker command, and the bot works on long polling: no public address and no open ports are needed.
  • 149 automated tests run payment, refunds, reminders and removal on a fake clock without real waiting: a purchase, reminders on day 27 and day 29, renewal on day 29.5, removal after expiry and a replayed old payment are checked in a second on a real database.

Checked in Telegram

The whole path was run in real Telegram, and the gallery comes from that run: buying the demo plan for one star, the personal link and joining the private channel, the star refund, the reminders 3 days and 1 day ahead, removal on expiry and the admin summary. The reminders and the removal were triggered by admin commands that move the end date instead of waiting several weeks: the messages are the same ones the schedule sends. The invite link and the buyer’s details are blurred in the screenshots.

There is no ruble payment through YooKassa in this demo: it needs a contract with the provider, and the access logic does not depend on how the payment is made.