kramcat/CharacterAI

need hep stuck with problem for 4 weeks 😭😭😭

Opened this issue Β· 1 comments

Can i ask what would be the full code to prompt character ai through command prompt by batch using their api? πŸ₯ΊπŸ₯Ί what i want is to ask a question to all characters i picked and all their responses (their names included in responses so i can track them) will be in a file saved in my folderpath ive been stuck with this for 4 weeks and never figured it out 😭😭😭

I can write a Python script for that if you want. It's straightforward and easy. Using only a batch script seems unnecessary in this context as batch scripts are primarily for Windows automation.

import asyncio  
from characterai import aiocai  
  
# Configuration  
token = 'your_character_ai_token'  
character_ids = ['XD3dOm5OMlHVhdqN-PPoR9KtWKkd9Y4CzZIjYEQfaLw',   
                 '6HhWfeDjetnxESEcThlBQtEUo0O8YHcXyHqCgN7b2hY',  
                 'qtEICpGfFS8f5Zr5kCHR1EsGsHlawNutYSZJq_IEZDY']  # List of character IDs  
question = "Your question here"  
output_file_path = 'responses.txt'  
  
  
async def get_response(client, char, me_id, question):  
    async with await client.connect() as chat:  
        new, answer = await chat.new_chat(char, me_id)  
        message = await chat.send_message(char, new.chat_id, question)  
        return message.name, message.text  
  
  
async def main():  
    client = aiocai.Client(token)  
    me = await client.get_me()  
  
    responses = []  
  
    for char in character_ids:  
        try:  
            name, response_text = await get_response(client, char, me.id, question)  
            responses.append(f"Character: {name}\nResponse: {response_text}\n")  
        except Exception as e:  
            responses.append(f"Character ID: {char}\nError: {str(e)}\n")  
  
    # Save responses to file  
    with open(output_file_path, 'w', encoding='utf-8') as file:  
        file.writelines(responses)  
  
    print("Responses saved successfully.")  
  
  
asyncio.run(main())

You can use batch script to calls the Python script if you want.

@echo off 
python path\to\your\script.py 
pause