Untitled

 avatar
unknown
plain_text
2 years ago
11 kB
5
Indexable

import telebot
from telebot import types

# Replace with your own bot token
bot = telebot.TeleBot("6014047695:AAHvjt6h-hzLBT0WzEHBrOIMXo2T3LG8sDs")

# A dictionary to store the virtual money of each user
money = {}

# A function to check if a user is an admin
def is_admin(user_id):
    # Replace with your own admin id
    return user_id == 2115205796

# A function to format the money amount
def format_money(amount):
    return f"{amount:.2f} V$"

# A handler for the /start command
@bot.message_handler(commands=["start"])
def start(message):
    # Get the user id and name
    user_id = message.from_user.id
    user_name = message.from_user.first_name

    # Initialize the money for the user if not already done
    if user_id not in money:
        money[user_id] = 0

    # Send a welcome message and show the keyboard
    bot.send_message(user_id, f"Hello, {user_name}! Welcome to the virtual money bot.\nYou have {format_money(money[user_id])} in your account.", reply_markup=keyboard())

# A function to create a keyboard with buttons
def keyboard():
    # Create a new keyboard
    kb = types.ReplyKeyboardMarkup(resize_keyboard=True)

    # Add buttons for sending and receiving money
    kb.add(types.KeyboardButton("Send money"), types.KeyboardButton("Receive money"))

    # Add a button for checking balance
    kb.add(types.KeyboardButton("Check balance"))

    # Return the keyboard
    return kb

# A handler for text messages
@bot.message_handler(content_types=["text"])
def text(message):
    # Get the user id and name
    user_id = message.from_user.id
    user_name = message.from_user.first_name

    # Get the text of the message
    text = message.text

    # If the user wants to send money, ask for the receiver and the amount
    if text == "Send money":
        bot.send_message(user_id, "Who do you want to send money to? Please enter their user id or username.")
        bot.register_next_step_handler(message, send_money)

    # If the user wants to receive money, ask for the sender and the amount
    elif text == "Receive money":
        bot.send_message(user_id, "Who do you want to receive money from? Please enter their user id or username.")
        bot.register_next_step_handler(message, receive_money)

    # If the user wants to check balance, show their current money
    elif text == "Check balance":
        bot.send_message(user_id, f"You have {format_money(money[user_id])} in your account.")

    # If the user is an admin, they can also give money to any user
    elif text == "Give money" and is_admin(user_id):
        bot.send_message(user_id, "Who do you want to give money to? Please enter their user id or username.")
        bot.register_next_step_handler(message, give_money)

    # Otherwise, send an invalid message
    else:
        bot.send_message(user_id, "Invalid message. Please use one of the buttons below.", reply_markup=keyboard())

# A function to handle sending money
def send_money(message):
    # Get the sender id and name
    sender_id = message.from_user.id
    sender_name = message.from_user.first_name

    # Get the receiver id or username from the message
    receiver = message.text

    # Try to find the receiver id from the username if needed
    try:
        receiver_id = int(receiver)
    except ValueError:
        receiver_id = bot.get_chat_member(receiver).user.id

    # Check if the receiver is valid and not the same as the sender
    if receiver_id in money and receiver_id != sender_id:
        # Ask for the amount to send
        bot.send_message(sender_id, f"How much do you want to send to {receiver}? Please enter a positive number.")
        bot.register_next_step_handler(message, lambda m: confirm_send_money(m, receiver_id))
    
    else:
        # Send an error message and go back to the main menu
        bot.send_message(sender_id, "Invalid receiver. Please try again.", reply_markup=keyboard())

# A function to handle confirming sending money
def confirm_send_money(message, receiver_id):
    # Get the sender id and name
    sender_id = message.from_user.id
    sender_name = message.from_user.first_name

    # Get the amount from the message
    amount = message.text

    # Try to convert the amount to a float and check if it is positive and less than or equal to the sender's balance
    try:
        amount = float(amount)
        if amount > 0 and amount <= money[sender_id]:
            # Deduct the amount from the sender's balance and add it to the receiver's balance
            money[sender_id] -= amount
            money[receiver_id] += amount

            # Send a confirmation message to both parties and go back to the main menu
            bot.send_message(sender_id, f"You have successfully sent {format_money(amount)} to {receiver_id}.\nYour new balance is {format_money(money[sender_id])}.", reply_markup=keyboard())
            bot.send_message(receiver_id, f"You have received {format_money(amount)} from {sender_id}.\nYour new balance is {format_money(money[receiver_id])}.", reply_markup=keyboard())
        
        else:
            # Send an error message and go back to the main menu
            bot.send_message(sender_id, "Invalid amount. Please try again.", reply_markup=keyboard())
    
    except ValueError:
        # Send an error message and go back to the main menu
        bot.send_message(sender_id, "Invalid amount. Please try again.", reply_markup=keyboard())

