dashboard.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from flask import Blueprint, jsonify, make_response, request
  2. from models import APIKey, BotList, db
  3. dashboard_bp = Blueprint("dashboard", __name__)
  4. # Set Open AI Key
  5. @dashboard_bp.route("/api/set_key", methods=["POST"])
  6. def set_key():
  7. data = request.get_json()
  8. api_key = data["openAIKey"]
  9. existing_key = APIKey.query.first()
  10. if existing_key:
  11. existing_key.key = api_key
  12. else:
  13. new_key = APIKey(key=api_key)
  14. db.session.add(new_key)
  15. db.session.commit()
  16. return make_response(jsonify(message="API key saved successfully"), 200)
  17. # Check OpenAI Key
  18. @dashboard_bp.route("/api/check_key", methods=["GET"])
  19. def check_key():
  20. existing_key = APIKey.query.first()
  21. if existing_key:
  22. return make_response(jsonify(status="ok", message="OpenAI Key exists"), 200)
  23. else:
  24. return make_response(jsonify(status="fail", message="No OpenAI Key present"), 200)
  25. # Create a bot
  26. @dashboard_bp.route("/api/create_bot", methods=["POST"])
  27. def create_bot():
  28. data = request.get_json()
  29. name = data["name"]
  30. slug = name.lower().replace(" ", "_")
  31. existing_bot = BotList.query.filter_by(slug=slug).first()
  32. if existing_bot:
  33. return (make_response(jsonify(message="Bot already exists"), 400),)
  34. new_bot = BotList(name=name, slug=slug)
  35. db.session.add(new_bot)
  36. db.session.commit()
  37. return make_response(jsonify(message="Bot created successfully"), 200)
  38. # Delete a bot
  39. @dashboard_bp.route("/api/delete_bot", methods=["POST"])
  40. def delete_bot():
  41. data = request.get_json()
  42. slug = data.get("slug")
  43. bot = BotList.query.filter_by(slug=slug).first()
  44. if bot:
  45. db.session.delete(bot)
  46. db.session.commit()
  47. return make_response(jsonify(message="Bot deleted successfully"), 200)
  48. return make_response(jsonify(message="Bot not found"), 400)
  49. # Get the list of bots
  50. @dashboard_bp.route("/api/get_bots", methods=["GET"])
  51. def get_bots():
  52. bots = BotList.query.all()
  53. bot_list = []
  54. for bot in bots:
  55. bot_list.append(
  56. {
  57. "name": bot.name,
  58. "slug": bot.slug,
  59. }
  60. )
  61. return jsonify(bot_list)