mj_bale_chat/router/bale_bot.py
2025-12-30 11:39:44 +00:00

41 lines
1.2 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# router.bale_bot.py
from fastapi import Depends, APIRouter, Request
import requests, traceback
from utils.main import BaleBot, BaleUpdate
from dependencies import _get_bale_token, _get_bale_bot
##############
router = APIRouter(tags=["bale-bot"])
##############
# @app.post(f"/webhook/{TOKEN}")
@router.post("/webhook/{token}", description="ربات قانون یار")
async def webhook(
request: Request,
token: str = Depends(_get_bale_token),
bale_bot: BaleBot = Depends(_get_bale_bot),
):
raw = await request.json()
try:
update = BaleUpdate(**raw)
except Exception as e:
print("❌ Parse Error", e)
return {"ok": True}
# print(f"update {update}")
return await bale_bot.render_update(update)
def initialize_webhook(webhook_url, set_webhook_url):
"""
این تابع برای ربات اجباری است
به سرور بله مشخص میکند که به چه server ایی درخواست ها را با این توکن داده شده ارسال کند
"""
params = {"url": webhook_url}
r = requests.get(set_webhook_url, params=params)
print("Webhook set status:", r.status_code)
print("Webhook set response:", r.json())