Skip to content

Commit 4ec3941

Browse files
committed
VK
1 parent 2118861 commit 4ec3941

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

migrations/versions/1cacaf803a1d_user_defined_groups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def upgrade():
6666
op.create_table(
6767
'vk_chat',
6868
sa.Column('id', sa.Integer(), nullable=False),
69-
sa.Column('chat_id', sa.Integer(), nullable=False),
69+
sa.Column('peer_id', sa.Integer(), nullable=False),
7070
sa.ForeignKeyConstraint(
7171
['id'],
7272
['group.id'],

social/models/group.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class VkGroup(Group):
3535

3636
class VkChat(Group):
3737
id: Mapped[int] = mapped_column(sa.ForeignKey("group.id"), primary_key=True)
38-
chat_id: Mapped[int]
38+
peer_id: Mapped[int]
3939

4040
__mapper_args__ = {
4141
"polymorphic_identity": "vk_chat",

social/routes/vk.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from datetime import UTC, datetime
23

34
from auth_lib.fastapi import UnionAuth
45
from fastapi import APIRouter, Depends, Request
@@ -7,7 +8,7 @@
78
from pydantic import BaseModel, ConfigDict
89

910
from social.handlers_telegram import get_application
10-
from social.models.group import VkGroup
11+
from social.models.group import VkChat, VkGroup
1112
from social.models.webhook_storage import WebhookStorage, WebhookSystems
1213
from social.settings import get_settings
1314
from social.utils.string import random_string
@@ -54,6 +55,27 @@ async def vk_webhook(request: Request) -> str:
5455
)
5556
db.session.commit()
5657

58+
if request_data.get("type") == "message_new":
59+
# Получение сообщения в чате ВК
60+
try:
61+
peer_id = request_data["object"]["message"]["peer_id"]
62+
obj = db.session.query(VkChat).where(VkChat.peer_id == peer_id).one_or_none()
63+
if obj is None:
64+
# Надо будет добавлять название группы
65+
# conversation = requests.post("https://api.vk.com/method/messages.getConversationsById", json={
66+
# "peer_ids": peer_id,
67+
# "group_id": 222099060,
68+
# "access_token": settings.VK_BOT_TOKEN,
69+
# "v": 5.199,
70+
# })
71+
# chat_title = conversation["response"]["items"][0]["chat_settings"]["title"]
72+
obj = VkChat(chat_id=peer_id)
73+
db.session.add(obj)
74+
obj.last_active_ts = datetime.now(UTC)
75+
db.session.commit()
76+
except Exception as exc:
77+
logger.exception(exc)
78+
5779
return PlainTextResponse('ok')
5880

5981

social/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Settings(BaseSettings):
2020

2121
TELEGRAM_BOT_TOKEN: str | None = None
2222

23+
VK_BOT_TOKEN: str | None = None
24+
2325
GITHUB_APP_ID: int | None = None
2426
GITHUB_WEBHOOK_SECRET: str | None = None
2527
GITHUB_PRIVATE_KEY: str | None = None

0 commit comments

Comments
 (0)