# A function to handle receiving money
def receive_money(message):
     # Get the receiver id and name 
     receiver_id = message.from_user.id 
     receiver_name = message.from_user.first_name 

     # Get the sender id or username from the message 
     sender = message.text 

     # Try to find the sender id from the username if needed 
     try: 
         sender_id = int(sender) 
     except ValueError: 
         sender_id = bot.get_chat_member(sender).user.id 

     # Check if the sender is valid and not the same as the receiver 
     if sender_id in money and sender_id != receiver_id: 
         # Ask for the amount to receive 
         bot.send_message(receiver_id, f"How much do you want to receive from {sender}? Please enter a positive number.") 
         bot.register_next_step_handler(message, lambda m: confirm_receive_money(m, sender_id)) 
    
     else: 
         # Send an error message and go back to the main menu 
         bot.send_message(receiver_id, "Invalid sender. Please try again.", reply_markup=keyboard()) 

# A function to handle confirming receiving money 
def confirm_receive_money(message, sender_id): 
     # Get the receiver id and name 
     receiver_id = message.from_user.id 
     receiver_name = message.from_user.first_name 

     # Get the amount from the message 
     amount = message.text 

     # Try to convert the amount to a float and check if it is positive and less than or equal to the sender's balance 
     try: 
         amount = float(amount) 
         if amount > 0 and amount <= money[sender_id]: 
             # Deduct the amount from the sender's balance and add it to the receiver's balance 
             money[sender_id] -= amount 
             money[receiver_id] += amount 

             # Send a confirmation message to both parties and go back to the main menu 
             bot.send_message(receiver_id, f"You have successfully received {format_money(amount)} from {sender_id}.\nYour new balance is {format_money(money[receiver_id])}.", reply_markup=keyboard()) 
             bot.send_message(sender_id, f"You have sent {format_money(amount)} to {receiver_id}.\nYour new balance is {format_money(money[sender_id])}.", reply_markup=keyboard()) 
        
         else: 
             # Send an error message and go back to the main menu 
             bot.send_message(receiver_id, "Invalid amount. Please try again.", reply_markup=keyboard()) 
    
     except ValueError: 
         # Send an error message and go back to the main menu 
         bot.send_message(receiver_id, "Invalid amount. Please try again.", reply_markup=keyboard()) 

# A function to handle giving money (only for admin)
def give_money(message):
     # Get the admin id and name 
     admin_id = message.from_user.id 
     admin_name = message.from_user.first_name 

     # Get the recipient id or username from the message 
     recipient = message.text 

     # Try to find the recipient id from the username if needed 
     try: 
         recipient_id = int(recipient) 
     except ValueError: 
         recipient_id = bot.get_chat_member(recipient).user.id 

     # Check if the recipient is valid  
     if recipient in money:  
         # Ask for how much you want give them  
         bot.send_message(adminid ,f"How much do you want give them?")  
         bot.register_next_step_handler(message ,lambda m :confirm_give_money(m ,recipientid))  
     
      else :
# A function to handle giving money (only for admin)
def give_money(message):
     # Get the admin id and name 
     admin_id = message.from_user.id 
     admin_name = message.from_user.first_name 

     # Get the recipient id or username from the message 
     recipient = message.text 

     # Try to find the recipient id from the username if needed 
     try: 
         recipient_id = int(recipient) 
     except ValueError: 
         recipient_id = bot.get_chat_member(recipient).user.id 

     # Check if the recipient is valid  
     if recipient_id in money:  
         # Ask for how much you want to give them  
         bot.send_message(admin_id ,f"How much do you want to give to {recipient}? Please enter a positive number.")  
         bot.register_next_step_handler(message ,lambda m :confirm_give_money(m ,recipient_id))  
     
     else: 
         # Send an error message and go back to the main menu 
         bot.send_message(admin_id, "Invalid recipient. Please try again.", reply_markup=keyboard()) 

# A function to handle confirming giving money (only for admin)
def confirm_give_money(message, recipient_id):
     # Get the admin id and name 
     admin_id = message.from_user.id 
     admin_name = message.from_user.first_name 

     # Get the amount from the message 
     amount = message.text 

     # Try to convert the amount to a float and check if it is positive
     try: 
         amount = float(amount) 
         if amount > 0: 
             # Add the amount to the recipient's balance
             money[recipient_id] += amount 

             # Send a confirmation message to both parties and go back to the main menu 
             bot.send_message(admin_id, f"You have successfully given {format_money(amount)} to {recipient_id}.\nTheir new balance is {format_money(money[recipient_id])}.", reply_markup=keyboard()) 
             bot.send_message(recipient_id, f"You have received {format_money(amount)} from {admin_id}.\nYour new balance is {format_money(money[recipient_id])}.", reply_markup=keyboard()) 
        
         else: 
             # Send an error message and go back to the main menu 
             bot.send_message(admin_id, "Invalid amount. Please try again.", reply_markup=keyboard()) 
    
     except ValueError: 
         # Send an error message and go back to the main menu 
         bot.send_message(admin_id, "Invalid amount. Please try again.", reply_markup=keyboard()) 

# Start polling for updates
bot.polling()
Editor is loading...