TelegramAktienBot/api/api_blueprint_telegram.py

43 lines
1.4 KiB
Python

import os
from apiflask import APIBlueprint, abort
from db import db
from helper_functions import make_response, get_email_or_abort_401
from auth import auth
from schema import TelegramIdSchema, UsersSchema
from models import User
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()
check_if_telegram_user_id_data_exists(data)
query_user = db.session.query(User).filter_by(email=email).first()
if query_user is None: # Username doesn't exist
abort(500, message="Unable to login")
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:
abort(400, message="User ID missing")
if data['telegram_user_id'] == "" or data['telegram_user_id'] is None:
abort(400, message="User ID missing")