CharacterAI icon indicating copy to clipboard operation
CharacterAI copied to clipboard

How to continue to chat with a specific character?

Open MarkDeng1 opened this issue 1 year ago • 1 comments

This is my code:

from characterai import aiocai
import asyncio
from datetime import datetime

async def main():
    char = input('CHAR ID: ')
    client = aiocai.Client('a9ec003a613b2c639b415062701827c1fd2d2d24')
    me = await client.get_me()

    async with await client.connect() as chat:
        new, answer = await chat.new_chat(
            char, me.id
        )

        print(f'{answer.name}: {answer.text}')
        
        while True:
            text = input('YOU: ')

            message = await chat.send_message(
                char, new.chat_id, text
            )
            now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            print(f'{[{now}]}-{message.name}: {message.text}')

asyncio.run(main())

How to continue to chat with a specific character? The current codes cannot continue to chat, always from the scratch.

MarkDeng1 avatar Jan 30 '25 02:01 MarkDeng1

what u can do i guess is obtain a chat_id first by doing:

character_id='some id'
async def start_chat():
        me = await client.get_me(character_id)

        async with await client.connect() as chat:
            new_chat, initial_message = await chat.new_chat(
                character_id, me.id
            )

            #fetching data      
            chat_id = new_chat.chat_id
            initial_message = initial_message

            return chat_id, initial_message

then once u obtained a chat_id u can then use it to talk with a character by doing:

character_id= 'some character id'
chatroom_id = 'some id'
message - 'some message'

async def send_message(character_id, chatroom_id, message  ):
    async with await client.connect() as chat:
        message = await chat.send_message(
            character_id, chatroom_id, message
        )
        response = f'{message.name}: {message.text}'
        return response

senkuuuuu avatar Apr 19 '25 15:04 senkuuuuu