-
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.
## Изменения Добавил возможность читать сообщения от ВК ## Детали Для добавления ВК группы нужно создать ее через АПИ, передав секретный ключ, после чего сохранить настройки в группе в интерфейсе ВК ## Check-List <!-- Проставьте x в квадратные скобки в нужных пунктах. Example: [x] --> - [x] Вы проверили свой код перед отправкой запроса? - [ ] Вы написали тесты к реализованным функциям? - [x] Вы не забыли применить форматирование `black` и `isort` для _Back-End_ или `Prettier` для _Front-End_?
- Loading branch information
Showing
10 changed files
with
142 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""vk | ||
Revision ID: 9d98c1e9c864 | ||
Revises: 57c72962d2b4 | ||
Create Date: 2023-08-19 15:53:19.787309 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '9d98c1e9c864' | ||
down_revision = '57c72962d2b4' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table('vk_groups', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('group_id', sa.Integer(), nullable=False), | ||
sa.Column('confirmation_token', sa.String(), nullable=False), | ||
sa.Column('secret_key', sa.String(), nullable=False), | ||
sa.Column('create_ts', sa.DateTime(), nullable=False), | ||
sa.Column('update_ts', sa.DateTime(), nullable=False), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table('vk_groups') |
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 |
---|---|---|
|
@@ -25,7 +25,7 @@ def get_application(): | |
|
||
# Хэндлеры конкретных чатов | ||
register_handlers(app) | ||
|
||
return app | ||
|
||
|
||
|
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,5 @@ | ||
from .vk import VkGroups | ||
from .webhook_storage import WebhookStorage | ||
|
||
|
||
__all__ = ['WebhookStorage', 'VkGroups'] |
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,15 @@ | ||
from datetime import datetime | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
|
||
from .base import Base | ||
|
||
|
||
class VkGroups(Base): | ||
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True) | ||
group_id: Mapped[int] = mapped_column(sa.Integer) | ||
confirmation_token: Mapped[str] = mapped_column(sa.String) | ||
secret_key: Mapped[str] = mapped_column(sa.String) | ||
create_ts: Mapped[datetime] = mapped_column(sa.DateTime, default=datetime.utcnow) | ||
update_ts: Mapped[datetime] = mapped_column(sa.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) |
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,77 @@ | ||
import logging | ||
|
||
from auth_lib.fastapi import UnionAuth | ||
from fastapi import APIRouter, Depends, Request | ||
from fastapi.responses import PlainTextResponse | ||
from fastapi_sqlalchemy import db | ||
from pydantic import BaseModel, ConfigDict | ||
|
||
from social.handlers_telegram import get_application | ||
from social.models.vk import VkGroups | ||
from social.models.webhook_storage import WebhookStorage, WebhookSystems | ||
from social.settings import get_settings | ||
from social.utils.string import random_string | ||
|
||
|
||
router = APIRouter(prefix="/vk", tags=['vk']) | ||
settings = get_settings() | ||
logger = logging.getLogger(__name__) | ||
application = get_application() | ||
|
||
|
||
class VkGroupCreate(BaseModel): | ||
confirmation_token: str | ||
change_secret_key: bool = False | ||
|
||
|
||
class VkGroupCreateResponse(BaseModel): | ||
group_id: int | ||
secret_key: str | ||
|
||
model_config = ConfigDict(from_attributes=True) | ||
|
||
|
||
@router.post('', tags=["webhooks"]) | ||
async def vk_webhook(request: Request): | ||
"""Принимает любой POST запрос от VK""" | ||
request_data = await request.json() | ||
logger.debug(request_data) | ||
group_id = request_data["group_id"] # Fail if no group | ||
group = db.session.query(VkGroups).where(VkGroups.group_id == group_id).one() # Fail if no settings | ||
|
||
# Проверка на создание нового вебхука со страничка ВК | ||
if request_data.get("type", "") == "confirmation": | ||
return PlainTextResponse(group.confirmation_token) | ||
|
||
if request_data.get("secret") != group.secret_key: | ||
raise Exception("Not a valid secret") | ||
|
||
db.session.add( | ||
WebhookStorage( | ||
system=WebhookSystems.VK, | ||
message=request_data, | ||
) | ||
) | ||
db.session.commit() | ||
|
||
return | ||
|
||
|
||
@router.put('/{group_id}') | ||
def create_or_replace_group( | ||
group_id: int, group_info: VkGroupCreate, _=Depends(UnionAuth(["social.vk_group.create"])) | ||
) -> VkGroupCreateResponse: | ||
group = db.session.query(VkGroups).where(VkGroups.group_id == group_id).one_or_none() | ||
if group is None: | ||
group = VkGroups() | ||
db.session.add(group) | ||
group.group_id = group_id | ||
group.secret_key = random_string(32) | ||
|
||
if group_info.change_secret_key: | ||
group.secret_key = random_string(32) | ||
|
||
group.confirmation_token = group_info.confirmation_token | ||
|
||
db.session.commit() | ||
return group |
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,6 @@ | ||
import random | ||
import string | ||
|
||
|
||
def random_string(N: int): | ||
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=N)) |