|
4 | 4 | import requests
|
5 | 5 | from fastapi_sqlalchemy import db
|
6 | 6 |
|
7 |
| -from social.models import CreateGroupRequest, VkChat |
| 7 | +from social.models import CreateGroupRequest, VkChat, VkGroup |
8 | 8 | from social.settings import get_settings
|
9 | 9 |
|
10 | 10 |
|
|
14 | 14 |
|
15 | 15 | def get_chat_info(peer_id) -> dict:
|
16 | 16 | """Получить название чата ВК"""
|
17 |
| - conversation = requests.post( |
| 17 | + resp = requests.post( |
18 | 18 | "https://api.vk.com/method/messages.getConversationsById",
|
19 |
| - json={ |
| 19 | + data={ |
20 | 20 | "peer_ids": peer_id,
|
21 |
| - "group_id": settings.VK_BOT_GROUP_ID, |
22 | 21 | "access_token": settings.VK_BOT_TOKEN,
|
23 | 22 | "v": 5.199,
|
24 | 23 | },
|
25 | 24 | )
|
26 | 25 | 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"] |
28 | 29 | except Exception as exc:
|
29 | 30 | logger.exception(exc)
|
30 | 31 | return None
|
31 | 32 |
|
32 | 33 |
|
33 | 34 | def get_chat_invite_link(peer_id):
|
34 | 35 | """Получить название чата ВК"""
|
35 |
| - conversation = requests.post( |
| 36 | + resp = requests.post( |
36 | 37 | "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, |
40 | 59 | "access_token": settings.VK_BOT_TOKEN,
|
| 60 | + "fields": "description", |
41 | 61 | "v": 5.199,
|
42 | 62 | },
|
43 | 63 | )
|
44 | 64 | try:
|
45 |
| - return conversation.json()["response"]["link"] |
| 65 | + return groups.json()["response"]["groups"][0] |
46 | 66 | except Exception as exc:
|
47 | 67 | logger.exception(exc)
|
48 | 68 | return None
|
@@ -84,12 +104,22 @@ def approve_vk_chat(request_data: dict[str]):
|
84 | 104 | logger.info("VK group %d validated (secret=%s)", group.id, text)
|
85 | 105 |
|
86 | 106 |
|
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): |
88 | 119 | """Обновляет информацию о группе ВК"""
|
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}" |
95 | 125 | return group
|
0 commit comments