Skip to main content

Claude Client

中文文档

Table of Contents

Claude Ai

1. Initialize Client

Function:
ClaudeAiClient(cookie).init()
Parameters:
cookie: str | Path | list[dict] - It can be a string or a Path to a file, or a direct json.
The cookie can be extracted as a JSON format using a browser extension called "cookie-editor".

from async_claude_client import ClaudeAiClient

claude_ai_client = await ClaudeAiClient(cookie="cookies.json").init()

2. Get All Chats

Function:
get_all_chats()
Returns:
A dictionary containing all chat windows.

chats = await claude_ai_client.get_all_chats()

3. Get Chat History

Function:
get_chat_history()
Parameters:
chat_id: str - The chat_id of the chat window. It can be extracted from the last UUID in the chat window's link or from the 'uuid' value in the data returned by the create_new_chat() function.
Returns:
A dictionary containing the chat history of the specified chat window.

history = await claude_ai_client.get_chat_history(chat_id)

4. Create a New Chat

Function:
create_new_chat()
Returns:
A dictionary containing the information of the newly created chat window. The value corresponding to the key 'uuid' is the chat_id of the new window.

new_chat = await claude_ai_client.create_new_chat()

5. Delete a Chat

Function:
delete_chat()
Parameters:
chat_id: str - The chat_id of the chat window. It can be extracted from the last UUID in the chat window's link or from the 'uuid' value in the data returned by the create_new_chat() function.

await claude_ai_client.delete_chat(chat_id)

6. Rename Chat

Function:
rename_chat()
Parameters:
title: str - The new name for the chat window.
chat_id: str - The chat_id of the chat window. It can be extracted from the last UUID in the chat window's link or from the 'uuid' value in the data returned by the create_new_chat() function.

await claude_ai_client.rename_chat(title, chat_id)

7. Stream Ask Claude

Function:
ask_stream()
Parameters:
question: str - The question to ask Claude.
chat_id: str - The chat_id of the chat window. It can be extracted from the last UUID in the chat window's link or from the 'uuid' value in the data returned by the create_new_chat() function.
attachment: str | Path - The address string or Path of the file attachment to upload.

new_chat = await claude_ai_client.create_new_chat()
async for text in claude_ai_client.ask_stream("Are you there?", new_chat["uuid"]):
    print(text, end="")

Slack Claude

1. Create Client

Function:
ClaudeAiClient(cookie).init()
Parameters:
slack_user_token: str - User token for the official Slack API. Refer to How to Get Slack User Token for obtaining the token.
claude_id: str - Member ID of the Claude app added in Slack. To obtain the Member ID, open Claude in the app and click on Claude's avatar.
channel_id: str - ID of the channel to communicate with Claude through the client. To obtain the channel ID, open the desired channel and extract the concatenated address string before '/thread/' in the URL. It usually starts with 'C', for example, C0579MZR3LH.
pre_msg: str - Prefix for the messages sent to Claude. This is a workaround because Slack adds '@claude' to the messages sent to Claude in the channel. This prefix is used to make Claude ignore the '@claude' mention.

from async_claude_client import Slack_Claude_Client, Text, ChatUpdate

slack_claude_client = Slack_Claude_Client(
    slack_user_token,
    claude_id,
    channel_id,
    "<Ignore '@Claude', neither say 'not seen' nor say 'seen'>"
)

2. Create a New Chat Message dict

Function:
create_new_chat()
Return:
A new dict for the chat message

chat = slack_claude_client.create_new_chat()

3. Ask Claude in a Streaming Manner

Function:
ask_stream_raw()
Parameters:
question: str - The question to ask
chat: dict - The chat message dict
Return Types:
ChatUpdate: Updated chat message dict
Text: Text reply message

Note: After each question, the chat message dict (chat) will be updated. The updated chat should be used for the next interaction.

chat = slack_claude_client.create_new_chat()
async for data in client.ask_stream_raw("Are you there?", chat):
    if isinstance(data, Text):
        print(data.content, end="")
    elif isinstance(data, ChatUpdate):
        # Update the chat with the new content
        chat = data.content

async for data in client.ask_stream_raw("Who are you talking about?", chat):
    if isinstance(data, Text):
        print(data.content, end="")
    elif isinstance(data, ChatUpdate):
        chat = data.content

QA

1. How to Get Slack User Token?

Go to https://api.slack.com  
Create a bot API  
Go to the Options

Add the following permissions to OAuth & Permissions

Bot Token Scopes require the following permissions:
channels:history
channels:join
channels:manage
channels:read
channels:write.invites
channels:write.topic
chat:write
chat:write.customize
chat:write.public
groups:history
groups:read
groups:write
groups:write.invites
groups:write.topic
im:history
im:read
im:write
im:write.invites
im:write.topic
usergroups:write
users:read
users:write

User Token Scopes require the following permissions:
admin
Administer a workspace
channels:history
channels:read
channels:write
channels:write.invites
channels:write.topic
chat:write
groups:history
groups:read
groups:write
groups:write.invites
groups:write.topic
im:history
im:read
im:write
im:write.invites
im:write.topic
mpim:history
mpim:read
mpim:write
usergroups:write
users:read
users:write

After adding all the permissions, select Install App to proceed with the installation.

Once installed, you will receive a token starting with 'xoxp', which is the Slack user token.

2. Why is Slack Claude not responding?

It could be an issue with the Slack server itself, and developers cannot fix it. You can try creating a new chat window to resolve the issue.

Metadata

Release files for async-claude-client 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for async-claude-client 0.1.3
File Size Uploaded Provenance Actions
async_claude_client-0.1.3.tar.gz 8.7 kB Without provenance Details for async_claude_client-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for async-claude-client 0.1.3
File Interpreter ABI Platform Actions
async_claude_client-0.1.3-py3-none-any.whl Python 3 none any Details for async_claude_client-0.1.3-py3-none-any.whl

Total release size: 19.4 kB

Release files / async_claude_client-0.1.3.tar.gz

Download URL async_claude_client-0.1.3.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4beefaab9cfa8dbef5c9fa1da64fbe6ed4d482d562e0b576884d66453aa83a1d
BLAKE2b-256 checksum
How to use checksums
e445282d7804a08069973d2013f0595e6be32c7ee84b870391acc25516916401
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / async_claude_client-0.1.3-py3-none-any.whl

Download URL async_claude_client-0.1.3-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6a10f4ba42481527af0987dd7c7095e1e24f543f2e4ebb3a88d1b98fca042ef6
BLAKE2b-256 checksum
How to use checksums
94f66f65f0824f09a6c49265cd07bfd79e8f47a081d0e0392715cfd2f8781422
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Security for async-claude-client 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page