Skip to content

AsterPay/telegram-bot-payments

Repository files navigation

Telegram Bot with AsterPay Payments

A complete template for monetizing your Telegram bot with crypto payments via AsterPay.

Features

  • 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

Quick Start

1. Create a Telegram Bot

  1. Open Telegram and search for @BotFather
  2. Send /newbot and follow the prompts
  3. Copy the bot token

2. Clone and Install

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.txt

3. Configure Environment

cp .env.example .env

Edit .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-key

4. Run the Bot

python main.py

5. Run Webhook Server (for payments)

In a separate terminal:

uvicorn webhook_server:app --reload --port 8000

Bot Commands

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)

How It Works

1. User Starts Bot

User sends /start, gets 5 free credits.

2. User Generates Content

/generate Write a haiku about programming

Costs 1 credit per generation.

3. User Runs Out of Credits

Bot prompts to buy more with /buy.

4. User Purchases Credits

  • Selects credit package (10, 50, or 100 credits)
  • Gets payment link via AsterPay
  • Pays with USDC/USDT

5. Webhook Delivers Credits

AsterPay sends webhook → credits added automatically.

Project Structure

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

Pricing Structure

Credits Price Per Generation
10 $1 $0.10
50 $4 $0.08
100 $7 $0.07

Production Deployment

Deploy Bot

Option 1: VPS (DigitalOcean, Linode, etc.)

# Install dependencies
pip install -r requirements.txt

# Run with systemd or supervisor
python main.py

Option 2: Railway

railway login
railway init
railway up

Deploy Webhook Server

The webhook server needs a public URL. Options:

  1. Same VPS - Run on port 8000, use nginx reverse proxy
  2. Render - Deploy as web service
  3. Railway - Deploy alongside bot

Configure Webhook URL

In your AsterPay dashboard, set webhook URL:

https://your-domain.com/webhook/asterpay

Customization

Add More Commands

# 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))

Change Pricing

Edit in handlers.py:

pricing = {
    10: "1.00",   # 10 credits = $1
    50: "4.00",   # 50 credits = $4
    100: "7.00",  # 100 credits = $7
}

Use Real Database

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)

Testing

Test Bot Commands

  1. Start your bot
  2. Open Telegram, find your bot
  3. Send /start
  4. Try /generate Hello world

Test Payments (Demo Mode)

Without API keys, the bot runs in demo mode:

  • Payment buttons show demo links
  • AI generation returns placeholder text

Test Webhooks Locally

Use ngrok to expose local webhook server:

ngrok http 8000
# Use the ngrok URL in AsterPay dashboard

Support

License

MIT

About

Telegram bot with AsterPay crypto payments

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages