2020-07-10 21:53:24 +00:00
|
|
|
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
|
2020-09-28 22:39:32 +00:00
|
|
|
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
|
2020-07-10 21:53:24 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-07-17 13:07:24 +00:00
|
|
|
from aiogram.bot.api import check_token
|
|
|
|
from aiogram import exceptions
|
2020-07-10 21:53:24 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
|
|
|
from kibicara.platforms.telegram.bot import spawner
|
2020-09-06 17:11:18 +00:00
|
|
|
from kibicara.platforms.telegram.model import Telegram, TelegramUser
|
2020-09-03 15:38:12 +00:00
|
|
|
from kibicara.webapi.hoods import get_hood, get_hood_unauthorized
|
2020-07-10 21:53:24 +00:00
|
|
|
from logging import getLogger
|
2020-07-17 17:59:25 +00:00
|
|
|
from sqlite3 import IntegrityError
|
2020-07-10 21:53:24 +00:00
|
|
|
from ormantic.exceptions import NoMatch
|
2020-07-17 13:07:24 +00:00
|
|
|
from pydantic import BaseModel, validator
|
2020-07-10 21:53:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class BodyTelegram(BaseModel):
|
|
|
|
api_token: str
|
|
|
|
welcome_message: str = 'Welcome!'
|
|
|
|
|
2020-07-17 13:07:24 +00:00
|
|
|
@validator('api_token')
|
|
|
|
def valid_api_token(cls, value):
|
|
|
|
try:
|
|
|
|
check_token(value)
|
|
|
|
return value
|
|
|
|
except exceptions.ValidationError as e:
|
|
|
|
raise ValueError(e)
|
|
|
|
|
2020-07-10 21:53:24 +00:00
|
|
|
|
2020-09-03 15:38:12 +00:00
|
|
|
class BodyTelegramPublic(BaseModel):
|
|
|
|
username: str
|
|
|
|
|
|
|
|
|
2020-07-10 21:53:24 +00:00
|
|
|
async def get_telegram(telegram_id: int, hood=Depends(get_hood)):
|
|
|
|
try:
|
|
|
|
return await Telegram.objects.get(id=telegram_id, hood=hood)
|
|
|
|
except NoMatch:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
telegram_callback_router = APIRouter()
|
|
|
|
|
|
|
|
|
2020-09-03 15:38:12 +00:00
|
|
|
@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
|
2020-09-10 17:29:57 +00:00
|
|
|
if telegrambot.enabled == 1 and telegrambot.username
|
2020-09-03 15:38:12 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.get(
|
|
|
|
'/',
|
|
|
|
# TODO response_model,
|
2020-07-25 19:46:39 +00:00
|
|
|
operation_id='get_telegrams',
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-10 21:53:24 +00:00
|
|
|
async def telegram_read_all(hood=Depends(get_hood)):
|
|
|
|
return await Telegram.objects.filter(hood=hood).all()
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.get(
|
|
|
|
'/{telegram_id}',
|
|
|
|
# TODO response_model,
|
2020-07-25 19:46:39 +00:00
|
|
|
operation_id='get_telegram',
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-10 21:53:24 +00:00
|
|
|
async def telegram_read(telegram=Depends(get_telegram)):
|
|
|
|
return telegram
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.delete(
|
2020-07-25 19:46:39 +00:00
|
|
|
'/{telegram_id}',
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
operation_id='delete_telegram',
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-10 21:53:24 +00:00
|
|
|
async def telegram_delete(telegram=Depends(get_telegram)):
|
|
|
|
spawner.stop(telegram)
|
2020-09-06 17:11:18 +00:00
|
|
|
for user in await TelegramUser.objects.filter(bot=telegram).all():
|
|
|
|
await user.delete()
|
2020-07-10 21:53:24 +00:00
|
|
|
await telegram.delete()
|
2020-09-11 00:58:39 +00:00
|
|
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
2020-07-10 21:53:24 +00:00
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.post(
|
|
|
|
'/',
|
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
# TODO response_model,
|
2020-07-25 19:46:39 +00:00
|
|
|
operation_id='create_telegram',
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-17 13:07:24 +00:00
|
|
|
async def telegram_create(
|
|
|
|
response: Response, values: BodyTelegram, hood=Depends(get_hood)
|
|
|
|
):
|
2020-07-10 21:53:24 +00:00
|
|
|
try:
|
|
|
|
telegram = await Telegram.objects.create(hood=hood, **values.__dict__)
|
|
|
|
spawner.start(telegram)
|
2020-09-28 22:39:32 +00:00
|
|
|
response.headers['Location'] = str(telegram.id)
|
2020-07-10 21:53:24 +00:00
|
|
|
return telegram
|
|
|
|
except IntegrityError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
2020-07-17 13:07:24 +00:00
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.put(
|
|
|
|
'/{telegram_id}',
|
|
|
|
status_code=status.HTTP_202_ACCEPTED,
|
|
|
|
# TODO response_model,
|
2020-07-25 19:46:39 +00:00
|
|
|
operation_id='update_telegram',
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-17 13:07:24 +00:00
|
|
|
async def telegram_update(values: BodyTelegram, telegram=Depends(get_telegram)):
|
|
|
|
try:
|
|
|
|
spawner.stop(telegram)
|
|
|
|
await telegram.update(**values.__dict__)
|
|
|
|
spawner.start(telegram)
|
|
|
|
return telegram
|
|
|
|
except IntegrityError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.get(
|
|
|
|
'/{telegram_id}/status',
|
|
|
|
status_code=status.HTTP_200_OK,
|
|
|
|
# TODO response_model,
|
2020-07-25 19:46:39 +00:00
|
|
|
operation_id='status_telegram',
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-17 13:07:24 +00:00
|
|
|
async def telegram_status(telegram=Depends(get_telegram)):
|
|
|
|
return {'status': spawner.get(telegram).status.name}
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.post(
|
|
|
|
'/{telegram_id}/start',
|
|
|
|
status_code=status.HTTP_200_OK,
|
|
|
|
# TODO response_model,
|
2020-07-25 19:46:39 +00:00
|
|
|
operation_id='start_telegram',
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-17 13:07:24 +00:00
|
|
|
async def telegram_start(telegram=Depends(get_telegram)):
|
|
|
|
await telegram.update(enabled=True)
|
|
|
|
spawner.get(telegram).start()
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.post(
|
|
|
|
'/{telegram_id}/stop',
|
|
|
|
status_code=status.HTTP_200_OK,
|
|
|
|
# TODO response_model,
|
2020-07-25 19:46:39 +00:00
|
|
|
operation_id='stop_telegram',
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-17 13:07:24 +00:00
|
|
|
async def telegram_stop(telegram=Depends(get_telegram)):
|
|
|
|
await telegram.update(enabled=False)
|
|
|
|
spawner.get(telegram).stop()
|
|
|
|
return {}
|