Skip to content

Commit 0e1dc95

Browse files
committed
VK Groups info
1 parent 12452c5 commit 0e1dc95

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
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

+47-17
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,35 +14,55 @@
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",
19-
json={
19+
data={
2020
"peer_ids": peer_id,
21-
"group_id": settings.VK_BOT_GROUP_ID,
2221
"access_token": settings.VK_BOT_TOKEN,
2322
"v": 5.199,
2423
},
2524
)
2625
try:
27-
return conversation.json()["response"]["items"][0]["chat_settings"]
26+
conversation = resp.json()
27+
logger.debug("Chat info: %s", conversation)
28+
return conversation["response"]["items"][0]["chat_settings"]
2829
except Exception as exc:
2930
logger.exception(exc)
3031
return None
3132

3233

3334
def get_chat_invite_link(peer_id):
3435
"""Получить название чата ВК"""
35-
conversation = requests.post(
36+
resp = requests.post(
3637
"https://api.vk.com/method/messages.getInviteLink",
37-
json={
38-
"peer_ids": peer_id,
39-
"group_id": settings.VK_BOT_GROUP_ID,
38+
data={
39+
"peer_id": peer_id,
40+
"access_token": settings.VK_BOT_TOKEN,
41+
"v": 5.199,
42+
},
43+
)
44+
try:
45+
link = resp.json()
46+
logger.debug("Chat link: %s", link)
47+
return link["response"]["link"]
48+
except Exception as exc:
49+
logger.exception(exc)
50+
return None
51+
52+
53+
def get_group_info(group_id) -> dict:
54+
"""Получить название чата ВК"""
55+
groups = requests.post(
56+
"https://api.vk.com/method/groups.getById",
57+
data={
58+
"group_id": group_id,
4059
"access_token": settings.VK_BOT_TOKEN,
60+
"fields": "description",
4161
"v": 5.199,
4262
},
4363
)
4464
try:
45-
return conversation.json()["response"]["link"]
65+
return groups.json()["response"]["groups"][0]
4666
except Exception as exc:
4767
logger.exception(exc)
4868
return None
@@ -84,12 +104,22 @@ def approve_vk_chat(request_data: dict[str]):
84104
logger.info("VK group %d validated (secret=%s)", group.id, text)
85105

86106

87-
def update_vk_chat(group: VkChat):
107+
def update_vk_chat(chat: VkChat):
108+
"""Обновляет информацию о чате ВК"""
109+
chat_info = get_chat_info(chat.peer_id)
110+
chat_invite = get_chat_invite_link(chat.peer_id)
111+
logger.debug("Chat info: %s, invite: %s", chat_info, chat_invite)
112+
chat.name = chat_info.get("title")
113+
chat.description = chat_info.get("description")
114+
chat.invite_link = chat_invite
115+
return chat
116+
117+
118+
def update_vk_group(group: VkGroup):
88119
"""Обновляет информацию о группе ВК"""
89-
chat_info = get_chat_info(group.peer_id)
90-
chat_invite = get_chat_invite_link(group.peer_id)
91-
logger.info("Chat info: %s, invite: %s", chat_info, chat_invite)
92-
group.name = chat_info.get("title")
93-
group.description = chat_info.get("description")
94-
group.invite_link = chat_invite
120+
group_info = get_group_info(group.group_id)
121+
logger.debug("Group info: %s", group_info)
122+
group.name = group_info.get("name")
123+
group.description = group_info.get("description")
124+
group.invite_link = f"https://vk.com/public{group.group_id}"
95125
return group

0 commit comments

Comments
 (0)