CharacterAI
CharacterAI copied to clipboard
flask implementaiton on browser
from flask import Flask, jsonify, request from characterai import PyCAI
app = Flask(name)
@app.route('/', methods=['GET', 'POST']) def home(): if request.method == 'GET': data = "hello world" return jsonify({'data': data})
@app.route('/chat/string:msg', methods=['GET']) def disp(msg): token = '34f5788ba1f0cfcb8b8f03437c732bad6e0abd2a' client = PyCAI(token)
char = 'AbuIXFqY7EsLrOjUDLR0bDNYwqU6MDQyAKtzGIMeLI4'
print(token)
print(client)
print(char)
chat = client.chat.get_chat(char)
if(chat == None):
print("error")
else:
print(chat)
print(chat)
participants = chat['participants']
if not participants[0]['is_human']:
tgt = participants[0]['user']['username']
else:
tgt = participants[1]['user']['username']
data = client.chat.send_message(chat['external_id'], tgt, msg)
name = data['src_char']['participant']['name']
text = data['replies'][0]['text']
return jsonify({'token': token,'char':char})
print()
@app.route('/chat', methods=['POST']) def chat(): if request.method == 'POST' and 'msg' in request.json: msg = request.json['msg'] token = "34f5788ba1f0cfcb8b8f03437c732bad6e0abd2a" client = PyCAI(token)
char = "AbuIXFqY7EsLrOjUDLR0bDNYwqU6MDQyAKtzGIMeLI4"
chat = client.chat.get_chat(char)
participants = chat['participants']
if not participants[0]['is_human']:
tgt = participants[0]['user']['username']
else:
tgt = participants[1]['user']['username']
data = client.chat.send_message(chat['external_id'], tgt, msg)
name = data['src_char']['participant']['name']
text = data['replies'][0]['text']
return jsonify({'reply': text})
else:
return jsonify({'error': 'Invalid request or missing "msg" in JSON payload'})
if name == 'main': app.run(debug=False,host='0.0.0.0')
?????????????????????????????????????????????????????? this code works fine on local machine but not working in the browser or any hosting service can you please tell me what's the error
okay, but you better take your token outta there becuause someone could hack into your account, BlazeX (for example i can impersonate you when for example you're talking to Taffy, and yes i just did that to scare you to not do it gain, there are people you will do something far worse) and instead of just pasting your code in, wrap in in ```, so it will look like this:
if idiot:
print("idiot")