[telegram] Add public telegram endpoint to list all bots to a hood

This commit is contained in:
Cathy Hu 2020-09-03 17:38:12 +02:00
parent d8c138982d
commit d8a270573e

View file

@ -7,7 +7,7 @@ from aiogram import exceptions
from fastapi import APIRouter, Depends, HTTPException, Response, status from fastapi import APIRouter, Depends, HTTPException, Response, status
from kibicara.platforms.telegram.bot import spawner from kibicara.platforms.telegram.bot import spawner
from kibicara.platforms.telegram.model import Telegram from kibicara.platforms.telegram.model import Telegram
from kibicara.webapi.hoods import get_hood from kibicara.webapi.hoods import get_hood, get_hood_unauthorized
from logging import getLogger from logging import getLogger
from sqlite3 import IntegrityError from sqlite3 import IntegrityError
from ormantic.exceptions import NoMatch from ormantic.exceptions import NoMatch
@ -30,6 +30,10 @@ class BodyTelegram(BaseModel):
raise ValueError(e) raise ValueError(e)
class BodyTelegramPublic(BaseModel):
username: str
async def get_telegram(telegram_id: int, hood=Depends(get_hood)): async def get_telegram(telegram_id: int, hood=Depends(get_hood)):
try: try:
return await Telegram.objects.get(id=telegram_id, hood=hood) return await Telegram.objects.get(id=telegram_id, hood=hood)
@ -41,6 +45,20 @@ router = APIRouter()
telegram_callback_router = APIRouter() telegram_callback_router = APIRouter()
@router.get(
'/public',
# TODO response_model,
operation_id='get_telegrams_public',
)
async def telegram_read_all_public(hood=Depends(get_hood_unauthorized)):
telegrambots = await Telegram.objects.filter(hood=hood).all()
return [
BodyTelegramPublic(username=telegrambot.username)
for telegrambot in telegrambots
if telegrambot.enabled == 1
]
@router.get( @router.get(
'/', '/',
# TODO response_model, # TODO response_model,