-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
115 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import logging | ||
from collections.abc import Callable | ||
|
||
from social.utils.events import EventProcessor | ||
from social.utils.vk_groups import approve_vk_chat | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
EVENT_PROCESSORS: list[EventProcessor] = [] | ||
|
||
|
||
def event(**filters: str): | ||
"""Помечает функцию как обработчик событий, задает фильтры для запуска""" | ||
|
||
def deco(func: Callable): | ||
EVENT_PROCESSORS.append(EventProcessor(filters, func)) | ||
return func | ||
|
||
return deco | ||
|
||
|
||
def process_event(event: dict): | ||
for processor in EVENT_PROCESSORS: | ||
if processor.check_and_process(event): | ||
break | ||
else: | ||
logger.debug("Event without processor") | ||
|
||
|
||
@event( | ||
type="message_new", | ||
object=lambda i: i.get("message", {}).get("text", "").startswith("/validate"), | ||
) | ||
def validate_group(event: dict): | ||
approve_vk_chat(event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
from datetime import UTC, datetime | ||
|
||
import requests | ||
from fastapi_sqlalchemy import db | ||
|
||
from social.models import CreateGroupRequest, VkChat | ||
from social.settings import get_settings | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
settings = get_settings() | ||
|
||
|
||
def get_chat_name(peer_id): | ||
"""Получить название чата ВК""" | ||
conversation = requests.post( | ||
"https://api.vk.com/method/messages.getConversationsById", | ||
json={ | ||
"peer_ids": peer_id, | ||
"group_id": settings.VK_BOT_GROUP_ID, | ||
"access_token": settings.VK_BOT_TOKEN, | ||
"v": 5.199, | ||
}, | ||
) | ||
try: | ||
return conversation["response"]["items"][0]["chat_settings"]["title"] | ||
except Exception as exc: | ||
logger.exception(exc) | ||
return None | ||
|
||
|
||
def create_vk_chat(request_data: dict[str]): | ||
"""Создает запись о вк чате в внутренней БД приложения или возвращает существующий""" | ||
if ( | ||
request_data.get("group_id") == settings.VK_BOT_GROUP_ID # peer_id чатов уникальные для каждого пользователя ВК | ||
and request_data.get("type") == "message_new" | ||
): | ||
# Получение сообщения в чате ВК | ||
try: | ||
peer_id = request_data["object"]["message"]["peer_id"] | ||
obj = db.session.query(VkChat).where(VkChat.peer_id == peer_id).one_or_none() | ||
if obj is None: | ||
obj = VkChat(peer_id=peer_id) | ||
db.session.add(obj) | ||
obj.last_active_ts = datetime.now(UTC) | ||
db.session.commit() | ||
except Exception as exc: | ||
logger.exception(exc) | ||
return obj | ||
return None | ||
|
||
|
||
def approve_vk_chat(request_data: dict[str]): | ||
"""Если получено сообщение команды /validate, то за группой закрепляется владелец""" | ||
logger.debug("Validation started") | ||
group = create_vk_chat(request_data) | ||
text = request_data.get("object", {}).get("message", {}).get("text", "").removeprefix("/validate").strip() | ||
if not text or not group or group.owner_id is not None: | ||
logger.error("Telegram group not validated (secret=%s, group=%s)", text, group) | ||
return | ||
request = db.session.query(CreateGroupRequest).where(CreateGroupRequest.secret_key == text).one_or_none() | ||
request.mapped_group_id = group.id | ||
group.owner_id = request.owner_id | ||
db.session.commit() | ||
logger.info("VK group %d validated (secret=%s)", group.id, text) |