2022-04-12 07:50:24 +00:00
|
|
|
__author__ = "Florian Kaiser"
|
|
|
|
__copyright__ = "Copyright 2022, Project Aktienbot"
|
|
|
|
__credits__ = ["Florian Kaiser", "Florian Kellermann", "Linus Eickhof", "Kevin Pauer"]
|
|
|
|
__license__ = "GPL 3.0"
|
|
|
|
__version__ = "1.0.0"
|
|
|
|
|
2022-03-27 15:24:26 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from apiflask import APIBlueprint, abort
|
2022-03-30 08:46:54 +00:00
|
|
|
from app.auth import auth
|
2022-04-05 08:51:09 +00:00
|
|
|
from app.db import database as db
|
|
|
|
from app.helper_functions import make_response, get_email_or_abort_401, get_user
|
2022-03-30 08:46:54 +00:00
|
|
|
from app.schema import TelegramIdSchema, UsersSchema
|
2022-03-27 15:24:26 +00:00
|
|
|
|
|
|
|
telegram_blueprint = APIBlueprint('telegram', __name__, url_prefix='/api')
|
|
|
|
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
|
|
|
|
|
|
|
|
|
|
|
@telegram_blueprint.route('/telegram', methods=['POST'])
|
|
|
|
@telegram_blueprint.output(UsersSchema(many=False), 200)
|
|
|
|
@telegram_blueprint.input(schema=TelegramIdSchema)
|
|
|
|
@telegram_blueprint.auth_required(auth)
|
|
|
|
@telegram_blueprint.doc(summary="Connects telegram user id", description="Connects telegram user id to user account")
|
|
|
|
def add_keyword(data):
|
|
|
|
email = get_email_or_abort_401()
|
|
|
|
|
2022-04-12 09:36:23 +00:00
|
|
|
# Check if request data is valid
|
2022-03-30 08:46:54 +00:00
|
|
|
if not check_if_telegram_user_id_data_exists(data):
|
|
|
|
abort(400, message="User ID missing")
|
2022-03-27 15:24:26 +00:00
|
|
|
|
2022-04-05 08:51:09 +00:00
|
|
|
query_user = get_user(email)
|
|
|
|
|
2022-04-12 09:36:23 +00:00
|
|
|
# Change user id
|
2022-03-27 15:24:26 +00:00
|
|
|
query_user.telegram_user_id = data['telegram_user_id']
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return make_response(query_user.as_dict(), 200, "Successfully connected telegram user")
|
|
|
|
|
|
|
|
|
|
|
|
def check_if_telegram_user_id_data_exists(data):
|
|
|
|
if "telegram_user_id" not in data:
|
2022-03-30 08:46:54 +00:00
|
|
|
return False
|
2022-03-27 15:24:26 +00:00
|
|
|
|
|
|
|
if data['telegram_user_id'] == "" or data['telegram_user_id'] is None:
|
2022-03-30 08:46:54 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|