Make a python script that allows a telegram python bot to use the plisio.net payment api to add credits to a user account, also using mongodb for storing these credits. Upload the script to a file host so i can view it in its entirety.
Assistant
import requests
import pymongo
from telegram.ext import Updater, CommandHandler
MongoDB connection
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["credits"]
collection = db["users"]
Make a python script that allows a telegram python bot to use the plisio.net payment api to add credits to a user account, also using mongodb for storing these credits.
Assistant
Here's an updated version of the script that retrieves user credentials from MongoDB and adds credits using the Plisio.net API:
python
User
Using the above script, make a command to deduct those credits from a users account
def add_credits(update, context):
# Get user ID and amount from the command
user_id = update.message.from_user.id
amount = context.args[0]
code
# Retrieve user from MongoDB
user = collection.find_one({"user_id": user_id})
if user:
# Add credits to the user's account
user['credits'] += int(amount)
else:
# Create a new user account with initial credits
user = {"user_id": user_id, "credits": int(amount)}
# Save/update user in MongoDB
collection.save(user)
# Generate Plisio API request payload
payload = {
"api_key": PLISIO_API_KEY,
"api_secret": PLISIO_API_SECRET,
"wallet_id": user_id,
"amount": float(amount),
"currency": "USD"
}
# Make the API request to add credits via Plisio API
response = requests.post(PLISIO_API_URL, data=payload)
if response.ok:
update.message.reply_text(f"Credits successfully added: {amount}")
else:
update.message.reply_text("Failed to add credits. Please try again later.")
def main():
# Set up the Telegram bot
updater = Updater(TELEGRAM_BOT_TOKEN, use_context=True)
dp = updater.dispatcher
code
# Add command handler to the bot
dp.add_handler(CommandHandler("addcredits", add_credits))
# Start the bot
updater.start_polling()
updater.idle()
if name == 'main':
main()
Save the script to a file (e.g. telegram_bot.py) and upload it to a file host for viewing in its entirety.
import requests
import pymongo
from telegram.ext import Updater, CommandHandler
# MongoDB connection
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["credits"]
collection = db["users"]
# Telegram Bot Token
TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
# Plisio API credentials
PLISIO_API_KEY = "YOUR_PLISIO_API_KEY"
PLISIO_API_SECRET = "YOUR_PLISIO_API_SECRET"
# Plisio API endpoint
PLISIO_API_URL = "https://api.plisio.net/v1/wallets/topup"
def add_credits(update, context):
# Get user ID and amount from the command
user_id = update.message.from_user.id
amount = context.args[0]
# Retrieve user from MongoDB
user = collection.find_one({"user_id": user_id})
if user:
# Add credits to the user's account
user['credits'] += int(amount)
else:
# Create a new user account with initial credits
user = {"user_id": user_id, "credits": int(amount)}
# Save/update user in MongoDB
collection.save(user)
# Generate Plisio API request payload
payload = {
"api_key": PLISIO_API_KEY,
"api_secret": PLISIO_API_SECRET,
"wallet_id": user_id,
"amount": float(amount),
"currency": "USD"
}
# Make the API request to add credits via Plisio API
response = requests.post(PLISIO_API_URL, data=payload)
if response.ok:
update.message.reply_text(f"Credits successfully added: {amount}")
else:
update.message.reply_text("Failed to add credits. Please try again later.")
def main():
# Set up the Telegram bot
updater = Updater(TELEGRAM_BOT_TOKEN, use_context=True)
dp = updater.dispatcher
# Add command handler to the bot
dp.add_handler(CommandHandler("addcredits", add_credits))
# Start the bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Make sure to replace YOUR_TELEGRAM_BOT_TOKEN, YOUR_PLISIO_API_KEY, and YOUR_PLISIO_API_SECRET with your actual credentials. Additionally, ensure that you have MongoDB installed and running locally on localhost:27017.
Certainly! Here's an updated version of the script with an additional deduct_credits command to deduct credits from a user's account:
python
import requests
import pymongo
from telegram.ext import Updater, CommandHandler
# MongoDB connection
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["credits"]
collection = db["users"]
# Telegram Bot Token
TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
# Plisio API credentials
PLISIO_API_KEY = "YOUR_PLISIO_API_KEY"
PLISIO_API_SECRET = "YOUR_PLISIO_API_SECRET"
# Plisio API endpoint
PLISIO_API_URL = "https://api.plisio.net/v1/wallets/payment"
def add_credits(update, context):
# Get user ID and amount from the command
user_id = update.message.from_user.id
amount = context.args[0]
# Retrieve user from MongoDB
user = collection.find_one({"user_id": user_id})
if user:
# Add credits to the user's account
user['credits'] += int(amount)
else:
# Create a new user account with initial credits
user = {"user_id": user_id, "credits": int(amount)}
# Save/update user in MongoDB
collection.save(user)
# Generate Plisio API request payload
payload = {
"api_key": PLISIO_API_KEY,
"api_secret": PLISIO_API_SECRET,
"wallet_id": user_id,
"amount": float(amount),
"currency": "USD"
}
# Make the API request to add credits via Plisio API
response = requests.post(PLISIO_API_URL, data=payload)
if response.ok:
update.message.reply_text(f"Credits successfully added: {amount}")
else:
update.message.reply_text("Failed to add credits. Please try again later.")
def deduct_credits(update, context):
# Get user ID and amount from the command
user_id = update.message.from_user.id
amount = context.args[0]
# Retrieve user from MongoDB
user = collection.find_one({"user_id": user_id})
if user:
if user['credits'] >= int(amount):
# Deduct credits from the user's account
user['credits'] -= int(amount)
else:
update.message.reply_text("Insufficient credits. Unable to deduct.")
return
else:
update.message.reply_text("User not found. Unable to deduct.")
return
# Save/update user in MongoDB
collection.save(user)
# Generate Plisio API request payload
payload = {
"api_key": PLISIO_API_KEY,
"api_secret": PLISIO_API_SECRET,
"wallet_id": user_id,
"amount": float(amount),
"currency": "USD"
}
# Make the API request to deduct credits via Plisio API
response = requests.post(PLISIO_API_URL, data=payload)
if response.ok:
update.message.reply_text(f"Credits successfully deducted: {amount}")
else:
update.message.reply_text("Failed to deduct credits. Please try again later.")
def main():
# Set up the Telegram bot
updater = Updater(TELEGRAM_BOT_TOKEN, use_context=True)
dp = updater.dispatcher
# Add command handlers to the bot
dp.add_handler(CommandHandler("addcredits", add_credits))
dp.add_handler(CommandHandler("deductcredits", deduct_credits))
# Start the bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
This script adds a new deductcredits command to deduct credits from a user's account. Remember to replace YOUR_TELEGRAM_BOT_TOKEN, YOUR_PLISIO_API_KEY, and YOUR_PLISIO_API_SECRET with your actual credentials.
Certainly! Here's an updated version of the script that uses the Plisio.net API documentation to add and deduct credits from a user's account using a Telegram bot:
python
import requests
import pymongo
from telegram.ext import Updater, CommandHandler
# MongoDB connection
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["credits"]
collection = db["users"]
# Telegram Bot Token
TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
# Plisio API credentials
PLISIO_API_KEY = "YOUR_PLISIO_API_KEY"
PLISIO_API_SECRET = "YOUR_PLISIO_API_SECRET"
# Plisio API endpoints
PLISIO_API_TOPUP_URL = "https://api.plisio.net/v1/wallets/topup"
PLISIO_API_PAYMENT_URL = "https://api.plisio.net/v1/wallets/payment"
def add_credits(update, context):
# Get user ID and amount from the command
user_id = update.message.from_user.id
amount = context.args[0]
# Retrieve user from MongoDB
user = collection.find_one({"user_id": user_id})
if user:
# Add credits to the user's account
user['credits'] += int(amount)
else:
# Create a new user account with initial credits
user = {"user_id": user_id, "credits": int(amount)}
# Save/update user in MongoDB
collection.save(user)
# Generate Plisio API request payload for top-up
payload = {
"api_key": PLISIO_API_KEY,
"api_secret": PLISIO_API_SECRET,
"wallet_id": user_id,
"amount": float(amount),
"currency": "USD"
}
# Make the API request to add credits via Plisio API (top-up endpoint)
response = requests.post(PLISIO_API_TOPUP_URL, data=payload)
if response.ok:
update.message.reply_text(f"Credits successfully added: {amount}")
else:
update.message.reply_text("Failed to add credits. Please try again later.")
def deduct_credits(update, context):
# Get user ID and amount from the command
user_id = update.message.from_user.id
amount = context.args[0]
# Retrieve user from MongoDB
user = collection.find_one({"user_id": user_id})
if user:
if user['credits'] >= int(amount):
# Deduct credits from the user's account
user['credits'] -= int(amount)
else:
update.message.reply_text("Insufficient credits. Unable to deduct.")
return
else:
update.message.reply_text("User not found. Unable to deduct.")
return
# Save/update user in MongoDB
collection.save(user)
# Generate Plisio API request payload for payment
payload = {
"api_key": PLISIO_API_KEY,
"api_secret": PLISIO_API_SECRET,
"wallet_id": user_id,
"amount": float(amount),
"currency": "USD"
}
# Make the API request to deduct credits via Plisio API (payment endpoint)
response = requests.post(PLISIO_API_PAYMENT_URL, data=payload)
if response.ok:
update.message.reply_text(f"Credits successfully deducted: {amount}")
else:
update.message.reply_text("Failed to deduct credits. Please try again later.")
def main():
# Set up the Telegram bot
updater = Updater(TELEGRAM_BOT_TOKEN, use_context=True)
dp = updater.dispatcher
# Add command handlers to the bot
dp.add_handler(CommandHandler("addcredits", add_credits))
dp.add_handler(CommandHandler("deductcredits", deduct_credits))
# Start the bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
In this script, we use two endpoints from the Plisio.net API documentation: /wallets/topup for adding credits to a user's account and /wallets/payment for deducting credits from a user's account.
Remember to replace YOUR_TELEGRAM_BOT_TOKEN, YOUR_PLISIO_API_KEY, and YOUR_PLISIO_API_SECRET with your actual credentials. Additionally, ensure that you have MongoDB installed and running locally on localhost:27017.
Apologies for the confusion. Below is an updated script that uses the Plisio.net API documentation to add and deduct credits from a user's account using a Telegram bot:
python
import requests
import pymongo
from telegram.ext import Updater, CommandHandler
# MongoDB connection
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["credits"]
collection = db["users"]
# Telegram Bot Token
TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
# Plisio API credentials
PLISIO_API_KEY = "YOUR_PLISIO_API_KEY"
PLISIO_SECRET_KEY = "YOUR_PLISIO_SECRET_KEY"
PLISIO_WALLET_ID = "YOUR_PLISIO_WALLET_ID"
# Plisio API endpoint
PLISIO_API_URL = "https://api.plisio.net/v1/"
def add_credits(update, context):
# Get user ID and amount from the command
user_id = update.message.from_user.id
amount = float(context.args[0])
# Retrieve user from MongoDB
user = collection.find_one({"user_id": user_id})
if user:
user['credits'] += amount
else:
user = {"user_id": user_id, "credits": amount}
# Save/update user in MongoDB
collection.save(user)
# Generate Plisio API request payload
payload = {
"wallet_id": PLISIO_WALLET_ID,
"currency": "USD",
"amount": amount,
"externalid": str(user_id)
}
# Make the API request to add credits via Plisio API
response = requests.post(
f"{PLISIO_API_URL}wallets/{PLISIO_WALLET_ID}/receive",
headers={"Content-Type": "application/x-www-form-urlencoded"},
auth=(PLISIO_API_KEY, PLISIO_SECRET_KEY),
data=payload
)
if response.status_code == 200:
update.message.reply_text(f"Credits successfully added: {amount}")
else:
update.message.reply_text("Failed to add credits. Please try again later.")
def deduct_credits(update, context):
# Get user ID and amount from the command
user_id = update.message.from_user.id
amount = float(context.args[0])
# Retrieve user from MongoDB
user = collection.find_one({"user_id": user_id})
if user:
if user['credits'] >= amount:
user['credits'] -= amount
else:
update.message.reply_text("Insufficient credits. Unable to deduct.")
return
else:
update.message.reply_text("User not found. Unable to deduct.")
return
# Save/update user in MongoDB
collection.save(user)
# Generate Plisio API request payload
payload = {
"wallet_id": PLISIO_WALLET_ID,
"currency": "USD",
"amount": amount,
"externalid": str(user_id)
}
# Make the API request to deduct credits via Plisio API
response = requests.post(
f"{PLISIO_API_URL}wallets/{PLISIO_WALLET_ID}/send",
headers={"Content-Type": "application/x-www-form-urlencoded"},
auth=(PLISIO_API_KEY, PLISIO_SECRET_KEY),
data=payload
)
if response.status_code == 200:
update.message.reply_text(f"Credits successfully deducted: {amount}")
else:
update.message.reply_text("Failed to deduct credits. Please try again later.")
def main():
# Set up the Telegram bot
updater = Updater(TELEGRAM_BOT_TOKEN, use_context=True)
dp = updater.dispatcher
# Add command handlers to the bot
dp.add_handler(CommandHandler("addcredits", add_credits))
dp.add_handler(CommandHandler("deductcredits", deduct_credits))
# Start the bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Please replace YOUR_TELEGRAM_BOT_TOKEN, YOUR_PLISIO_API_KEY, YOUR_PLISIO_SECRET_KEY, and YOUR_PLISIO_WALLET_ID with your actual credentials. Additionally, ensure that you have MongoDB installed and running locally on localhost:27017.