A complete template for monetizing your Telegram bot with crypto payments via AsterPay.
- Telegram Bot - Full bot with commands and inline buttons
- Credit System - Per-use billing for AI generation
- AsterPay Integration - Accept USDC/USDT payments
- Webhook Handling - Automatic credit delivery
- OpenAI Integration - AI text generation
- Open Telegram and search for @BotFather
- Send
/newbotand follow the prompts - Copy the bot token
git clone https://github.com/AsterPay/telegram-bot-payments.git
cd telegram-bot-payments
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txtcp .env.example .envEdit .env:
# Required
TELEGRAM_BOT_TOKEN=123456789:ABCdefGHIjklMNOpqrSTUvwxYZ
# AsterPay (for payments)
ASTERPAY_API_KEY=sk_live_your_api_key
ASTERPAY_WEBHOOK_SECRET=whsec_your_webhook_secret
# OpenAI (for AI generation)
OPENAI_API_KEY=sk-your-openai-keypython main.pyIn a separate terminal:
uvicorn webhook_server:app --reload --port 8000| Command | Description |
|---|---|
/start |
Welcome message and introduction |
/help |
Show available commands |
/credits |
Check your credit balance |
/buy |
Purchase credits with crypto |
/generate <prompt> |
Generate AI content (1 credit) |
User sends /start, gets 5 free credits.
/generate Write a haiku about programming
Costs 1 credit per generation.
Bot prompts to buy more with /buy.
- Selects credit package (10, 50, or 100 credits)
- Gets payment link via AsterPay
- Pays with USDC/USDT
AsterPay sends webhook → credits added automatically.
telegram-bot-payments/
├── main.py # Bot entry point
├── handlers.py # Command handlers
├── database.py # User/credits storage
├── payments.py # AsterPay integration
├── ai_service.py # AI text generation
├── webhook_server.py # Payment webhook receiver
├── requirements.txt # Python dependencies
├── .env.example # Environment template
└── README.md
| Credits | Price | Per Generation |
|---|---|---|
| 10 | $1 | $0.10 |
| 50 | $4 | $0.08 |
| 100 | $7 | $0.07 |
# Install dependencies
pip install -r requirements.txt
# Run with systemd or supervisor
python main.pyrailway login
railway init
railway upThe webhook server needs a public URL. Options:
- Same VPS - Run on port 8000, use nginx reverse proxy
- Render - Deploy as web service
- Railway - Deploy alongside bot
In your AsterPay dashboard, set webhook URL:
https://your-domain.com/webhook/asterpay
# In handlers.py
async def custom_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle /custom command."""
# Your logic here
await update.message.reply_text("Custom response!")
# In main.py
application.add_handler(CommandHandler("custom", custom_command))Edit in handlers.py:
pricing = {
10: "1.00", # 10 credits = $1
50: "4.00", # 50 credits = $4
100: "7.00", # 100 credits = $7
}Replace database.py with PostgreSQL, MongoDB, or Redis:
# Example with Redis
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
def get_user_credits(user_id: int) -> int:
return int(r.get(f"credits:{user_id}") or 0)
def add_credits(user_id: int, amount: int) -> int:
return r.incrby(f"credits:{user_id}", amount)- Start your bot
- Open Telegram, find your bot
- Send
/start - Try
/generate Hello world
Without API keys, the bot runs in demo mode:
- Payment buttons show demo links
- AI generation returns placeholder text
Use ngrok to expose local webhook server:
ngrok http 8000
# Use the ngrok URL in AsterPay dashboard- Documentation: asterpay.io/docs
- Email: support@asterpay.io
- Telegram: t.me/asterpaycommunity
MIT