Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,76 @@
|
|
| 1 |
-
|
| 2 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
from aiogram import Bot, Dispatcher, F
|
| 5 |
from aiogram.enums import ChatType
|
| 6 |
from aiogram.filters import Command, CommandStart
|
| 7 |
from aiogram.types import FSInputFile, Message
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from agent1 import Config, DB, engine, scheduler, supabase_store
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
dp = Dispatcher()
|
| 13 |
|
|
@@ -233,24 +296,26 @@ async def notify_boss_messages(bot: Bot):
|
|
| 233 |
|
| 234 |
|
| 235 |
async def main():
|
| 236 |
-
from aiogram.client.session.aiohttp import AiohttpSession
|
| 237 |
-
from aiogram.client.telegram import TelegramAPIServer
|
| 238 |
-
|
| 239 |
if not Config.BOT_TOKEN:
|
| 240 |
raise RuntimeError("BOT_TOKEN is missing")
|
| 241 |
-
|
| 242 |
-
#
|
| 243 |
-
# This grabs your Cloudflare Worker URL from agent1.py and smashes the DNS block!
|
| 244 |
custom_session = AiohttpSession(
|
| 245 |
-
api=TelegramAPIServer.from_base(
|
| 246 |
)
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
bot = Bot(token=Config.BOT_TOKEN, session=custom_session)
|
| 250 |
|
| 251 |
scheduler.set_bot(bot)
|
| 252 |
await scheduler.start()
|
| 253 |
asyncio.create_task(notify_boss_messages(bot))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
await dp.start_polling(bot)
|
| 255 |
|
| 256 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
asyncio
|
| 2 |
import base64
|
| 3 |
+
import json
|
| 4 |
+
import subprocess
|
| 5 |
+
import threading
|
| 6 |
+
import time
|
| 7 |
+
from urllib.parse import urlparse
|
| 8 |
|
| 9 |
+
from flask import Flask, request, Response
|
| 10 |
from aiogram import Bot, Dispatcher, F
|
| 11 |
from aiogram.enums import ChatType
|
| 12 |
from aiogram.filters import Command, CommandStart
|
| 13 |
from aiogram.types import FSInputFile, Message
|
| 14 |
+
from aiogram.client.session.aiohttp import AiohttpSession
|
| 15 |
+
from aiogram.client.telegram import TelegramAPIServer
|
| 16 |
|
| 17 |
from agent1 import Config, DB, engine, scheduler, supabase_store
|
| 18 |
|
| 19 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 20 |
+
# β π THE CURL BRIDGE (HUGGING FACE BYPASS) β
|
| 21 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 22 |
+
|
| 23 |
+
BRIDGE_PORT = 7860
|
| 24 |
+
# We pull your URLs directly from your Config class!
|
| 25 |
+
PROXY_TARGET = Config.PROXY_TARGET
|
| 26 |
+
CLOUDFLARE_IP = Config.CLOUDFLARE_IP
|
| 27 |
+
|
| 28 |
+
bridge_app = Flask(__name__)
|
| 29 |
+
|
| 30 |
+
def run_curl(method, url, data=None):
|
| 31 |
+
parsed = urlparse(url)
|
| 32 |
+
domain = parsed.hostname
|
| 33 |
+
|
| 34 |
+
cmd = [
|
| 35 |
+
"curl", "-X", method, url,
|
| 36 |
+
"--resolve", f"{domain}:443:{CLOUDFLARE_IP}",
|
| 37 |
+
"-H", "User-Agent: Mozilla/5.0",
|
| 38 |
+
"-H", "Content-Type: application/json",
|
| 39 |
+
"-k", "-s", "--max-time", "30"
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
input_str = None
|
| 43 |
+
if data:
|
| 44 |
+
cmd.extend(["--data-binary", "@-"])
|
| 45 |
+
input_str = json.dumps(data)
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
result = subprocess.run(cmd, input=input_str, capture_output=True, text=True, timeout=35)
|
| 49 |
+
if not result.stdout:
|
| 50 |
+
return json.dumps({"ok": False, "description": "Empty Curl Response", "error_code": 500})
|
| 51 |
+
return result.stdout
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return json.dumps({"ok": False, "description": str(e), "error_code": 500})
|
| 54 |
+
|
| 55 |
+
@bridge_app.route('/bot<token>/<method>', methods=['POST', 'GET'])
|
| 56 |
+
def proxy(token, method):
|
| 57 |
+
real_url = f"{PROXY_TARGET}/bot{token}/{method}"
|
| 58 |
+
data = request.get_json(force=True, silent=True)
|
| 59 |
+
if not data:
|
| 60 |
+
data = request.form.to_dict() or request.args.to_dict()
|
| 61 |
+
response_text = run_curl(request.method, real_url, data)
|
| 62 |
+
return Response(response_text, mimetype='application/json')
|
| 63 |
+
|
| 64 |
+
def start_bridge():
|
| 65 |
+
print(f"π Starting Bridge on 0.0.0.0:{BRIDGE_PORT}")
|
| 66 |
+
bridge_app.run(host="0.0.0.0", port=BRIDGE_PORT, threaded=True)
|
| 67 |
+
|
| 68 |
+
# Start the bridge in the background IMMEDIATELY
|
| 69 |
+
threading.Thread(target=start_bridge, daemon=True).start()
|
| 70 |
+
time.sleep(3)
|
| 71 |
+
|
| 72 |
+
# ββ KEEP YOUR dp = Dispatcher() AND ALL YOUR MESSAGE HANDLERS BELOW THIS LINE ββ
|
| 73 |
+
|
| 74 |
|
| 75 |
dp = Dispatcher()
|
| 76 |
|
|
|
|
| 296 |
|
| 297 |
|
| 298 |
async def main():
|
|
|
|
|
|
|
|
|
|
| 299 |
if not Config.BOT_TOKEN:
|
| 300 |
raise RuntimeError("BOT_TOKEN is missing")
|
| 301 |
+
|
| 302 |
+
# Connect the bot to the local Flask bridge we just built
|
|
|
|
| 303 |
custom_session = AiohttpSession(
|
| 304 |
+
api=TelegramAPIServer.from_base(f"http://127.0.0.1:{BRIDGE_PORT}")
|
| 305 |
)
|
| 306 |
+
|
| 307 |
+
bot = Bot(token=Config.BOT_TOKEN, session=custom_session, parse_mode="HTML")
|
|
|
|
| 308 |
|
| 309 |
scheduler.set_bot(bot)
|
| 310 |
await scheduler.start()
|
| 311 |
asyncio.create_task(notify_boss_messages(bot))
|
| 312 |
+
|
| 313 |
+
print("π€ BRUKGUARDIAN AGENTFORGE IS STARTING...")
|
| 314 |
+
try:
|
| 315 |
+
await bot.delete_webhook(drop_pending_updates=True)
|
| 316 |
+
except Exception:
|
| 317 |
+
pass
|
| 318 |
+
|
| 319 |
await dp.start_polling(bot)
|
| 320 |
|
| 321 |
if __name__ == "__main__":
|