Test-Chatbot / app.py
ParthCodes's picture
Upload 3 files
73e95e8
from flask import Flask, jsonify, request
from flask_cors import CORS
import string
import random
import g4f
app = Flask(__name__)
CORS(app)
rooms = {}
room_data = {}
group_chat = {'chats': []}
@app.route('/')
def index():
return "Hello πŸ‘‹, This is CrickTech Stream API's Hub."
def generate_room_code():
return ''.join(random.choices(string.ascii_uppercase, k=6))
@app.route('/create-room', methods=['POST'])
def create_room():
room_code = generate_room_code()
rooms[room_code] = {'players': [], 'started': True, 'process': 0, 'generated': False, 'story_content': '',
'reactions': 0, 'chats': [], 'pollCount': 0}
player_name = request.json.get('name')
if not player_name:
return jsonify({'error': 'Player name is required'})
# Add the player to the room.
player_id = len(rooms[room_code]['players']) + 1
rooms[room_code]['players'].append({'id': player_id, 'name': player_name})
return jsonify({'room_code': room_code})
@app.route('/join-room/<room_code>', methods=['POST', 'GET'])
def join_room(room_code):
if request.method == 'POST':
if room_code not in rooms:
return jsonify({'noRoom': True})
player_name = request.json.get('name')
if not player_name:
return jsonify({'error': 'Player name is required'})
# Add the player to the room.
player_id = len(rooms[room_code]['players']) + 1
rooms[room_code]['players'].append({'id': player_id, 'name': player_name})
return jsonify({'player_id': player_id, 'players': rooms[room_code]['players']})
if request.method == 'GET':
if room_code not in rooms:
return jsonify({'noRoom': True})
ready = False
if len(rooms[room_code]['players']) == 5: # do it <= 5
ready = True
return jsonify({'players': rooms[room_code]['players'], 'ready': ready, 'started': rooms[room_code]['started'],
'meta_data': rooms[room_code]})
@app.route('/leave-room/<room_code>/<int:player_id>', methods=['POST'])
def leave_room(room_code, player_id):
if room_code not in rooms:
return jsonify({'error': 'Room not found'})
rooms[room_code]['players'] = [player for player in rooms[room_code]['players'] if player['id'] != player_id]
if player_id == 1 or not rooms[room_code]['players']:
del rooms[room_code]
return jsonify({'message': 'Player has left the room'})
@app.route('/reaction-true/<room_code>', methods=['GET'])
def reaction_true(room_code):
rooms[room_code]['reactions'] += 1
return jsonify({'success': True, 'likeCount': rooms[room_code]['reactions']})
@app.route('/reaction-false/<room_code>', methods=['GET'])
def reaction_false(room_code):
rooms[room_code]['reactions'] -= 1
return jsonify({'success': True, 'likeCount': rooms[room_code]['reactions']})
@app.route('/room-chat/<room_code>', methods=['GET'])
def room_chat(room_code):
if room_code not in rooms:
return jsonify({'success': False, 'error': 'Room not found'})
print(rooms[room_code]['chats'])
return jsonify({'success': True, 'chats': rooms[room_code]['chats'], 'pollCount': rooms[room_code]['pollCount']})
@app.route('/chat-input/<room_code>/<int:player_id>', methods=['POST'])
def chat_input(room_code, player_id):
data = request.get_json()
if room_code not in rooms:
return jsonify({'success': False, 'error': 'Room not found'})
rooms[room_code]['chats'].append({'id': player_id, 'text': data.get('text'), 'playerName': data.get('playerName')})
return jsonify({'success': True})
@app.route('/poll-data/<room_code>', methods=['POST'])
def poll_count(room_code):
if room_code not in rooms:
return jsonify({'success': False, 'error': 'Room not found'})
rooms[room_code]['pollCount'] += 1
return jsonify({'success': True, 'pollCount': rooms[room_code]['pollCount']})
@app.route('/list-rooms', methods=['GET'])
def list_rooms():
room_info = {}
for room_code, player in rooms.items():
room_info[room_code] = len(player['players'])
return jsonify({'roomInfo': room_info})
@app.route('/group-chat', methods=['POST', 'GET'])
def groupchat():
if request.method == 'POST':
data = request.get_json()
group_chat['chats'].append({'id': data.get('playerName'), 'text': data.get('text')})
return jsonify({'success': True})
if request.method == 'GET':
print(group_chat['chats'])
return jsonify({'success': True, 'chats': group_chat['chats']})
@app.route('/chat-bot', methods=['POST'])
def chatbot():
if request.method == 'POST':
data = request.get_json()
query = data.get('query')
response = g4f.ChatCompletion.create(
model="gpt-3.5-turbo",
provider=g4f.Provider.Hashnode,
messages=[{"role": "user", "content": query}],
)
return jsonify({'success': True, 'response': '#' + str(response)[1:]})
if __name__ == '__main__':
app.run()