|
import logging |
|
from contextlib import asynccontextmanager |
|
from akenoai import AkenoXToJs as js |
|
from akenoai.runner import run_fast |
|
from config import API_ID, API_HASH, BOT_TOKEN, SESSION |
|
|
|
logger = logging.getLogger(__name__) |
|
LOGS = logging.getLogger("[akenox]") |
|
logger.setLevel(logging.DEBUG) |
|
|
|
fast_app = js.get_app() |
|
|
|
client = js.create_pyrogram( |
|
name="fastapi-bot", |
|
api_id=API_ID, |
|
api_hash=API_HASH, |
|
bot_token=BOT_TOKEN |
|
) |
|
|
|
user_client = js.create_pyrogram( |
|
name="fastapi-user", |
|
api_id=API_ID, |
|
api_hash=API_HASH, |
|
session_string=SESSION |
|
) |
|
|
|
def get_random_from_channel(link): |
|
clean_link = link.split("?")[0] |
|
target_link = clean_link.split("/c/") if "/c/" in clean_link else clean_link.split("/") |
|
random_id = int(target_link[-1].split("/")[-1]) if len(target_link) > 1 else None |
|
desired_username = target_link[3] if len(target_link) > 3 else None |
|
username = ( |
|
"@" + desired_username if desired_username else "-100" + target_link[1].split("/")[0] |
|
if len(target_link) > 1 else None |
|
) |
|
return username, random_id |
|
|
|
@fast_app.on_event("startup") |
|
async def startup_event(): |
|
user = await client.start() |
|
userbot = await user_client.start() |
|
LOGS.info(f"Started Bot: {user.me.first_name}") |
|
LOGS.info(f"Started UserBot: {userbot.me.first_name}") |
|
|
|
@fast_app.get("/") |
|
async def hello(): |
|
return {"success": "hello world!"} |
|
|
|
@fast_app.get("/user/get_story") |
|
async def get_user_story(link=None): |
|
username, random_id = get_random_from_channel(link_story) |
|
try: |
|
stories = await user_client.get_stories(username, story_ids=[random_id]) |
|
except Exception as e: |
|
return {"error": str(e)} |
|
file_id = ( |
|
stories.photo.file_id if stories and stories.photo else None |
|
or stories.video.file_id if stories and stories.video else None |
|
) |
|
caption = stories.caption or f"By {user_client.me.mention}" |
|
return { |
|
"id": stories.id, |
|
"date": stories.date, |
|
"expire_date": stories.expire_date, |
|
"media": stories.media, |
|
"has_protected_content": stories.has_protected_content, |
|
"random_file_id": file_id, |
|
"caption": caption, |
|
"edited": stories.edited, |
|
"pinned": stories.pinned, |
|
"close_friends": stories.close_friends, |
|
"contacts": stories.contacts, |
|
"selected_contacts": stories.selected_contacts, |
|
"outgoing": stories.outgoing |
|
} |
|
|
|
@fast_app.get("/user/get_user") |
|
async def get_user(user_id=None): |
|
try: |
|
get_users = await client.get_users(user_id) |
|
response = await js.get_creation_date( |
|
api_key="akeno_OSrXhljIomunACd5JY18jFIeIuuB6Pdx", |
|
user_id=get_users.id |
|
) |
|
except Exception: |
|
return {"error": "Error try again invalid"} |
|
return { |
|
"id": get_users.id, |
|
"is_self": get_users.is_self, |
|
"is_contact": get_users.is_contact, |
|
"is_mutual_contact": get_users.is_mutual_contact, |
|
"is_deleted": get_users.is_deleted, |
|
"is_bot": get_users.is_bot, |
|
"is_verified": get_users.is_verified, |
|
"is_restricted": get_users.is_restricted, |
|
"is_scam": get_users.is_scam, |
|
"is_fake": get_users.is_fake, |
|
"is_support": get_users.is_support, |
|
"is_premium": get_users.is_premium, |
|
"is_contact_require_premium": get_users.is_contact_require_premium, |
|
"is_close_friend": get_users.is_close_friend, |
|
"is_stories_hidden": get_users.is_stories_hidden, |
|
"is_stories_unavailable": get_users.is_stories_unavailable, |
|
"is_business_bot": get_users.is_business_bot, |
|
"first_name": get_users.first_name, |
|
"status": get_users.status.name, |
|
"last_online_date": get_users.last_online_date, |
|
"dc_id": get_users.dc_id, |
|
"username": get_users.username if get_users else None, |
|
"photo": { |
|
"small_file_id": get_users.photo.small_file_id, |
|
"small_photo_unique_id": get_users.photo.small_photo_unique_id, |
|
"big_file_id": get_users.photo.big_file_id, |
|
"has_animation": get_users.photo.has_animation, |
|
"is_personal": get_users.photo.is_personal |
|
}, |
|
"creation_date": response |
|
} |
|
|
|
run_fast(build=fast_app, port=7860) |