diff --git a/social/utils/telegram_groups.py b/social/utils/telegram_groups.py index f58eecc..b23f6dd 100644 --- a/social/utils/telegram_groups.py +++ b/social/utils/telegram_groups.py @@ -14,10 +14,15 @@ def get_chat_info(id: int) -> dict: - return requests.post( + resp = requests.post( f'https://api.telegram.org/bot{settings.TELEGRAM_BOT_TOKEN}/getChat', json={'chat_id': id}, - ).json() + ) + if not resp.ok: + logger.error(resp.text) + return {} + else: + return resp.json() def create_telegram_group(update: Update): @@ -59,8 +64,14 @@ def approve_telegram_group(update: Update): logger.info("Telegram group %d validated (secret=%s)", group.id, text) -def update_tg_chat(group: TelegramChat): - chat_info = get_chat_info(group.chat_id) +def update_tg_chat(group: TelegramChat | TelegramChannel): + if isinstance(group, TelegramChat): + chat_info = get_chat_info(group.chat_id) + elif isinstance(group, TelegramChannel): + chat_info = get_chat_info(group.channel_id) + else: + raise TypeError("Only TelegramChat and TelegramChannel are supported") + logger.info("TG chat info: %s", chat_info) group.name = chat_info.get("title") group.description = chat_info.get("description") group.invite_link = chat_info.get("invite_link") diff --git a/social/utils/vk_groups.py b/social/utils/vk_groups.py index 02f703e..680bb1e 100644 --- a/social/utils/vk_groups.py +++ b/social/utils/vk_groups.py @@ -12,7 +12,7 @@ settings = get_settings() -def get_chat_info(peer_id): +def get_chat_info(peer_id) -> dict: """Получить название чата ВК""" conversation = requests.post( "https://api.vk.com/method/messages.getConversationsById", @@ -87,7 +87,9 @@ def approve_vk_chat(request_data: dict[str]): def update_vk_chat(group: VkChat): """Обновляет информацию о группе ВК""" chat_info = get_chat_info(group.peer_id) + chat_invite = get_chat_invite_link(group.peer_id) + logger.info("Chat info: %s, invite: %s", chat_info, chat_invite) group.name = chat_info.get("title") group.description = chat_info.get("description") - group.invite_link = get_chat_invite_link(group.peer_id) + group.invite_link = chat_invite return group