File size: 2,177 Bytes
a85c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Blueprint, jsonify, make_response, request
from models import APIKey, BotList, db

dashboard_bp = Blueprint("dashboard", __name__)


# Set Open AI Key
@dashboard_bp.route("/api/set_key", methods=["POST"])
def set_key():
    data = request.get_json()
    api_key = data["openAIKey"]
    existing_key = APIKey.query.first()
    if existing_key:
        existing_key.key = api_key
    else:
        new_key = APIKey(key=api_key)
        db.session.add(new_key)
    db.session.commit()
    return make_response(jsonify(message="API key saved successfully"), 200)


# Check OpenAI Key
@dashboard_bp.route("/api/check_key", methods=["GET"])
def check_key():
    existing_key = APIKey.query.first()
    if existing_key:
        return make_response(jsonify(status="ok", message="OpenAI Key exists"), 200)
    else:
        return make_response(jsonify(status="fail", message="No OpenAI Key present"), 200)


# Create a bot
@dashboard_bp.route("/api/create_bot", methods=["POST"])
def create_bot():
    data = request.get_json()
    name = data["name"]
    slug = name.lower().replace(" ", "_")
    existing_bot = BotList.query.filter_by(slug=slug).first()
    if existing_bot:
        return (make_response(jsonify(message="Bot already exists"), 400),)
    new_bot = BotList(name=name, slug=slug)
    db.session.add(new_bot)
    db.session.commit()
    return make_response(jsonify(message="Bot created successfully"), 200)


# Delete a bot
@dashboard_bp.route("/api/delete_bot", methods=["POST"])
def delete_bot():
    data = request.get_json()
    slug = data.get("slug")
    bot = BotList.query.filter_by(slug=slug).first()
    if bot:
        db.session.delete(bot)
        db.session.commit()
        return make_response(jsonify(message="Bot deleted successfully"), 200)
    return make_response(jsonify(message="Bot not found"), 400)


# Get the list of bots
@dashboard_bp.route("/api/get_bots", methods=["GET"])
def get_bots():
    bots = BotList.query.all()
    bot_list = []
    for bot in bots:
        bot_list.append(
            {
                "name": bot.name,
                "slug": bot.slug,
            }
        )
    return jsonify(bot_list)