赞
踩
接入钉钉机器人支持群聊和私聊
网上看了很多方案,因为Claude的api申请难度非常大,大部分都是说使用Slack,但是Slack只能免费使用一个月。作为一个完美主义怎么可能允许这样的事情发生。何如处理,抓网页。
只能美,英,魔法这些都是入门小菜,这里不再赘述。自行解决。
1,获取organizationUuid
String url = "https://claude.ai/api/organizations";
2,新增会话
String url = String.format("https://claude.ai/api/organizations/%s/chat_conversations", organizationUuid);
返回 conversationUuid
3,获取单个会话
String url = String.format("https://claude.ai/api/organizations/%s/chat_conversations/%s", organizationUuid, conversationUuid);
4,获取会话列表
String url = String.format("https://claude.ai/api/organizations/%s/chat_conversations", organizationUuid);
5,删除会话
String url = String.format("https://claude.ai/api/organizations/%s/chat_conversations/%s", organizationUuid, conversationUuid);
7,发送消息
String url = "https://claude.ai/api/append_message";
一下代码都是他自己生成的,活学活用。
1,请求代理
- public class RestTemplateFactory {
-
- public static RestTemplate getRestTemplate() {
- return new RestTemplate();
- }
-
- public static RestTemplate getProxyRestTemplate(Integer type) {
- RestTemplateConf restTemplateConf = new RestTemplateConf();
- try {
- if (type == 1) {
- return restTemplateConf.getRestTemplateHongKong();
- } else if (type == 2) {
- return restTemplateConf.getRestTemplateWashington();
- } else if (type == 3) {
- return restTemplateConf.getProxyRestLocal();
- } else if (type == 4) {
- return restTemplateConf.getRestTemplate();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return new RestTemplate();
- }
-
- }
- public class RestTemplateConf {
-
- private String hostname;
- private Integer port;
- private String username;
- private String password;
-
- public RestTemplateConf() {
-
- }
-
- private RestTemplateConf(String hostname, Integer port, String username, String password) {
- this.hostname = hostname;
- this.port = port;
- this.username = username;
- this.password = password;
- }
-
- public RestTemplate getRestTemplateHongKong() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
- RestTemplateConf restTemplateConf = new RestTemplateConf("xxxx", xxxx, "xxxx", "xxxx");
- return restTemplateConf.getRestTemplateProxy();
- }
-
- public RestTemplate getRestTemplateWashington() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
- RestTemplateConf restTemplateConf = new RestTemplateConf("xxxx", xxxx, "xxxx", "xxxx");
- return restTemplateConf.getRestTemplateProxy();
- }
-
- public RestTemplate getProxyRestLocal() {
- HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
- // 配置代理
- HttpHost proxy = new HttpHost("127.0.0.1", 1087);
- httpClientBuilder.setProxy(proxy);
-
- // 配置其他HttpClient的相关参数
- RequestConfig requestConfig = RequestConfig.custom()
- // 可根据需求进行配置
- .setConnectTimeout(120000)
- .setSocketTimeout(120000)
- .build();
- httpClientBuilder.setDefaultRequestConfig(requestConfig);
-
- // 将配置好的HttpClient应用到RestTemplate
- return new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build()));
- }
-
- public RestTemplate getRestTemplate() {
- HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
- factory.setConnectTimeout(120000);
- factory.setReadTimeout(120000);
- return new RestTemplate(factory);
- }
-
- private RestTemplate getRestTemplateProxy() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
- RestTemplate restTemplate = new RestTemplate();
- restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
- HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
- // 配置其他HttpClient的相关参数
- RequestConfig requestConfig = RequestConfig.custom()
- // 可根据需求进行配置
- .setConnectTimeout(120000)
- .setSocketTimeout(120000)
- .build();
- httpClientBuilder.setDefaultRequestConfig(requestConfig);
- SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
- public boolean isTrusted(X509Certificate[] arg0, String arg1) {
- return true;
- }
- }).build();
- httpClientBuilder.setSSLContext(sslContext);
- //设置代理
- this.setProxy(restTemplate);
- //设置代理密码
- this.setCredentialsProvider(httpClientBuilder);
- HttpClient httpClient = httpClientBuilder.build();
- // httpClient连接配置
- HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
- restTemplate.setRequestFactory(clientHttpRequestFactory);
- return restTemplate;
- }
-
-
- private void setProxy(RestTemplate restTemplate) {
- SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
- Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port));
- requestFactory.setProxy(proxy);
- restTemplate.setRequestFactory(requestFactory);
- }
-
- private void setCredentialsProvider(HttpClientBuilder httpClientBuilder) {
- CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
- credentialsProvider.setCredentials(
- new AuthScope(hostname, port),
- new UsernamePasswordCredentials(username, password)
- );
- HttpHost proxy = new HttpHost(hostname, port);
- httpClientBuilder.setProxy(proxy).setDefaultCredentialsProvider(credentialsProvider).disableCookieManagement();
-
- }
2,发送消息
- /**
- * 获取问题答案
- */
- public static String getAnswer(List<Message> messages, String organizationUuid, String conversationUuid) {
- String organizations = null;
- String conversations = null;
- if (organizationUuid == null || conversationUuid == null) {
- organizations = getOrganizations();
- organizationUuid = JSONUtil.parseArray(organizations).getJSONObject(0).getStr("uuid");
- conversations = addChatConversation(organizationUuid, "问答");
- conversationUuid = JSONUtil.parseObj(conversations).getStr("uuid");
- }
- List<String> attachments = new ArrayList<>();
- if (messages.size() != 1) {
- for (int i = 0; i < messages.size() - 1; i++) {
- Message message = messages.get(i);
- if ("user".equals(message.getRole())) {
- try {
- sendMessage(organizationUuid, conversationUuid, messages.get(i).getContent(), attachments);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- }
- }
- Message message = messages.get(messages.size() - 1);
- List<String> responses = null;
- try {
- responses = sendMessage(organizationUuid, conversationUuid, message.getContent(), attachments);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- //deleteChatConversation(organizationUuid, conversationUuid);
- return String.join("", responses).trim();
1,方案一,不能用挺久了,这种支持外部群
2,方案二
企业内部应用
配置好即可
项目地址,欢迎交流
java 版和 python 版都有
https://gitee.com/g7go/chat-ai
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。