Skip to content

Commit d2ab184

Browse files
committed
VK Groups info
1 parent 12452c5 commit d2ab184

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

social/routes/group.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from social.models.group import Group
1212
from social.settings import get_settings
1313
from social.utils.telegram_groups import update_tg_chat
14-
from social.utils.vk_groups import update_vk_chat
14+
from social.utils.vk_groups import update_vk_chat, update_vk_group
1515

1616

1717
router = APIRouter(prefix="/group", tags=['User defined groups'])
@@ -28,6 +28,7 @@ class GroupGet(BaseModel):
2828
id: int
2929
owner_id: int | None = None
3030
name: str | None = None
31+
type: str | None = None
3132
description: str | None = None
3233
invite_link: str | None = None
3334

@@ -119,6 +120,8 @@ def update_group_info(
119120
if patch_info.update_from_source:
120121
if group.type == "vk_chat":
121122
update_vk_chat(group)
123+
elif group.type == "vk_group":
124+
update_vk_group(group)
122125
elif group.type == "tg_chat" or group.type == "tg_channel":
123126
update_tg_chat(group)
124127

social/utils/vk_groups.py

+42-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import requests
55
from fastapi_sqlalchemy import db
66

7-
from social.models import CreateGroupRequest, VkChat
7+
from social.models import CreateGroupRequest, VkChat, VkGroup
88
from social.settings import get_settings
99

1010

@@ -14,7 +14,7 @@
1414

1515
def get_chat_info(peer_id) -> dict:
1616
"""Получить название чата ВК"""
17-
conversation = requests.post(
17+
resp = requests.post(
1818
"https://api.vk.com/method/messages.getConversationsById",
1919
json={
2020
"peer_ids": peer_id,
@@ -24,25 +24,47 @@ def get_chat_info(peer_id) -> dict:
2424
},
2525
)
2626
try:
27-
return conversation.json()["response"]["items"][0]["chat_settings"]
27+
conversation = resp.json()
28+
logger.info("Chat info: %s", conversation)
29+
return conversation["response"]["items"][0]["chat_settings"]
2830
except Exception as exc:
2931
logger.exception(exc)
3032
return None
3133

3234

3335
def get_chat_invite_link(peer_id):
3436
"""Получить название чата ВК"""
35-
conversation = requests.post(
37+
resp = requests.post(
3638
"https://api.vk.com/method/messages.getInviteLink",
37-
json={
39+
data={
3840
"peer_ids": peer_id,
3941
"group_id": settings.VK_BOT_GROUP_ID,
4042
"access_token": settings.VK_BOT_TOKEN,
4143
"v": 5.199,
4244
},
4345
)
4446
try:
45-
return conversation.json()["response"]["link"]
47+
link = resp.json()
48+
logger.info("Chat link: %s", link)
49+
return link["response"]["link"]
50+
except Exception as exc:
51+
logger.exception(exc)
52+
return None
53+
54+
55+
def get_group_info(group_id) -> dict:
56+
"""Получить название чата ВК"""
57+
groups = requests.post(
58+
"https://api.vk.com/method/groups.getById",
59+
data={
60+
"group_id": group_id,
61+
"access_token": settings.VK_BOT_TOKEN,
62+
"fields": "description",
63+
"v": 5.199,
64+
},
65+
)
66+
try:
67+
return groups.json()["response"]["groups"][0]
4668
except Exception as exc:
4769
logger.exception(exc)
4870
return None
@@ -84,12 +106,22 @@ def approve_vk_chat(request_data: dict[str]):
84106
logger.info("VK group %d validated (secret=%s)", group.id, text)
85107

86108

87-
def update_vk_chat(group: VkChat):
109+
def update_vk_chat(chat: VkChat):
88110
"""Обновляет информацию о группе ВК"""
89-
chat_info = get_chat_info(group.peer_id)
90-
chat_invite = get_chat_invite_link(group.peer_id)
111+
chat_info = get_chat_info(chat.peer_id)
112+
chat_invite = get_chat_invite_link(chat.peer_id)
91113
logger.info("Chat info: %s, invite: %s", chat_info, chat_invite)
114+
chat.name = chat_info.get("title")
115+
chat.description = chat_info.get("description")
116+
chat.invite_link = chat_invite
117+
return chat
118+
119+
120+
def update_vk_group(group: VkGroup):
121+
"""Обновляет информацию о группе ВК"""
122+
chat_info = get_group_info(group.group_id)
123+
logger.info("Chat info: %s", chat_info)
92124
group.name = chat_info.get("title")
93125
group.description = chat_info.get("description")
94-
group.invite_link = chat_invite
126+
group.invite_link = f"https://vk.com/public{group.group_id}"
95127
return group

0 commit comments

Comments
 (0)