|
| 1 | +# Import required libraries |
| 2 | +import telebot |
| 3 | +from telebot import types |
| 4 | + |
| 5 | +bot = telebot.TeleBot('TOKEN') |
| 6 | + |
| 7 | +# Product prices in stars |
| 8 | +PRICES = { |
| 9 | + '10 Stars': 10, # $1.00 - Entry level package |
| 10 | + '50 Stars': 45, # $4.50 - Medium package with 10% discount |
| 11 | + '100 Stars': 80, # $8.00 - Large package with 20% discount |
| 12 | + '500 Stars': 350 # $35.00 - Bulk package with 30% discount |
| 13 | +} |
| 14 | + |
| 15 | +# Handler for /start command - Then the bot will send amount stars to purchase |
| 16 | +@bot.message_handler(commands=['start']) |
| 17 | +def send_welcome(message): |
| 18 | + # Create a custom keyboard with 2 buttons per row |
| 19 | + markup = types.ReplyKeyboardMarkup(row_width=2) |
| 20 | + # Generate buttons for each product in our PRICES dictionary |
| 21 | + buttons = [types.KeyboardButton(product) for product in PRICES.keys()] |
| 22 | + # Add all buttons to the markup |
| 23 | + markup.add(*buttons) |
| 24 | + |
| 25 | + # Send welcome message with the custom keyboard |
| 26 | + bot.reply_to(message, |
| 27 | + "Welcome to Stars Payment Bot!\nPlease select amount of stars to purchase:", |
| 28 | + reply_markup=markup) |
| 29 | + |
| 30 | +# Handler for when user selects a product from the keyboard |
| 31 | +@bot.message_handler(func=lambda message: message.text in PRICES.keys()) |
| 32 | +def handle_product_selection(message): |
| 33 | + # Get selected product and its price |
| 34 | + product = message.text |
| 35 | + price = PRICES[product] |
| 36 | + |
| 37 | + # Create invoice with product details |
| 38 | + prices = [types.LabeledPrice(label=product, amount=price)] |
| 39 | + |
| 40 | + # Send payment invoice to user |
| 41 | + bot.send_invoice( |
| 42 | + message.chat.id, # Chat ID to send invoice to |
| 43 | + title=f"Purchase {product}", # Title of the invoice |
| 44 | + description=f"Buy {product} for your account", # Description shown on invoice |
| 45 | + provider_token='', # Payment provider token (empty for testing) |
| 46 | + currency='XTR', # Currency code |
| 47 | + prices=prices, # List of prices (we only have one item) |
| 48 | + start_parameter='stars-payment', # Deep-linking parameter |
| 49 | + invoice_payload=product # Payload to identify the product after payment |
| 50 | + ) |
| 51 | + |
| 52 | +# Pre-checkout handler - Called before payment is completed |
| 53 | +@bot.pre_checkout_query_handler(func=lambda query: True) |
| 54 | +def process_pre_checkout_query(pre_checkout_query): |
| 55 | + bot.answer_pre_checkout_query(pre_checkout_query.id, ok=True) |
| 56 | + |
| 57 | +# Handler for successful payments |
| 58 | +@bot.message_handler(content_types=['successful_payment']) |
| 59 | +def handle_successful_payment(message): |
| 60 | + # Get the product that was purchased from the invoice payload |
| 61 | + product = message.successful_payment.invoice_payload |
| 62 | + bot.reply_to(message, |
| 63 | + f"Payment for {product} successful!") |
| 64 | +bot.polling() |
0 commit comments