CharacterAI
  • 👋Welcome
  • 🧑‍💻Quick Start
  • 🛠️API
    • 🔑 Values
    • ⚠️ Errors
    • connect
    • ping
    • user
      • info
      • get_profile
      • followers
      • following
      • recent
      • update
    • post
      • get_post
      • my_posts
      • get_posts
      • upvote
      • undo_upvote
      • send_comment
      • delete_comment
      • create
      • delete
      • get_topics
      • feed
    • character
      • create
      • update
      • trending
      • recommended
      • categories
      • info
      • search
      • voices
    • chat
      • create_room
      • rate
      • next_message
      • get_histories
      • get_history
      • get_chat
      • send_message
      • delete_message
      • new_chat
    • chat2
      • next_message
      • send_message
      • get_histories
      • new_chat
      • get_chat
      • get_history
      • rate
      • delete_message
  • 📚Examples
    • Chatting
    • Parse Chat
  • ⚠️Issues
Powered by GitBook
On this page
  1. Examples

Chatting

Code for chatting with a character from the console

from characterai import PyCAI

client = PyCAI('TOKEN')
    
char = input('Enter CHAR: ')

# Save tgt and history_external_id 
# to avoid making a lot of requests
chat = client.chat.get_chat(char)

participants = chat['participants']

# In the list of "participants",
# a character can be at zero or in the first place
if not participants[0]['is_human']:
    tgt = participants[0]['user']['username']
else:
    tgt = participants[1]['user']['username']

while True:
    message = input('You: ')

    data = client.chat.send_message(
        chat['external_id'], tgt, message
    )

    name = data['src_char']['participant']['name']
    text = data['replies'][0]['text']

    print(f"{name}: {text}")
import asyncio
from characterai import PyAsyncCAI

async def main():
    client = PyAsyncCAI('TOKEN')

    char = input('Enter CHAR: ')

    # Getting chat info
    chat = await client.chat.get_chat(char)

    participants = chat['participants']

    # In the list of "participants",
    # a character can be at zero or in the first place
    if not participants[0]['is_human']:
        tgt = participants[0]['user']['username']
    else:
        tgt = participants[1]['user']['username']

    while True:
        message = input('You: ')

        data = await client.chat.send_message(
            chat['external_id'], tgt, message
        )

        name = data['src_char']['participant']['name']
        text = data['replies'][0]['text']

        print(f"{name}: {text}")

asyncio.run(main())
PreviousExamplesNextParse Chat

Last updated 1 year ago

📚