File size: 7,732 Bytes
8bef42a
2c0a8e1
c5b19bf
8bef42a
ff41199
f7bc7c5
ff41199
8bef42a
 
 
 
ecf3d1e
 
8bef42a
99d08e8
8bef42a
 
 
 
 
f7bc7c5
 
 
 
 
 
 
26c910c
 
 
 
 
 
 
 
 
 
 
ecf3d1e
 
8bef42a
f7bc7c5
ecf3d1e
f7bc7c5
 
ecf3d1e
 
1001fe0
8bef42a
f7bc7c5
2c0a8e1
1ad52b3
f7bc7c5
 
 
 
441fb2e
 
 
 
 
 
 
 
2c0a8e1
 
 
 
441fb2e
2c0a8e1
441fb2e
 
 
2c0a8e1
 
 
 
 
137e850
dd06910
137e850
 
d676f4e
137e850
 
 
 
 
d676f4e
 
d230be6
137e850
d676f4e
 
 
 
137e850
d230be6
d676f4e
 
d230be6
 
d676f4e
 
 
 
 
 
 
 
137e850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b36489
137e850
 
1001fe0
 
d82dee8
 
f154ce2
d82dee8
 
 
 
 
1001fe0
 
 
 
 
 
 
 
 
 
 
 
 
92e82d0
 
 
 
 
1001fe0
 
92e82d0
 
1001fe0
92e82d0
15bf604
 
 
 
 
d82dee8
 
1001fe0
5edf6d2
ecf3d1e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import logging
import base64
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: str = None):
    username, random_id = get_random_from_channel(link)
    try:
        stories = await user_client.get_stories(username, story_ids=[random_id])
    except Exception as e:
        return {"error": str(e)}
    if stories:
        for story in stories:
            file_id = (
                story.photo.file_id if story and story.photo else None
                or story.video.file_id if story and story.video else None
            )
            caption = story.caption or f"By {user_client.me.mention}"
            if file_id:
                image_path = await user_client.download_media(file_id)
                
                with open(image_path, "rb") as f:
                    base64_image = base64.b64encode(f.read()).decode("utf-8")
                return {
                    "download": base64_image,
                    "random_file_id": file_id,
                    "caption": caption
                }
    return {
        "random_file_id": None,
        "caption": None
    }

@fast_app.get("/user/raw/getchat")
async def get_chat_raw(username: str = None):
    try:
        chat_raw = (await client.get_chat(username)).raw
        ok = await user_client.get_chat(username)
    except Exception:
        return {"error": "Error try again invalid"}
    return {
        "id": chat_raw.id,
        "about": chat_raw.about,
        "title": ok.title,
        "username": ok.username if ok else None,
        "photo": {
            "id": chat_raw.chat_photo.id,
            "small_file_id": ok.photo.small_file_id,
            "small_photo_unique_id": ok.photo.small_photo_unique_id,
            "big_file_id": ok.photo.big_file_id,
            "big_photo_unique_id": ok.photo.big_photo_unique_id,
            "date": chat_raw.chat_photo.date,
            "dc_id": chat_raw.chat_photo.dc_id,
            "has_animation": ok.photo.has_animation,
            "is_personal": ok.photo.is_personal
        },
        "from_user": {
            "id": ok.pinned_message.from_user.id if ok.pinned_message.from_user else None,
            "first_name": ok.pinned_message.from_user.first_name if ok.pinned_message.from_user else None,
            "last_name": ok.pinned_message.from_user.last_name if ok.pinned_message.from_user else None,
            "status": ok.pinned_message.from_user.status.name if ok.pinned_message.from_user else None,
            "last_online_date": chat_.pinned_message.from_user.last_online_date if ok.pinned_message.from_user else None,
            "username": ok.pinned_message.from_user.username if ok.pinned_message.from_user else None,
            "is_premium": ok.pinned_message.from_user.is_premium if ok.pinned_message.from_user else None,
            "is_scam": ok.pinned_message.from_user.is_scam if ok.pinned_message.from_user else None
        },
        "can_view_participants": chat_raw.can_view_participants,
        "can_set_username": chat_raw.can_set_username,
        "can_set_stickers": chat_raw.can_set_stickers,
        "hidden_prehistory": chat_raw.hidden_prehistory,
        "can_set_location": chat_raw.can_set_location,
        "has_scheduled": chat_raw.has_scheduled,
        "can_view_stats": chat_raw.can_view_stats,
        "blocked": chat_raw.blocked,
        "can_delete_channel": chat_raw.can_delete_channel,
        "antispam": chat_raw.antispam,
        "participants_hidden": chat_raw.participants_hidden,
        "translations_disabled": chat_raw.translations_disabled,
        "stories_pinned_available": chat_raw.stories_pinned_available,
        "view_forum_as_messages": chat_raw.view_forum_as_messages,
        "restricted_sponsored": chat_raw.restricted_sponsored,
        "can_view_revenue": chat_raw.can_view_revenue,
        "paid_media_allowed": chat_raw.paid_media_allowed,
        "can_view_stars_revenue": chat_raw.can_view_stars_revenue,
        "paid_reactions_available": chat_raw.paid_reactions_available,
        "stargifts_available": chat_raw.stargifts_available,
        "participants_count": chat_raw.participants_count,
        "online_count": chat_raw.online_count,
        "pinned_msg_id": chat_raw.pinned_msg_id,
        "linked_chat_id": chat_raw.linked_chat_id,
        "pending_suggestions": chat_raw.pending_suggestions,
        "recent_requesters": chat_raw.recent_requesters,
        "available_reactions": {
            "allow_custom": chat_raw.available_reactions.allow_custom
        },
    }

@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)