-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from Studio-Yandex-Practicum/feature/add_webhook
feature/add_webhook
- Loading branch information
Showing
4 changed files
with
46 additions
and
3 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
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,23 @@ | ||
from fastapi import APIRouter, Request | ||
from telegram import Update | ||
|
||
from src.core.exceptions.exceptions import UnauthorizedError, WebhookOnError | ||
from src.settings import settings | ||
|
||
telegram_webhook_router = APIRouter() | ||
|
||
|
||
@telegram_webhook_router.post( | ||
"/webhook", | ||
description="Получить обновления telegram.", | ||
) | ||
async def get_telegram_bot_updates(request: Request) -> None: | ||
"""Получение обновлений telegram в режиме работы бота webhook.""" | ||
if not settings.BOT_WEBHOOK_MODE: | ||
raise WebhookOnError | ||
secret_token = request.headers.get("x-telegram-bot-api-secret-token") | ||
if secret_token != settings.SECRET_KEY: | ||
raise UnauthorizedError | ||
bot_instance = request.app.state.bot_instance | ||
request_json_data = await request.json() | ||
await bot_instance.update_queue.put(Update.de_json(data=request_json_data, bot=bot_instance.bot)) |
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 |
---|---|---|
@@ -1,11 +1,19 @@ | ||
from fastapi import APIRouter | ||
|
||
from src.api.endpoints import analytic_router, category_router, notification_router, site_user_router, task_router | ||
from src.api.endpoints import ( | ||
analytic_router, | ||
category_router, | ||
notification_router, | ||
site_user_router, | ||
task_router, | ||
telegram_webhook_router, | ||
) | ||
from src.settings import settings | ||
|
||
api_router = APIRouter(prefix=settings.ROOT_PATH) | ||
api_router.include_router(category_router, prefix="/categories", tags=["Categories"]) | ||
api_router.include_router(analytic_router, prefix="/analytics", tags=["Analytic"]) | ||
api_router.include_router(task_router, prefix="/tasks", tags=["Tasks"]) | ||
api_router.include_router(category_router, prefix="/categories", tags=["Categories"]) | ||
api_router.include_router(notification_router, prefix="/messages", tags=["Messages"]) | ||
api_router.include_router(site_user_router, prefix="/external_user_registration", tags=["ExternalSiteUser"]) | ||
api_router.include_router(task_router, prefix="/tasks", tags=["Tasks"]) | ||
api_router.include_router(telegram_webhook_router, prefix="/telegram", tags=["Telegram"]) |
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