πŸš€ How to Create a Successful Telegram Bot for Digital Commerce (DCommerce Bot)

πŸš€ How to Create a Successful Telegram Bot for Digital Commerce (DCommerce Bot)

πŸš€ How to Create a Successful Telegram Bot for Digital Commerce (DCommerce Bot)

βœ… Sell ZIP, PDF, Image, and Other Digital Files via PayPal & Apirone

πŸ“Œ Business-Focused & Simple Guide


🟒 0. Define the Business Model

πŸ“Œ What will this bot do?

  • Sell digital files (ZIP, PDF, Images, Audio, Video, etc.)
  • Accept payments via PayPal & Apirone (Crypto)
  • Automatically deliver download links after successful payment
  • Store all transactions in SQLite3 using the Aiogram framework

🟒 1. Setting Up the Development Environment

πŸ“Œ Required Technologies

βœ… Python (3.8+)
βœ… Aiogram (Telegram bot framework)
βœ… SQLite3 (Database)
βœ… PayPal & Apirone APIs (for payments)

πŸ“Œ Install Required Packages

pip install aiogram sqlite3 requests paypalrestsdk

🟒 2. Creating a Telegram Bot via BotFather

  1. Open Telegram and search for BotFather
  2. Type:
    /newbot
    
  3. Set the bot name β†’ DCommerceBot
  4. Set the username β†’ dcommerce_bot
  5. Copy the Token provided – you’ll need it for coding

🟒 3. Creating the Bot Structure (Boilerplate Code)

from aiogram import Bot, Dispatcher, types
from aiogram.utils import executor

TOKEN = "YOUR_BOT_TOKEN"
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)

@dp.message_handler(commands=['start'])
async def start_command(message: types.Message):
    await message.reply("Welcome to DCommerce Bot! πŸŽ‰ Send /buy to see available products.")

executor.start_polling(dp, skip_updates=True)

πŸ“Œ What happens?

  • Users get a welcome message upon starting the bot

🟒 4. Setting Up the Database (SQLite3)

πŸ“Œ Create the Database and Tables

import sqlite3

conn = sqlite3.connect("dcommerce.db")
cursor = conn.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS products (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    file_path TEXT NOT NULL,
    price REAL NOT NULL
)
""")

cursor.execute("""
CREATE TABLE IF NOT EXISTS orders (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    product_id INTEGER NOT NULL,
    payment_status TEXT NOT NULL,
    FOREIGN KEY(product_id) REFERENCES products(id)
)
""")

conn.commit()
conn.close()

πŸ“Œ What does this code do?

  1. The products table stores product names, file paths, and prices.
  2. The orders table stores user purchases and payment status.

🟒 5. Adding Products to the Database

def add_product(name, file_path, price):
    conn = sqlite3.connect("dcommerce.db")
    cursor = conn.cursor()
    cursor.execute("INSERT INTO products (name, file_path, price) VALUES (?, ?, ?)", (name, file_path, price))
    conn.commit()
    conn.close()

πŸ“Œ Example Usage:

add_product("Premium Guide PDF", "files/guide.pdf", 9.99)

Now, the bot stores digital products in the database.


🟒 6. Displaying Products to Users

@dp.message_handler(commands=['buy'])
async def show_products(message: types.Message):
    conn = sqlite3.connect("dcommerce.db")
    cursor = conn.cursor()
    cursor.execute("SELECT id, name, price FROM products")
    products = cursor.fetchall()
    conn.close()

    if not products:
        await message.reply("No products available at the moment.")
        return

    response = "Available Products:\n"
    for product in products:
        response += f"{product[0]}) {product[1]} - ${product[2]}\n"

    response += "\nSend /order <product_id> to buy."

    await message.reply(response)

πŸ“Œ What happens?

  • Users can see available products by typing /buy

🟒 7. Payment Integration (PayPal & Apirone)

πŸ“Œ PayPal Payment Integration

import paypalrestsdk

paypalrestsdk.configure({
    "mode": "sandbox",
    "client_id": "YOUR_CLIENT_ID",
    "client_secret": "YOUR_CLIENT_SECRET"
})

def create_payment(amount, return_url, cancel_url):
    payment = paypalrestsdk.Payment({
        "intent": "sale",
        "payer": {"payment_method": "paypal"},
        "redirect_urls": {"return_url": return_url, "cancel_url": cancel_url},
        "transactions": [{"amount": {"total": str(amount), "currency": "USD"}}]
    })
    
    if payment.create():
        return payment.links[1].href  # Redirect URL
    return None

πŸ“Œ What happens?

  • Users are redirected to PayPal for payment

πŸ“Œ Apirone (Crypto) Payment Integration

import requests

def create_crypto_payment(amount, crypto="BTC"):
    url = "https://apirone.com/api/v2/merchant/payment"
    payload = {
        "currency": crypto,
        "amount": amount,
        "callback_url": "https://yourserver.com/callback"
    }
    response = requests.post(url, json=payload)
    return response.json()["bitcoin"]["invoice"]

πŸ“Œ What happens?

  • Users receive a Bitcoin invoice for payment

🟒 8. Processing Orders & Delivering Files

@dp.message_handler(commands=['order'])
async def process_order(message: types.Message):
    product_id = int(message.text.split()[1])
    conn = sqlite3.connect("dcommerce.db")
    cursor = conn.cursor()
    cursor.execute("SELECT file_path, price FROM products WHERE id = ?", (product_id,))
    product = cursor.fetchone()
    conn.close()

    if product:
        payment_link = create_payment(product[1], "https://yourbot.com/success", "https://yourbot.com/cancel")
        await message.reply(f"Click to pay: {payment_link}")
    else:
        await message.reply("Product not found.")

πŸ“Œ What happens?

  • Users receive a payment link when they order a product.

🟒 9. Auto-Deliver Files After Payment (Webhooks)

  1. Set up a webhook URL to verify successful payments.
  2. Automatically send the file link to the user.
@dp.message_handler(commands=['deliver'])
async def deliver_product(message: types.Message):
    order_id = int(message.text.split()[1])
    conn = sqlite3.connect("dcommerce.db")
    cursor = conn.cursor()
    cursor.execute("SELECT file_path FROM products WHERE id = ?", (order_id,))
    product = cursor.fetchone()
    conn.close()

    if product:
        await message.reply_document(open(product[0], 'rb'))
    else:
        await message.reply("Order not found.")

πŸ“Œ What happens?

  • After successful payment, the bot automatically delivers the file

🟒 10. Final Thoughts

βœ… The bot successfully sells digital files
βœ… Supports PayPal & Crypto payments
βœ… Automatically delivers files after payment
βœ… Stores transactions securely using SQLite3

πŸš€ Now you can create a successful Telegram DCommerce Bot! πŸŽ‰