Added README, added Google Sheets URL

This commit is contained in:
Joan
2023-10-31 17:53:37 +01:00
parent f617561c58
commit fe769cf8a0
3 changed files with 61 additions and 62 deletions

View File

@@ -29,7 +29,7 @@ bot_token = os.environ.get("TELEGRAM_TOKEN")
spreadsheet_id = os.environ.get("SPREADSHEET_ID")
# Configura la base de datos SQLite
conn = sqlite3.connect('/app/data/db/conjuntas.db')
conn = sqlite3.connect('/app/data/conjuntas.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS conjuntas (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -65,28 +65,6 @@ creds = ServiceAccountCredentials.from_json_keyfile_name(json_keyfile, scopes)
client = gspread.authorize(creds)
spreadsheet = client.open_by_key(spreadsheet_id)
async def list_conjuntas(update: Update, context: CallbackContext):
chat_id = update.message.chat_id
cursor.execute("SELECT * FROM conjuntas WHERE closed=0")
conjuntas = cursor.fetchall()
if conjuntas:
for conjunta in conjuntas:
time.sleep(0.5)
conjunta_id, message_id, product_name, product_description, limit, limit_per_user, price, price_member, closed, photo_id = conjunta
caption = f"Conjunta para '{product_name}'\n"
cursor.execute("SELECT COUNT(*) FROM conjunta_users WHERE conjunta_id = ?", (conjunta[0],))
users = cursor.fetchone()
if users:
caption += f"Participantes: {users[0]}\n"
if limit is not None:
caption += f"Límite de productos: {limit} ({limit_per_user} por usuario)\n"
await context.bot.send_photo(chat_id, photo=photo_id, caption=caption)
else:
await update.message.reply_text("No hay conjuntas activas.")
# Función para iniciar una nueva conjunta
async def start_conjunta(update: Update, context: CallbackContext):
user_id = update.effective_user.id
@@ -390,39 +368,11 @@ async def handle_conjunta(update: Update, context: CallbackContext):
else:
await update.message.reply_text("La conjunta ya está cerrada y no puedes unirte.")
# Función para consultar el estado de una conjunta
async def status_conjunta(update: Update, context: CallbackContext):
user_id = update.effective_user.id
conjunta_id = context.args[0]
cursor.execute("SELECT * FROM conjuntas WHERE id=?", (conjunta_id,))
conjunta = cursor.fetchone()
if conjunta:
conjunta_id, message_id, product_name, product_description, limit, limit_per_user, price, price_member, closed, photo_id = conjunta
cursor.execute("SELECT * FROM conjunta_users WHERE conjunta_id=?", (conjunta_id,))
users_data = cursor.fetchall()
status_text = f"Conjunta para '{product_name}':\n"
if users_data:
status_text += "Participantes y cantidades:\n"
for user_data in users_data:
user_id, user_quantity = user_data[2], user_data[3]
status_text += f"Usuario {user_id}: {user_quantity} unidades\n"
if limit is not None:
status_text += f"Límite de productos: {limit}\n"
status_text += f"Estado: {'Abierta' if closed == 0 else 'Cerrada'}"
await update.message.reply_text(status_text)
else:
await update.message.reply_text("La conjunta no existe.")
async def close_conjunta(update: Update, context: CallbackContext):
user_id = update.effective_user.id
query = update.callback_query
await query.answer(text="Cerrando conjunta")
await query.edit_message_reply_markup(None)
conjunta_id = int(query.data.split("close ")[1])
@@ -484,6 +434,8 @@ async def admin_summary(update: Update, context: CallbackContext):
summary_text += f"#️⃣ Límite: <b>{limit}</b>\n"
summary_text += f"#️⃣ Límite por usuario: <b>{limit_per_user}</b>\n"
summary_text += f"🔢 Cantidad total de productos pedidos: <b>{total_quantity}</b>\n"
url = spreadsheet.worksheet(f"{product_name} - {conjunta_id}").url
summary_text += f"<a href='{url}'>💻 URL en Google Sheets</a>\n"
summary_text += f"\n--------------------------------------\n\n"
await update.message.reply_text(summary_text, parse_mode=ParseMode.HTML)
@@ -510,6 +462,7 @@ async def admin_summary(update: Update, context: CallbackContext):
async def show_conjunta_details(update: Update, context: CallbackContext):
query = update.callback_query
await query.answer(text="Mostrando detalles")
await query.edit_message_reply_markup(None)
selected_conjunta_idx = int(query.data.split("info ")[1])
@@ -533,6 +486,9 @@ async def show_conjunta_details(update: Update, context: CallbackContext):
id, conjunta_id, user_id, user_name, quantity = user
selected_conjunta_details += f"@{user_name},{quantity}\n"
selected_conjunta_details += "-----------------------"
url = spreadsheet.worksheet(f"{product_name} - {conjunta_id}").url
selected_conjunta_details += f"\n<a href='{url}'>💻 URL en Google Sheets</a>\n"
await context.bot.send_photo(chat_id=update.effective_chat.id, photo=photo_id, caption=selected_conjunta_details, parse_mode=ParseMode.HTML)
@@ -556,11 +512,9 @@ def main()->None:
)
application.add_handler(conv_handler)
application.add_handler(CommandHandler('status', status_conjunta))
application.add_handler(CallbackQueryHandler(close_conjunta, pattern="close \d"))
application.add_handler(CommandHandler('list', list_conjuntas))
application.add_handler(CommandHandler('admin_summary', admin_summary))
application.add_handler(CallbackQueryHandler(show_conjunta_details, pattern="info \d"))
application.add_handler(CallbackQueryHandler(close_conjunta, pattern="close \d"))
application.add_handler(MessageHandler(filters.REPLY, handle_conjunta))
application.run_polling()