Show user info

This commit is contained in:
Paul Nameless 2020-07-22 20:39:57 +08:00
parent 50b93eb308
commit fb11dafb38
3 changed files with 67 additions and 2 deletions

View file

@ -13,7 +13,7 @@ from telegram.utils import AsyncResult
from tg import config
from tg.models import Model
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 (
get_duration,
get_mime,
@ -76,6 +76,35 @@ class Controller:
self.tg = tg
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"])
def open_url(self) -> None:
msg = MsgProxy(self.model.current_msg)

View file

@ -608,12 +608,27 @@ class UserModel:
return True
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]:
if user_id in self.not_found:
return {}
if user_id in self.users:
return self.users[user_id]
result = self.tg.call_method("getUser", {"user_id": user_id})
result = self.tg.get_user(user_id)
result.wait()
if result.error:
log.warning(f"get user error: {result.error_info}")

View file

@ -37,6 +37,13 @@ class UserStatus(Enum):
userStatusLastMonth = "last month"
class UserType(Enum):
userTypeRegular = ""
userTypeDeleted = "deleted"
userTypeBot = "bot"
userTypeUnknown = "unknownn"
class TextParseModeInput(Enum):
textParseModeMarkdown = "markdown"
textParseModeHTML = "html"
@ -366,6 +373,20 @@ class Tdlib(Telegram):
}
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]:
try: