Skip to content

Commit

Permalink
Merge pull request #115 from 673575554/feature/dingTalkTool
Browse files Browse the repository at this point in the history
1. add ding talk plugin - custom robot send group message
  • Loading branch information
chickenlj authored Nov 26, 2024
2 parents 1ca8719 + 7be051c commit 5a96a93
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</parent>
<artifactId>spring-ai-alibaba-starter-plugin-baidusearch</artifactId>
<name>spring-ai-alibaba-starter-plugin-baidusearch</name>
<description>Baidu seartch tool for Spring AI Alibaba</description>
<description>Baidu search tool for Spring AI Alibaba</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</parent>
<artifactId>spring-ai-alibaba-starter-plugin-bingsearch</artifactId>
<name>spring-ai-alibaba-starter-plugin-bingsearch</name>
<description>Bing seartch tool for Spring AI Alibaba</description>
<description>Bing search tool for Spring AI Alibaba</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
com.alibaba.cloud.ai.plugin.bing.BingSearchPluginConfiguration
com.alibaba.cloud.ai.plugin.bing.BingSearchPluginConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright 2023-2024 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba</artifactId>
<version>${revision}</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<artifactId>spring-ai-alibaba-starter-plugin-dingtalk</artifactId>
<name>spring-ai-alibaba-starter-plugin-dingtalk</name>
<description>Ding Talk tool for Spring AI Alibaba</description>

<properties>
<dingtalk-sdk-version>2.1.68</dingtalk-sdk-version>
<old-dingtalk-sdk-version>2.0.0</old-dingtalk-sdk-version>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<!--New version of DingTalk SDK-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dingtalk</artifactId>
<version>${dingtalk-sdk-version}</version>
</dependency>
<!--The old version of DingTalk SDK. Some interfaces have not been fully replaced yet-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>${old-dingtalk-sdk-version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alibaba.cloud.ai.plugin.dingtalk;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

/**
* @author YunLong
*/
@Configuration
@ConditionalOnClass(DingTalkService.class)
@EnableConfigurationProperties(DingTalkProperties.class)
public class DingTalkConfiguration {

@Bean
@ConditionalOnMissingBean
@Description("Send DingTalk group chat messages using a custom robot")
@ConditionalOnProperty(prefix = "spring.ai.alibaba.plugin.dingtalk", name = "enabled", havingValue = "true")
public DingTalkService dingTalkGroupSendMessageByCustomRobotFunction(DingTalkProperties dingTalkProperties) {
return new DingTalkService(dingTalkProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.alibaba.cloud.ai.plugin.dingtalk;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* @author YunLong
*/
@ConfigurationProperties(prefix = "spring.ai.alibaba.plugin.dingtalk")
public class DingTalkProperties {

// Official Document Address:https://open.dingtalk.com/document/orgapp/custom-robots-send-group-messages
private String customRobotAccessToken;

private String customRobotSignature;

public DingTalkProperties() {}

public DingTalkProperties(String customRobotAccessToken, String customRobotSignature) {
this.customRobotAccessToken = customRobotAccessToken;
this.customRobotSignature = customRobotSignature;
}

public String getCustomRobotAccessToken() {
return customRobotAccessToken;
}

public void setCustomRobotAccessToken(String customRobotAccessToken) {
this.customRobotAccessToken = customRobotAccessToken;
}

public String getCustomRobotSignature() {
return customRobotSignature;
}

public void setCustomRobotSignature(String customRobotSignature) {
this.customRobotSignature = customRobotSignature;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.alibaba.cloud.ai.plugin.dingtalk;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.alibaba.cloud.ai.plugin.dingtalk.utils.SignUtils;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import org.springframework.util.ObjectUtils;

import java.util.function.Function;

/**
* @author YunLong
*/
public class DingTalkService implements Function<DingTalkService.Request, DingTalkService.Response> {

private final DingTalkProperties dingTalkProperties;

public DingTalkService(DingTalkProperties dingTalkProperties) {
this.dingTalkProperties = dingTalkProperties;
}

/**
* The old version of DingTalk SDK. Some interfaces have not been fully replaced yet.
* Official Document Address:https://open.dingtalk.com/document/orgapp/custom-robots-send-group-messages
*/
@Override
public Response apply(Request request) {

if (ObjectUtils.isEmpty(dingTalkProperties.getCustomRobotAccessToken()) || ObjectUtils.isEmpty(dingTalkProperties.getCustomRobotSignature())) {
throw new IllegalArgumentException("spring.ai.alibaba.plugin.dingtalk.custom-robot must not be null.");
}

String accessToken = dingTalkProperties.getCustomRobotAccessToken();
String signature = dingTalkProperties.getCustomRobotSignature();

// Request Body, please see the official document for more parameters.
OapiRobotSendRequest req = new OapiRobotSendRequest();
req.setMsgtype("text");
req.setText(String.format("{\"content\":\"%s\"}", request.message()));

try {
DingTalkClient client = new DefaultDingTalkClient(String.format("https://oapi.dingtalk.com/robot/send?%s", SignUtils.getSign(signature)));
OapiRobotSendResponse response = client.execute(req, accessToken);

if (response.isSuccess()) {
return new Response("The custom robot message was sent successfully!");
}
} catch (Exception e) {
throw new RuntimeException("Custom robot message sending failed!");
}

return null;
}

@JsonClassDescription("Send group chat messages using a custom robot")
public record Request(
@JsonProperty(required = true, value = "message")
@JsonPropertyDescription("Customize what the robot needs to send") String message) {
}

public record Response(String message) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.alibaba.cloud.ai.plugin.dingtalk.utils;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
* @author YunLong
*/
public class SignUtils {

public static String getSign(String signature) {
Long timestamp = System.currentTimeMillis();
String stringToSign = timestamp + "\n" + signature;

try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(signature.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), StandardCharsets.UTF_8);
return "&timestamp=" + timestamp + "&sign=" + sign;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.alibaba.cloud.ai.plugin.dingtalk.DingTalkConfiguration
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<module>community/plugins/spring-ai-alibaba-starter-plugin-time</module>
<module>community/plugins/spring-ai-alibaba-starter-plugin-baidusearch</module>
<module>community/plugins/spring-ai-alibaba-starter-plugin-bingsearch</module>
<module>community/plugins/spring-ai-alibaba-starter-plugin-dingtalk</module>
</modules>

<properties>
Expand Down
22 changes: 16 additions & 6 deletions spring-ai-alibaba-examples/function-calling-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,31 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-plugin-time</artifactId>
<version>${spring-ai-alibaba.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-plugin-time</artifactId>
<version>1.0.0-M3.2</version>
<artifactId>spring-ai-alibaba-starter-plugin-baidusearch</artifactId>
<version>${spring-ai-alibaba.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-plugin-baidusearch</artifactId>
<version>1.0.0-M3.2</version>
<artifactId>spring-ai-alibaba-starter-plugin-bingsearch</artifactId>
<version>${spring-ai-alibaba.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-plugin-bingsearch</artifactId>
<version>1.0.0-M3.2</version>
<artifactId>spring-ai-alibaba-starter-plugin-dingtalk</artifactId>
<version>${spring-ai-alibaba.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
Loading

0 comments on commit 5a96a93

Please sign in to comment.