mirror of
https://github.com/paul-nameless/tg
synced 2025-02-16 18:48:24 +00:00
Move extended implementation of telegram api to tdlib.py
This commit is contained in:
parent
4c83254908
commit
d6f50c5483
4 changed files with 162 additions and 163 deletions
|
@ -7,11 +7,10 @@ from signal import SIGWINCH, signal
|
|||
from tempfile import NamedTemporaryFile
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from telegram.client import Telegram
|
||||
|
||||
from tg import config
|
||||
from tg.models import Model
|
||||
from tg.msg import MsgProxy
|
||||
from tg.tdlib import Tdlib
|
||||
from tg.utils import (
|
||||
get_duration,
|
||||
get_video_resolution,
|
||||
|
@ -43,7 +42,7 @@ class Controller:
|
|||
# View is terminal vindow
|
||||
"""
|
||||
|
||||
def __init__(self, model: Model, view: View, tg: Telegram) -> None:
|
||||
def __init__(self, model: Model, view: View, tg: Tdlib) -> None:
|
||||
self.model = model
|
||||
self.view = view
|
||||
self.lock = threading.Lock()
|
||||
|
|
157
tg/main.py
157
tg/main.py
|
@ -5,17 +5,16 @@ import threading
|
|||
from curses import window, wrapper # type: ignore
|
||||
from functools import partial
|
||||
|
||||
from telegram.client import AsyncResult, Telegram
|
||||
|
||||
from tg import config, utils
|
||||
from tg.controllers import Controller
|
||||
from tg.models import Model
|
||||
from tg.tdlib import Tdlib
|
||||
from tg.views import ChatView, MsgView, StatusView, View
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def run(tg: Telegram, stdscr: window) -> None:
|
||||
def run(tg: Tdlib, stdscr: window) -> None:
|
||||
# run this function in thread?
|
||||
model = Model(tg)
|
||||
status_view = StatusView(stdscr)
|
||||
|
@ -31,156 +30,6 @@ def run(tg: Telegram, stdscr: window) -> None:
|
|||
t.join()
|
||||
|
||||
|
||||
class TelegramApi(Telegram):
|
||||
def download_file(
|
||||
self, file_id, priority=16, offset=0, limit=0, synchronous=False,
|
||||
):
|
||||
result = self.call_method(
|
||||
"downloadFile",
|
||||
params=dict(
|
||||
file_id=file_id,
|
||||
priority=priority,
|
||||
offset=offset,
|
||||
limit=limit,
|
||||
synchronous=synchronous,
|
||||
),
|
||||
block=False,
|
||||
)
|
||||
result.wait()
|
||||
|
||||
def send_doc(self, file_path: str, chat_id: int) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessageDocument",
|
||||
"document": {"@type": "inputFileLocal", "path": file_path},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def send_audio(self, file_path: str, chat_id: int) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessageAudio",
|
||||
"audio": {"@type": "inputFileLocal", "path": file_path},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def send_photo(self, file_path: str, chat_id: int) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessagePhoto",
|
||||
"photo": {"@type": "inputFileLocal", "path": file_path},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def send_video(
|
||||
self,
|
||||
file_path: str,
|
||||
chat_id: int,
|
||||
width: int,
|
||||
height: int,
|
||||
duration: int,
|
||||
) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessageVideo",
|
||||
"width": width,
|
||||
"height": height,
|
||||
"duration": duration,
|
||||
"video": {"@type": "inputFileLocal", "path": file_path},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def send_voice(
|
||||
self, file_path: str, chat_id: int, duration: int, waveform: int
|
||||
):
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessageVoiceNote",
|
||||
"duration": duration,
|
||||
"waveform": waveform,
|
||||
"voice_note": {"@type": "inputFileLocal", "path": file_path},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def edit_message(self, chat_id: int, message_id: int, msg: str):
|
||||
data = {
|
||||
"@type": "editMessageText",
|
||||
"message_id": message_id,
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessageText",
|
||||
"text": {"@type": "formattedText", "text": msg},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def toggle_chat_is_marked_as_unread(
|
||||
self, chat_id: int, is_marked_as_unread: bool
|
||||
) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "toggleChatIsMarkedAsUnread",
|
||||
"chat_id": chat_id,
|
||||
"is_marked_as_unread": is_marked_as_unread,
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def toggle_chat_is_pinned(
|
||||
self, chat_id: int, is_pinned: bool
|
||||
) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "toggleChatIsPinned",
|
||||
"chat_id": chat_id,
|
||||
"is_pinned": is_pinned,
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def set_chat_nottification_settings(
|
||||
self, chat_id: int, notification_settings: dict
|
||||
):
|
||||
data = {
|
||||
"@type": "setChatNotificationSettings",
|
||||
"chat_id": chat_id,
|
||||
"notification_settings": notification_settings,
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def view_messages(
|
||||
self, chat_id: int, message_ids: list, force_read: bool = True
|
||||
) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "viewMessages",
|
||||
"chat_id": chat_id,
|
||||
"message_ids": message_ids,
|
||||
"force_read": force_read,
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def open_message_content(
|
||||
self, chat_id: int, message_id: int
|
||||
) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "openMessageContent",
|
||||
"chat_id": chat_id,
|
||||
"message_id": message_id,
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
|
||||
def main():
|
||||
def signal_handler(sig, frame):
|
||||
log.info("You pressed Ctrl+C!")
|
||||
|
@ -190,7 +39,7 @@ def main():
|
|||
cfg = config.get_cfg()["DEFAULT"]
|
||||
utils.setup_log(cfg.get("level", "DEBUG"))
|
||||
log.debug("#" * 64)
|
||||
tg = TelegramApi(
|
||||
tg = Tdlib(
|
||||
api_id=cfg["api_id"],
|
||||
api_hash=cfg["api_hash"],
|
||||
phone=cfg["phone"],
|
||||
|
|
11
tg/models.py
11
tg/models.py
|
@ -2,15 +2,14 @@ import logging
|
|||
from collections import defaultdict
|
||||
from typing import Any, Dict, List, Optional, Set, Tuple
|
||||
|
||||
from telegram.client import Telegram
|
||||
|
||||
from tg.msg import MsgProxy
|
||||
from tg.tdlib import Tdlib
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Model:
|
||||
def __init__(self, tg: Telegram) -> None:
|
||||
def __init__(self, tg: Tdlib) -> None:
|
||||
self.tg = tg
|
||||
self.chats = ChatModel(tg)
|
||||
self.msgs = MsgModel(tg)
|
||||
|
@ -139,7 +138,7 @@ class Model:
|
|||
|
||||
|
||||
class ChatModel:
|
||||
def __init__(self, tg: Telegram) -> None:
|
||||
def __init__(self, tg: Tdlib) -> None:
|
||||
self.tg = tg
|
||||
self.chats: List[Dict[str, Any]] = []
|
||||
self.chat_ids: List[int] = []
|
||||
|
@ -220,7 +219,7 @@ class ChatModel:
|
|||
|
||||
|
||||
class MsgModel:
|
||||
def __init__(self, tg: Telegram) -> None:
|
||||
def __init__(self, tg: Tdlib) -> None:
|
||||
self.tg = tg
|
||||
self.msgs: Dict[int, List[Dict]] = defaultdict(list)
|
||||
self.current_msgs: Dict[int, int] = defaultdict(int)
|
||||
|
@ -401,7 +400,7 @@ class MsgModel:
|
|||
|
||||
|
||||
class UserModel:
|
||||
def __init__(self, tg: Telegram) -> None:
|
||||
def __init__(self, tg: Tdlib) -> None:
|
||||
self.tg = tg
|
||||
self.me = None
|
||||
self.users: Dict[int, Dict] = {}
|
||||
|
|
152
tg/tdlib.py
Normal file
152
tg/tdlib.py
Normal file
|
@ -0,0 +1,152 @@
|
|||
from telegram.client import AsyncResult, Telegram
|
||||
|
||||
|
||||
class Tdlib(Telegram):
|
||||
|
||||
def download_file(
|
||||
self, file_id, priority=16, offset=0, limit=0, synchronous=False,
|
||||
):
|
||||
result = self.call_method(
|
||||
"downloadFile",
|
||||
params=dict(
|
||||
file_id=file_id,
|
||||
priority=priority,
|
||||
offset=offset,
|
||||
limit=limit,
|
||||
synchronous=synchronous,
|
||||
),
|
||||
block=False,
|
||||
)
|
||||
result.wait()
|
||||
|
||||
def send_doc(self, file_path: str, chat_id: int) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessageDocument",
|
||||
"document": {"@type": "inputFileLocal", "path": file_path},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def send_audio(self, file_path: str, chat_id: int) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessageAudio",
|
||||
"audio": {"@type": "inputFileLocal", "path": file_path},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def send_photo(self, file_path: str, chat_id: int) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessagePhoto",
|
||||
"photo": {"@type": "inputFileLocal", "path": file_path},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def send_video(
|
||||
self,
|
||||
file_path: str,
|
||||
chat_id: int,
|
||||
width: int,
|
||||
height: int,
|
||||
duration: int,
|
||||
) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessageVideo",
|
||||
"width": width,
|
||||
"height": height,
|
||||
"duration": duration,
|
||||
"video": {"@type": "inputFileLocal", "path": file_path},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def send_voice(
|
||||
self, file_path: str, chat_id: int, duration: int, waveform: int
|
||||
):
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessageVoiceNote",
|
||||
"duration": duration,
|
||||
"waveform": waveform,
|
||||
"voice_note": {"@type": "inputFileLocal", "path": file_path},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def edit_message(self, chat_id: int, message_id: int, msg: str):
|
||||
data = {
|
||||
"@type": "editMessageText",
|
||||
"message_id": message_id,
|
||||
"chat_id": chat_id,
|
||||
"input_message_content": {
|
||||
"@type": "inputMessageText",
|
||||
"text": {"@type": "formattedText", "text": msg},
|
||||
},
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def toggle_chat_is_marked_as_unread(
|
||||
self, chat_id: int, is_marked_as_unread: bool
|
||||
) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "toggleChatIsMarkedAsUnread",
|
||||
"chat_id": chat_id,
|
||||
"is_marked_as_unread": is_marked_as_unread,
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def toggle_chat_is_pinned(
|
||||
self, chat_id: int, is_pinned: bool
|
||||
) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "toggleChatIsPinned",
|
||||
"chat_id": chat_id,
|
||||
"is_pinned": is_pinned,
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def set_chat_nottification_settings(
|
||||
self, chat_id: int, notification_settings: dict
|
||||
):
|
||||
data = {
|
||||
"@type": "setChatNotificationSettings",
|
||||
"chat_id": chat_id,
|
||||
"notification_settings": notification_settings,
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def view_messages(
|
||||
self, chat_id: int, message_ids: list, force_read: bool = True
|
||||
) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "viewMessages",
|
||||
"chat_id": chat_id,
|
||||
"message_ids": message_ids,
|
||||
"force_read": force_read,
|
||||
}
|
||||
return self._send_data(data)
|
||||
|
||||
def open_message_content(
|
||||
self, chat_id: int, message_id: int
|
||||
) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "openMessageContent",
|
||||
"chat_id": chat_id,
|
||||
"message_id": message_id,
|
||||
}
|
||||
return self._send_data(data)
|
Loading…
Add table
Reference in a new issue