Claude API interface calls configuration methods and scripts

Call the Claude method directly through the API, which has been applied to the one-click film AI painting software. For GPT, the speed is a bit slower, but it is acceptable to be similar to GPT35, and the most important thing is that it does not cost money.

Let's first look at my application in the project.
insert image description here

Create a Slack workbench

Slack registration URL , please use Google Mail, click to log in to use Google Mail.

insert image description here

Once logged in, select Create Workspace.

insert image description here
insert image description here
After the workspace is created, fill in the basic information to create.
insert image description here
insert image description here
Team members can skip here.
insert image description here
What the team is doing here is followed by the channel name, which is optional here.insert image description here

Associate Claude AI

Log in to claude-in-slack and click Add Application.

insert image description here
Association is allowed.
insert image description here
Prompt the following to prove it.

insert image description here
Jump to the workbench again and you will see the Claude icon in the lower left.
insert image description here
If you want to use Claude in the channel, you can add it to the specified channel, and the use needs to be @claudevalid.

insert image description here

Configure the Slack API

Go to the Slack workbench and start the project.
insert image description here
Then go to the Slack API configuration , create your project and connect to your Slack workbench.
insert image description here
Click Your apps in the upper right corner of the page , click Create an App, and click From scratsh. Here you need to create an application.

insert image description here
After creating the project, enter the workbench management and select your project.

insert image description hereFind User Token Scopes under the Scopes module, click the Add an OAuth Scopes button, and search to add the following permissions in turn.

channels:history
channels:read
channels:write
groups:history
groups:read
groups:write
chat:write
im:history
im:write
mpim:history
mpim:write

insert image description here

Finally, click the Install to Workspace button under OAuth Tokens for Your Workspace to confirm the authorization.

insert image description here

Upgrade Slack

There is no way to use the API to connect to Claude without upgrading, that is to say, there is no response to any text you enter.

insert image description here

Before the upgrade, the red part did not respond, and after the upgrade, the green part just finished.

insert image description here

Bonus API call code

More API functions can visit URL Slack API calls .

Only the most basic question and answer request functions are introduced here. Some functions need to be upgraded to enterprise users to access, try it yourself.

# coding=utf-8

import requests
import json
import time
import pandas as pd

token = '换成你的token'
channel = "换成你的Claude频道ID,查询频道ID看视频" 


def send_msg(msg):
    send_url = "https://slack.com/api/chat.postMessage"
    claude = ''

    data = {
    
    
        "token": token,
        "channel": channel,
        "text": claude + msg
    }
    response = requests.post(url=send_url, data=data)
    text = json.loads(response.text)
    return text


def receive_msg(ts):
    send_url = "https://slack.com/api/conversations.history"
    data = {
    
    
        "token": token,
        "channel": channel,
        "ts": ts,
        "oldest":ts
    }
    response = requests.post(url=send_url, data=data)
    text = json.loads(response.text)
    return text


msg_list = [
    "please help me",
    # "please say how to study English?",
    # "please say how to study English?",
]

list_data = []
for msg in msg_list:
    data = send_msg(msg)
    ts = data["ts"]
    list_data.append((ts, data))

df = pd.DataFrame(list_data, columns=["ts", "data"])
df.to_excel("data.xlsx")

for i in list_data:
    ts = i[0]
    print(ts)
    print(receive_msg(ts))

ts = "换成你要查询的时间戳"
print(receive_msg(ts))

Use result display.

insert image description here

Guess you like

Origin blog.csdn.net/qq_20288327/article/details/131801281