Skip to main content
The Telegram integration enables Stars payments (in-app purchases) and channel membership management for JOIP users.

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:
  1. Send /newbot
  2. Choose a display name (e.g., “JOIP Credits Bot”)
  3. Choose a username (must end in bot, e.g., JoipCreditsBot)
  4. Copy the bot token provided
Keep your bot token secret. Anyone with this token can control your bot.
2

Request Stars Payments Access

Telegram Stars requires approval from Telegram:
  1. Message @BotSupport
  2. Request Stars payment access
  3. Provide:
    • Bot username
    • Description of your service
    • Business details (if required)
  4. 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:
  1. Create a new channel in Telegram
  2. Make it public or private
  3. Add your bot as an administrator with:
    • Post messages permission
    • Invite users via link permission
  4. 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 .env:
Generate webhook secret:
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:
  1. Send /start to your bot
  2. Should receive welcome message
  3. Try /terms and /support commands
  4. 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:
You must respond to pre-checkout queries within 10 seconds or the payment fails.
5

Successful Payment

Telegram charges user and sends successful_payment webhook:

Channel Management

Automatic channel membership management:

Message Customization

Admins can customize bot messages:
Available message keys:
  • bot_welcome - Initial /start message
  • payment_success - Payment completed
  • payment_failed - Payment verification failed
  • session_ready - Payment session created
  • terms_info - /terms command response
  • support_info - /support command response
  • channel_invite - Channel invitation message
  • bonus_granted - Channel join bonus

API Reference

Core Functions

Type Definitions

Common Operations

Send Custom Message

Process Refund

Bulk Channel Invites

Error Handling

Common Issues

Cause: Webhook URL not accessible or secret mismatchSolution:
  1. Verify webhook URL is publicly accessible (HTTPS required)
  2. Check webhook secret matches TELEGRAM_WEBHOOK_SECRET
  3. Use getWebhookInfo() to see last error
  4. Test webhook: send /start to bot and check server logs
  5. Ensure server is deployed (webhooks don’t work on localhost)
Cause: Bot doesn’t have Stars payment permissionSolution:
  • Check bot is approved by @BotSupport
  • Verify provider_token is empty string (required for Stars)
  • Check currency is “XTR” (Telegram Stars currency code)
  • Review error in Telegram bot chat
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
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 .env only
  • 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:
Production should always use webhooks. Polling is inefficient and may hit rate limits.

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
Example pricing:
  • 10 Stars → User pays ~0.10→Youreceive 0.10 → You receive ~0.05
  • 100 Stars → User pays ~1.00→Youreceive 1.00 → You receive ~0.50
  • 1000 Stars → User pays ~10.00→Youreceive 10.00 → You receive ~5.00
Telegram takes ~50% fee. Factor this into your pricing strategy.