mirror of
https://github.com/paul-nameless/tg
synced 2025-02-16 18:48:24 +00:00
Show user info
This commit is contained in:
parent
50b93eb308
commit
fb11dafb38
3 changed files with 67 additions and 2 deletions
|
@ -13,7 +13,7 @@ from telegram.utils import AsyncResult
|
||||||
from tg import config
|
from tg import config
|
||||||
from tg.models import Model
|
from tg.models import Model
|
||||||
from tg.msg import MsgProxy
|
from tg.msg import MsgProxy
|
||||||
from tg.tdlib import ChatAction, ChatType, Tdlib, get_chat_type
|
from tg.tdlib import ChatAction, ChatType, Tdlib, UserType, get_chat_type
|
||||||
from tg.utils import (
|
from tg.utils import (
|
||||||
get_duration,
|
get_duration,
|
||||||
get_mime,
|
get_mime,
|
||||||
|
@ -76,6 +76,35 @@ class Controller:
|
||||||
self.tg = tg
|
self.tg = tg
|
||||||
self.chat_size = 0.5
|
self.chat_size = 0.5
|
||||||
|
|
||||||
|
@bind(msg_handler, ["u"])
|
||||||
|
def user_innfo(self) -> None:
|
||||||
|
"""Show user profile"""
|
||||||
|
msg = MsgProxy(self.model.current_msg)
|
||||||
|
user_id = msg.sender_id
|
||||||
|
users = self.model.users
|
||||||
|
name = users.get_user_label(user_id)
|
||||||
|
status = users.get_status(user_id)
|
||||||
|
user = users.get_user(user_id)
|
||||||
|
user_info = users.get_user_full_info(user_id)
|
||||||
|
user_type = None
|
||||||
|
try:
|
||||||
|
user_type = UserType[user["type"]["@type"]].value
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
info = {
|
||||||
|
name: status,
|
||||||
|
"Username": user.get("username", ""),
|
||||||
|
"UserId": user_id,
|
||||||
|
"Bio": user_info.get("bio", ""),
|
||||||
|
"Phone": user.get("phone_number", ""),
|
||||||
|
"Type": user_type,
|
||||||
|
}
|
||||||
|
with suspend(self.view) as s:
|
||||||
|
s.run_with_input(
|
||||||
|
config.VIEW_TEXT_CMD,
|
||||||
|
"\n".join(f"{k}: {v}" for k, v in info.items() if v),
|
||||||
|
)
|
||||||
|
|
||||||
@bind(msg_handler, ["o"])
|
@bind(msg_handler, ["o"])
|
||||||
def open_url(self) -> None:
|
def open_url(self) -> None:
|
||||||
msg = MsgProxy(self.model.current_msg)
|
msg = MsgProxy(self.model.current_msg)
|
||||||
|
|
17
tg/models.py
17
tg/models.py
|
@ -608,12 +608,27 @@ class UserModel:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def get_user_full_info(self, user_id: int) -> Dict[str, Any]:
|
||||||
|
user = self.get_user(user_id)
|
||||||
|
if not user:
|
||||||
|
return user
|
||||||
|
if user.get("full_info"):
|
||||||
|
return user["full_info"]
|
||||||
|
|
||||||
|
result = self.tg.get_user_full_info(user_id)
|
||||||
|
result.wait()
|
||||||
|
if result.error:
|
||||||
|
log.warning(f"get user full info error: {result.error_info}")
|
||||||
|
return {}
|
||||||
|
user["full_info"] = result.update
|
||||||
|
return result.update
|
||||||
|
|
||||||
def get_user(self, user_id: int) -> Dict[str, Any]:
|
def get_user(self, user_id: int) -> Dict[str, Any]:
|
||||||
if user_id in self.not_found:
|
if user_id in self.not_found:
|
||||||
return {}
|
return {}
|
||||||
if user_id in self.users:
|
if user_id in self.users:
|
||||||
return self.users[user_id]
|
return self.users[user_id]
|
||||||
result = self.tg.call_method("getUser", {"user_id": user_id})
|
result = self.tg.get_user(user_id)
|
||||||
result.wait()
|
result.wait()
|
||||||
if result.error:
|
if result.error:
|
||||||
log.warning(f"get user error: {result.error_info}")
|
log.warning(f"get user error: {result.error_info}")
|
||||||
|
|
21
tg/tdlib.py
21
tg/tdlib.py
|
@ -37,6 +37,13 @@ class UserStatus(Enum):
|
||||||
userStatusLastMonth = "last month"
|
userStatusLastMonth = "last month"
|
||||||
|
|
||||||
|
|
||||||
|
class UserType(Enum):
|
||||||
|
userTypeRegular = ""
|
||||||
|
userTypeDeleted = "deleted"
|
||||||
|
userTypeBot = "bot"
|
||||||
|
userTypeUnknown = "unknownn"
|
||||||
|
|
||||||
|
|
||||||
class TextParseModeInput(Enum):
|
class TextParseModeInput(Enum):
|
||||||
textParseModeMarkdown = "markdown"
|
textParseModeMarkdown = "markdown"
|
||||||
textParseModeHTML = "html"
|
textParseModeHTML = "html"
|
||||||
|
@ -366,6 +373,20 @@ class Tdlib(Telegram):
|
||||||
}
|
}
|
||||||
return self._send_data(data)
|
return self._send_data(data)
|
||||||
|
|
||||||
|
def get_user(self, user_id: int) -> AsyncResult:
|
||||||
|
data = {
|
||||||
|
"@type": "getUser",
|
||||||
|
"user_id": user_id,
|
||||||
|
}
|
||||||
|
return self._send_data(data)
|
||||||
|
|
||||||
|
def get_user_full_info(self, user_id: int) -> AsyncResult:
|
||||||
|
data = {
|
||||||
|
"@type": "getUserFullInfo",
|
||||||
|
"user_id": user_id,
|
||||||
|
}
|
||||||
|
return self._send_data(data)
|
||||||
|
|
||||||
|
|
||||||
def get_chat_type(chat: Dict[str, Any]) -> Optional[ChatType]:
|
def get_chat_type(chat: Dict[str, Any]) -> Optional[ChatType]:
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Add table
Reference in a new issue