add decline and acceptation buttons
Browse files- app/main.py +68 -42
app/main.py
CHANGED
|
@@ -9,6 +9,7 @@ import json
|
|
| 9 |
import threading
|
| 10 |
import uuid
|
| 11 |
import telebot
|
|
|
|
| 12 |
from telebot import types
|
| 13 |
|
| 14 |
# --- CLASE PARA PERSISTENCIA ---
|
|
@@ -55,11 +56,12 @@ def main(page: ft.Page):
|
|
| 55 |
# --- CONFIGURACIÓN DE VARIABLES ---
|
| 56 |
TG_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 57 |
TG_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
|
|
|
|
| 58 |
GITLAB_URL = "https://gitlab.com"
|
| 59 |
GIT_TOKEN = os.getenv("GITLAB_TOKEN")
|
| 60 |
GIT_GROUP = os.getenv("GITLAB_GROUP_ID")
|
| 61 |
|
| 62 |
-
# Inicializar bot
|
| 63 |
bot = telebot.TeleBot(TG_TOKEN) if TG_TOKEN else None
|
| 64 |
|
| 65 |
def mostrar_notificacion(texto, color="blue"):
|
|
@@ -69,7 +71,8 @@ def main(page: ft.Page):
|
|
| 69 |
page.update()
|
| 70 |
|
| 71 |
def enviar_telegram_con_botones(loan_id, mensaje):
|
| 72 |
-
|
|
|
|
| 73 |
return "ERR_TOKEN"
|
| 74 |
|
| 75 |
markup = types.InlineKeyboardMarkup()
|
|
@@ -77,56 +80,79 @@ def main(page: ft.Page):
|
|
| 77 |
btn_declinar = types.InlineKeyboardButton("❌ Declinar", callback_data=f"decline_{loan_id}")
|
| 78 |
markup.add(btn_aceptar, btn_declinar)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
try:
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
-
print(f"Error
|
| 85 |
-
return "
|
| 86 |
|
| 87 |
# --- BOT CALLBACK HANDLER ---
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
def start_bot():
|
| 125 |
if bot:
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
# Iniciar polling en hilo separado
|
| 129 |
-
if
|
| 130 |
threading.Thread(target=start_bot, daemon=True).start()
|
| 131 |
|
| 132 |
def mostrar_factura(i):
|
|
|
|
| 9 |
import threading
|
| 10 |
import uuid
|
| 11 |
import telebot
|
| 12 |
+
import time
|
| 13 |
from telebot import types
|
| 14 |
|
| 15 |
# --- CLASE PARA PERSISTENCIA ---
|
|
|
|
| 56 |
# --- CONFIGURACIÓN DE VARIABLES ---
|
| 57 |
TG_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 58 |
TG_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
|
| 59 |
+
GOOGLE_PROXY_URL = "https://script.google.com/macros/s/AKfycbz7z1Jb0vsur42GmmqrL3PVXeRkN2WxSojFDIleEDoLOg6MnrmJjb_uuPcQ15CTwyzD/exec"
|
| 60 |
GITLAB_URL = "https://gitlab.com"
|
| 61 |
GIT_TOKEN = os.getenv("GITLAB_TOKEN")
|
| 62 |
GIT_GROUP = os.getenv("GITLAB_GROUP_ID")
|
| 63 |
|
| 64 |
+
# Inicializar bot (esto no fallará aquí, solo crea la instancia)
|
| 65 |
bot = telebot.TeleBot(TG_TOKEN) if TG_TOKEN else None
|
| 66 |
|
| 67 |
def mostrar_notificacion(texto, color="blue"):
|
|
|
|
| 71 |
page.update()
|
| 72 |
|
| 73 |
def enviar_telegram_con_botones(loan_id, mensaje):
|
| 74 |
+
chat_id = str(os.getenv("TELEGRAM_CHAT_ID", "")).strip()
|
| 75 |
+
if not TG_TOKEN or not chat_id:
|
| 76 |
return "ERR_TOKEN"
|
| 77 |
|
| 78 |
markup = types.InlineKeyboardMarkup()
|
|
|
|
| 80 |
btn_declinar = types.InlineKeyboardButton("❌ Declinar", callback_data=f"decline_{loan_id}")
|
| 81 |
markup.add(btn_aceptar, btn_declinar)
|
| 82 |
|
| 83 |
+
# Enviar vía Proxy de Google para evitar bloqueos de red
|
| 84 |
+
payload = {
|
| 85 |
+
"chat_id": chat_id,
|
| 86 |
+
"text": mensaje,
|
| 87 |
+
"reply_markup": json.loads(markup.to_json()),
|
| 88 |
+
"parse_mode": "Markdown"
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
try:
|
| 92 |
+
response = requests.post(GOOGLE_PROXY_URL, json=payload, timeout=15)
|
| 93 |
+
if response.status_code == 200:
|
| 94 |
+
return "OK"
|
| 95 |
+
return f"ERR_PROXY_{response.status_code}"
|
| 96 |
except Exception as e:
|
| 97 |
+
print(f"Error Proxy: {e}")
|
| 98 |
+
return "ERR_CONN"
|
| 99 |
|
| 100 |
# --- BOT CALLBACK HANDLER ---
|
| 101 |
+
if bot:
|
| 102 |
+
@bot.callback_query_handler(func=lambda call: True)
|
| 103 |
+
def handle_query(call):
|
| 104 |
+
try:
|
| 105 |
+
if call.data.startswith("accept_"):
|
| 106 |
+
loan_id = call.data.replace("accept_", "")
|
| 107 |
+
if loan_mgr.update_status(loan_id, "ACCEPTED"):
|
| 108 |
+
bot.answer_callback_query(call.id, "Préstamo Aceptado")
|
| 109 |
+
bot.edit_message_text(f"✅ *ACEPTADO*\n{call.message.text}", TG_CHAT_ID, call.message.message_id, parse_mode="Markdown")
|
| 110 |
+
mostrar_notificacion(f"Préstamo {loan_id[:8]} ACEPTADO", ft.Colors.GREEN)
|
| 111 |
+
elif call.data.startswith("decline_"):
|
| 112 |
+
loan_id = call.data.replace("decline_", "")
|
| 113 |
+
if loan_mgr.update_status(loan_id, "DECLINED"):
|
| 114 |
+
bot.answer_callback_query(call.id, "Préstamo Declinado")
|
| 115 |
+
bot.edit_message_text(f"❌ *DECLINADO*\n{call.message.text}", TG_CHAT_ID, call.message.message_id, parse_mode="Markdown")
|
| 116 |
+
mostrar_notificacion(f"Préstamo {loan_id[:8]} DECLINADO", ft.Colors.RED)
|
| 117 |
+
page.update()
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Callback Error: {e}")
|
| 120 |
|
| 121 |
+
# --- BOT COMMAND HANDLER ---
|
| 122 |
+
@bot.message_handler(commands=['aceptar', 'declinar', 'status'])
|
| 123 |
+
def handle_text_commands(message):
|
| 124 |
+
try:
|
| 125 |
+
text = message.text.split()
|
| 126 |
+
if len(text) < 2:
|
| 127 |
+
bot.reply_to(message, "Uso: /aceptar <id> o /declinar <id>")
|
| 128 |
+
return
|
| 129 |
+
|
| 130 |
+
cmd = text[0][1:]
|
| 131 |
+
loan_id = text[1]
|
| 132 |
+
|
| 133 |
+
status = "ACCEPTED" if cmd == "aceptar" else "DECLINED"
|
| 134 |
+
if loan_mgr.update_status(loan_id, status):
|
| 135 |
+
emoji = "✅" if status == "ACCEPTED" else "❌"
|
| 136 |
+
bot.reply_to(message, f"{emoji} Préstamo {loan_id} actualizado a {status}")
|
| 137 |
+
mostrar_notificacion(f"Préstamo {loan_id[:8]} {status}", ft.Colors.GREEN if status == "ACCEPTED" else ft.Colors.RED)
|
| 138 |
+
else:
|
| 139 |
+
bot.reply_to(message, "ID de préstamo no encontrado.")
|
| 140 |
+
page.update()
|
| 141 |
+
except Exception as e:
|
| 142 |
+
print(f"Command Error: {e}")
|
| 143 |
|
| 144 |
def start_bot():
|
| 145 |
if bot:
|
| 146 |
+
while True:
|
| 147 |
+
try:
|
| 148 |
+
# El polling seguirá fallando si api.telegram.org está bloqueado totalmente
|
| 149 |
+
bot.infinity_polling(timeout=10, long_polling_timeout=5)
|
| 150 |
+
except Exception as e:
|
| 151 |
+
print(f"Error de red en el bot (DNS/Bloqueo?): {e}. Reintentando en 60s...")
|
| 152 |
+
time.sleep(60)
|
| 153 |
|
| 154 |
# Iniciar polling en hilo separado
|
| 155 |
+
if bot:
|
| 156 |
threading.Thread(target=start_bot, daemon=True).start()
|
| 157 |
|
| 158 |
def mostrar_factura(i):
|