Skip to main content

async client for claude.ai and slack.com

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async_claude_client-0.1.3.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

async_claude_client-0.1.3-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file async_claude_client-0.1.3.tar.gz.

File metadata

  • Download URL: async_claude_client-0.1.3.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for async_claude_client-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4beefaab9cfa8dbef5c9fa1da64fbe6ed4d482d562e0b576884d66453aa83a1d
MD5 dfdeb347566e9076d11efc777ba90b53
BLAKE2b-256 e445282d7804a08069973d2013f0595e6be32c7ee84b870391acc25516916401

See more details on using hashes here.

File details

Details for the file async_claude_client-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for async_claude_client-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6a10f4ba42481527af0987dd7c7095e1e24f543f2e4ebb3a88d1b98fca042ef6
MD5 59e5b5a4c07c724041a5d33d4719daf7
BLAKE2b-256 94f66f65f0824f09a6c49265cd07bfd79e8f47a081d0e0392715cfd2f8781422

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page