-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from 673575554/feature/dingTalkTool
1. add ding talk plugin - custom robot send group message
- Loading branch information
Showing
13 changed files
with
373 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
96 changes: 96 additions & 0 deletions
96
community/plugins/spring-ai-alibaba-starter-plugin-dingtalk/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
41 changes: 41 additions & 0 deletions
41
...in-dingtalk/src/main/java/com/alibaba/cloud/ai/plugin/dingtalk/DingTalkConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...lugin-dingtalk/src/main/java/com/alibaba/cloud/ai/plugin/dingtalk/DingTalkProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...r-plugin-dingtalk/src/main/java/com/alibaba/cloud/ai/plugin/dingtalk/DingTalkService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...r-plugin-dingtalk/src/main/java/com/alibaba/cloud/ai/plugin/dingtalk/utils/SignUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "×tamp=" + timestamp + "&sign=" + sign; | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.alibaba.cloud.ai.plugin.dingtalk.DingTalkConfiguration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.