File size: 3,248 Bytes
9d382e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5592c90
9d382e5
5592c90
9d382e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import httpx
from config import BALE_TOKEN, BALE_API_BASE

BASE = f"{BALE_API_BASE}{BALE_TOKEN}"


async def _req(method: str, **kwargs):
    url = f"{BASE}/{method}"
    async with httpx.AsyncClient(timeout=60) as client:
        r = await client.post(url, json=kwargs)
        return r.json()


async def get_me():
    return await _req("getMe")


async def send_message(chat_id, text, reply_markup=None, parse_mode=None):
    kwargs = {"chat_id": chat_id, "text": text}
    if reply_markup:
        kwargs["reply_markup"] = reply_markup
    if parse_mode:
        kwargs["parse_mode"] = parse_mode
    return await _req("sendMessage", **kwargs)


async def send_photo(chat_id, photo, caption=None, reply_markup=None):
    kwargs = {"chat_id": chat_id, "photo": photo}
    if caption:
        kwargs["caption"] = caption
    if reply_markup:
        kwargs["reply_markup"] = reply_markup
    return await _req("sendPhoto", **kwargs)


async def send_document(chat_id, document, caption=None, reply_markup=None, filename="file"):
    url = f"{BASE}/sendDocument"
    files = {"document": (filename, document)}
    data = {"chat_id": str(chat_id)}
    if caption:
        data["caption"] = caption
    if reply_markup:
        data["reply_markup"] = reply_markup
    async with httpx.AsyncClient(timeout=300) as client:
        r = await client.post(url, data=data, files=files)
        return r.json()


async def send_document_by_url(chat_id, file_url, caption=None):
    kwargs = {"chat_id": chat_id, "document": file_url}
    if caption:
        kwargs["caption"] = caption
    return await _req("sendDocument", **kwargs)


async def send_chat_action(chat_id, action):
    return await _req("sendChatAction", chat_id=chat_id, action=action)


async def answer_callback_query(callback_query_id, text=None, show_alert=False):
    kwargs = {"callback_query_id": callback_query_id}
    if text:
        kwargs["text"] = text
    kwargs["show_alert"] = show_alert
    return await _req("answerCallbackQuery", **kwargs)


async def edit_message_text(chat_id, message_id, text, reply_markup=None):
    kwargs = {"chat_id": chat_id, "message_id": message_id, "text": text}
    if reply_markup:
        kwargs["reply_markup"] = reply_markup
    return await _req("editMessageText", **kwargs)


async def get_file(file_id):
    return await _req("getFile", file_id=file_id)


async def get_file_link(file_path: str):
    return f"https://tapi.bale.ai/file/bot{BALE_TOKEN}/{file_path}"


def make_keyboard(buttons):
    return {"inline_keyboard": [[{"text": b[0], "callback_data": b[1]}] for b in buttons]}


def make_reply_keyboard(buttons, resize=True):
    return {
        "keyboard": [[{"text": b}] for b in buttons],
        "resize_keyboard": resize,
    }


def remove_keyboard():
    return {"remove_keyboard": True}


MENU_BUTTONS = make_reply_keyboard([
    "🎬 YouTube Downloader",
    "πŸ“ File Downloader",
    "πŸ€– ChatGPT",
    "🌐 Open Panel",
    "❓ Help",
])

YOUTUBE_QUALITY = make_keyboard([
    ("🎡 Audio (MP3)", "yt:audio"),
    ("🎬 Video (720p)", "yt:720p"),
    ("🎬 Video (1080p)", "yt:1080p"),
    ("πŸ”™ Back to Menu", "menu:main"),
])

BACK_TO_MENU = make_keyboard([
    ("πŸ”™ Back to Menu", "menu:main"),
])