π 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
- Open Telegram and search for BotFather
- Type:
/newbot
- Set the bot name β DCommerceBot
- Set the username β dcommerce_bot
- 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?
- The products table stores product names, file paths, and prices.
- 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)
- Set up a webhook URL to verify successful payments.
- 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! π