When developing within the Telegram ecosystem, many developers face a common pain point: how to manage both Bot and Mini App code simultaneously? The traditional approach involves deploying two separate projects, which not only increases maintenance costs but also easily leads to issues in data synchronization and payment processes.
This article will showcase a brand-new development paradigm: using a single Next.js project to implement both a Telegram Bot and a Mini App, truly achieving one codebase, one deployment, seamless integration. You'll learn how to build a complete points-based game system, including daily check-in rewards, real Telegram Stars payments, and real-time notification features.
In the traditional model, developers need to:
By using the Next.js all-in-one solution, you can:
This approach is particularly suitable for rapidly validating ideas, developing MVP products, or for independent developers looking to launch projects quickly.
We will build a points-based game system with the following features:
/start and /help, allowing users to launch the Mini App with a single click.The entire system runs within a single Next.js application, ensuring seamless interaction between the Bot and the Mini App.
Before you begin the development, you'll need to prepare the following tools:
ngrok config add-authtoken <your_token> to bind your account.ngrok http 3000 from that folder at any time.You can obtain the complete source code and detailed documentation from the relevant Telegram channel (specific links can be found in the original video description).
git clone <project_repository_url>
cd <project_directory>
npm install
Create a .env.local file in the project's root directory to store sensitive information:
TELEGRAM_BOT_TOKEN=your_bot_token
NEXT_PUBLIC_APP_URL=your_ngrok_url
Important Note: Ensure .env.local is added to .gitignore to prevent Bot Token leakage.
/newbot to create a new Bot.bot)..env.local.# Terminal 1: Start Ngrok
ngrok http 3000
# Terminal 2: Start Next.js development server
npm run dev
# Terminal 3: Set up Webhook
npm run webhook:setup
If everything goes smoothly, you will see the "Webhook set successfully" message.
/mybots, and select your Bot.Now, when users open your Bot, they will see a button to launch the game, which will open the Mini App interface upon clicking!
All updates sent by Telegram (user commands, payment callbacks, etc.) are passed through the Webhook to the /api/telegram/webhook route.
The main message types handled include:
/start, /help, etc.When a user first sends /start, the Bot will immediately push a "Start Game" button, requiring no further guidance.
The core of the payment functionality lies in the /api/buy-points route:
createInvoiceLink).Key Configuration:
provider_token blank (Telegram Stars mode).currency to XTR.prices are in units of Stars (not cents).Developer Benefit: After each purchase, a receipt ID is returned. You can immediately test refunds using /refund <receipt_id> without waiting for approval or manual intervention. This is extremely efficient for testing the real payment process.
In the /api/claim-daily route, we've implemented a time-based incentive mechanism:
The frontend uses a real-time countdown component that updates every second, clearly showing users when they can claim again. This model can be extended to any game mechanic requiring time limits (e.g., energy regeneration, lottery cooldowns, etc.).
Although this article uses in-memory storage for demonstration, real projects must be modified before going live:
Recommended deployment platforms:
Remember to update the Webhook URL and Mini App Menu Link to your production domain when deploying.
Telegram's Webhooks and Mini Apps both require the HTTPS protocol, which localhost cannot satisfy. Ngrok creates a publicly accessible HTTPS tunnel that forwards requests to your local development server.
During the development phase, you can purchase a small amount of Telegram Stars (as little as a few cents) and then use the /refund command to get an instant refund. This allows you to test the payment, webhook callbacks, and refund logic using the real process, without needing to repeatedly top up.
Absolutely not. In-memory storage loses all data upon server restart. You must use a persistent database (like PostgreSQL) and have backup and migration plans in place.
The biggest advantages are reduced development efficiency and maintenance costs. You won't need to:
All logic resides within a single project, leading to higher code reusability and faster bug identification.
It's perfectly adequate for MVP products and small to medium-sized applications. If your daily active users reach millions, it's recommended to split the Bot backend and Mini App frontend, using a microservices architecture and dedicated message queues to handle high concurrency.
Through the practical demonstration in this article, you've mastered the core skills for building an integrated Telegram Bot + Mini App application using Next.js. This development approach can help you quickly validate ideas, reduce development costs, and lay a solid foundation for future expansion. Start experimenting today and build your own Telegram mini-game in the shortest amount of time!
Outline
_00000.png)

