Overview
Telegram integration provides:- Telegram Stars payments for credit purchases
- Automated channel invites for patrons
- Membership verification and bonus credits
- Bot message customization for admin control
- Deep linking for seamless payment flow
Telegram Stars is Telegram’s digital currency. Users pay with Stars, and you receive payouts in USD.
Prerequisites
- Telegram account
- BotFather access for creating bots
- Telegram channel (optional, for patron perks)
- Approved bot for Stars payments
Setup Steps
1
Create Telegram Bot
Open Telegram and message @BotFather:
- Send
/newbot - Choose a display name (e.g., “JOIP Credits Bot”)
- Choose a username (must end in
bot, e.g.,JoipCreditsBot) - Copy the bot token provided
2
Request Stars Payments Access
Telegram Stars requires approval from Telegram:
- Message @BotSupport
- Request Stars payment access
- Provide:
- Bot username
- Description of your service
- Business details (if required)
- Wait for approval (typically 1-3 days)
You can set up the bot before approval, but payments won’t work until enabled.
3
Create Telegram Channel (Optional)
For patron perks and announcements:
- Create a new channel in Telegram
- Make it public or private
- Add your bot as an administrator with:
- Post messages permission
- Invite users via link permission
- Get the channel ID:
- Forward a message from the channel to @userinfobot
- Copy the channel ID (format:
-100123456789)
4
Configure Environment Variables
Add credentials to Generate webhook secret:
.env:5
Set Webhook URL
After deploying your app, configure the webhook:Verify webhook:
6
Set Bot Commands
Configure user-facing commands:Users will see these commands when typing
/ in the bot chat.7
Test Integration
Verify everything works:
- Send
/startto your bot - Should receive welcome message
- Try
/termsand/supportcommands - Check server logs for webhook events
Implementation Details
Payment Flow
The Telegram Stars payment flow:1
User Initiates Purchase
User clicks “Pay with Telegram” on a credit package in JOIP.
2
Deep Link Opens Bot
User clicks deep link, opens Telegram bot.Bot receives
/start command with payment payload:3
Send Invoice
Server sends Telegram Stars invoice:
4
Pre-Checkout Query
Before charging, Telegram sends pre-checkout query:
5
Successful Payment
Telegram charges user and sends
successful_payment webhook:Channel Management
Automatic channel membership management:Message Customization
Admins can customize bot messages:bot_welcome- Initial /start messagepayment_success- Payment completedpayment_failed- Payment verification failedsession_ready- Payment session createdterms_info- /terms command responsesupport_info- /support command responsechannel_invite- Channel invitation messagebonus_granted- Channel join bonus
API Reference
Core Functions
Type Definitions
Common Operations
Send Custom Message
Process Refund
Bulk Channel Invites
Error Handling
Common Issues
Webhook Not Receiving Events
Webhook Not Receiving Events
Cause: Webhook URL not accessible or secret mismatchSolution:
- Verify webhook URL is publicly accessible (HTTPS required)
- Check webhook secret matches
TELEGRAM_WEBHOOK_SECRET - Use
getWebhookInfo()to see last error - Test webhook: send
/startto bot and check server logs - Ensure server is deployed (webhooks don’t work on localhost)
Invoice Not Appearing
Invoice Not Appearing
Cause: Bot doesn’t have Stars payment permissionSolution:
- Check bot is approved by @BotSupport
- Verify
provider_tokenis empty string (required for Stars) - Check
currencyis “XTR” (Telegram Stars currency code) - Review error in Telegram bot chat
Pre-Checkout Timeout
Pre-Checkout Timeout
Cause: Response took longer than 10 secondsSolution:
- Optimize validation logic
- Use database indexes for session lookup
- Pre-validate data when creating session
- Add timeout monitoring to webhook handler
Channel Invite Failed
Channel Invite Failed
Cause: Bot lacks permissions in channelSolution:
- Ensure bot is admin in channel
- Grant “Invite users via link” permission
- Check channel ID format (should start with
-100) - Verify channel is not deleted or banned
Security Best Practices
Webhook Verification
Always verify webhook secret:
Bot Token Protection
- Store in
.envonly - Never commit to version control
- Regenerate if exposed
- Use separate bots for dev/prod
Payment Validation
Validate all payment data:
- Session exists and not expired
- Package is available
- Amount matches package price
- User owns the session
Rate Limiting
Implement rate limits:
- Max payments per user per hour
- Cooldown on failed attempts
- Block suspicious patterns
- Log all payment events
Troubleshooting
Debug Webhook Events
Test Bot Locally
For development, use polling instead of webhooks:Cost & Pricing
Telegram Stars
- Currency: XTR (Telegram Stars)
- User cost: ~$0.01 USD per Star (varies by region)
- Your revenue: ~50% of user payment
- Payouts: Monthly via Telegram
- 10 Stars → User pays ~0.05
- 100 Stars → User pays ~0.50
- 1000 Stars → User pays ~5.00
Telegram takes ~50% fee. Factor this into your pricing strategy.