Skip to content

Commit

Permalink
feat: v2.0.0支持飞书群机器人-文本消息
Browse files Browse the repository at this point in the history
  • Loading branch information
AnswerAIL committed May 28, 2022
1 parent 6af2c6e commit 9f77352
Show file tree
Hide file tree
Showing 16 changed files with 415 additions and 23 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@


## What(Dinger是什么)
Dinger是一个以SpringBoot框架为基础开发的消息发送中间件, 对现有两大移动办公系统[钉钉](https://ding-doc.dingtalk.com/doc#/serverapi3/iydd5)[企业微信](https://work.weixin.qq.com/api/doc/90000/90136/91770)的群机器人API做了一层封装,让使用更简单便捷。
Dinger是一个以SpringBoot框架为基础开发的消息发送中间件, 对如下移动办公系统的群机器人API做了一层封装,让使用更简单便捷。
- [钉钉](https://open.dingtalk.com/document/group/custom-robot-access)
- [企业微信](https://developer.work.weixin.qq.com/document/path/91770)
- [飞书](https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN#756b882f)

只需要简单的配置(最简单的发送功能只需要一行代码),即可快速的在springboot项目中将消息发送到指定的钉钉或企业微信群聊中。

Expand Down Expand Up @@ -77,6 +80,17 @@ spring:
token-id: 32865206-7082-46l5-8j39-2m7ycy6d868
```

**使用飞书群机器人配置**
```yaml
spring:
dinger:
project-id: ${spring.application.name}
dingers:
# 使用飞书机器人, 请根据自己机器人配置信息进行修改
bytetalk:
token-id: 20200528-0824-20jm-21hy-5yc556210y15
```

 

### 三、代码中使用
Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
<groupId>com.github.answerail</groupId>
<artifactId>dinger-spring-boot-starter</artifactId>
<packaging>jar</packaging>
<version>1.3.0</version>
<version>2.0.0</version>

<name>dinger-spring-boot-starter</name>
<description>Dinger-SpringBoot集成钉钉/企业微信群机器人实现消息通知中间件</description>
<description>Dinger-SpringBoot集成钉钉/企业微信/飞书群机器人实现消息通知中间件</description>
<url>https://github.com/AnswerAIL/dingtalk-spring-boot-starter</url>


<properties>
<spring-boot.version>2.1.5.RELEASE</spring-boot.version>
<spring-web-version>5.2.11.RELEASE</spring-web-version>
<spring-boot.version>2.7.0</spring-boot.version>
<spring-web-version>5.3.20</spring-web-version>
<java.version>1.8</java.version>
<slf4j-api.version>1.7.30</slf4j-api.version>
<slf4j-api.version>1.7.36</slf4j-api.version>
<jackson.version>2.13.2.2</jackson.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright ©2015-2022 Jaemon. All Rights Reserved.
*
* 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
*
* 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.
*/
package com.github.jaemon.dinger.bytetalk;

import com.github.jaemon.dinger.core.DingerDefinition;
import com.github.jaemon.dinger.core.DingerDefinitionGenerator;
import com.github.jaemon.dinger.core.DingerDefinitionGeneratorContext;
import com.github.jaemon.dinger.core.DingerDefinitionHandler;
import com.github.jaemon.dinger.core.annatations.DingerText;
import com.github.jaemon.dinger.core.entity.enums.DingerDefinitionType;
import com.github.jaemon.dinger.core.entity.enums.DingerType;
import com.github.jaemon.dinger.core.entity.xml.MessageTag;

/**
* 飞书消息体定义生成类
*
* @author Jaemon
* @since 1.0
*/
public class FeiShuDefinitionGenerator extends DingerDefinitionHandler {
/**
* 生成生成注解文本消息体定义
*/
public static class AnnotationText extends DingerDefinitionGenerator<DingerText> {

@Override
public DingerDefinition generator(DingerDefinitionGeneratorContext<DingerText> context) {
return dingerTextHandler(DingerType.BYTETALK, context);
}
}


/**
* 生成XML文本消息体定义
*/
public static class XmlText extends DingerDefinitionGenerator<MessageTag> {

@Override
public DingerDefinition generator(DingerDefinitionGeneratorContext<MessageTag> context) {
return xmlHandler(DingerDefinitionType.BYTETALK_XML_TEXT, context);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright ©2015-2022 Jaemon. All Rights Reserved.
*
* 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
*
* 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.
*/
package com.github.jaemon.dinger.bytetalk.entity;

import com.github.jaemon.dinger.core.entity.MsgType;
import com.github.jaemon.dinger.core.entity.enums.DingerType;

/**
* ByteTalk请求体
*
* @author Jaemon
* @since 1.0
*/
public class ByteTalkMessage extends MsgType {
public ByteTalkMessage() {
setDingerType(DingerType.BYTETALK);
}
}
114 changes: 114 additions & 0 deletions src/main/java/com/github/jaemon/dinger/bytetalk/entity/ByteText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright ©2015-2022 Jaemon. All Rights Reserved.
*
* 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
*
* 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.
*/
package com.github.jaemon.dinger.bytetalk.entity;

import com.github.jaemon.dinger.bytetalk.entity.enums.ByteTalkMsgType;
import com.github.jaemon.dinger.support.sign.SignBase;
import com.github.jaemon.dinger.support.sign.SignResult;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
* 飞书-消息类型-文本类型
*
* @author Jaemon
* @since 1.0
*/
public class ByteText extends ByteTalkMessage {
private String msg_type;
private Content content;

/** 签名-时间戳 */
private String timestamp;
/** 签名-秘钥 */
private String sign;

public ByteText(Content content) {
this.msg_type = ByteTalkMsgType.TEXT.type();
this.content = content;
}

@Override
public void signAttributes(SignBase sign) {
if (sign == null || !(sign instanceof SignResult)) {
return;
}
SignResult signResult = (SignResult) sign;
this.timestamp = String.valueOf(signResult.getTimestamp());
this.sign = signResult.getSign();
}

public Content getContent() {
return content;
}

public void setContent(Content content) {
this.content = content;
}

public String getMsg_type() {
return msg_type;
}

public void setMsg_type(String msg_type) {
this.msg_type = msg_type;
}

public String getSign() {
return sign;
}

public void setSign(String sign) {
this.sign = sign;
}

public String getTimestamp() {
return timestamp;
}

public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}

public static class Content implements Serializable {
/**
* 文本内容,最长不超过2048个字节,必须是utf8编码
* */
private String text;

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public Content() {
}

public Content(String text) {
this.text = text;
}
}

@Override
public void transfer(Map<String, Object> params) {
this.content.text = replaceContent(this.content.text, params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright ©2015-2022 Jaemon. All Rights Reserved.
*
* 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
*
* 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.
*/
package com.github.jaemon.dinger.bytetalk.entity.enums;

/**
* ByteTalk支持的消息类型
*
* @author Jaemon
* @since 1.0
*/
public enum ByteTalkMsgType {
/**
* text类型
*/
TEXT("text"),

;

private String type;

ByteTalkMsgType(String type) {
this.type = type;
}

public String type() {
return type;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/github/jaemon/dinger/bytetalk/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright ©2015-2022 Jaemon. All Rights Reserved.
*
* 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
*
* 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.
*/

/**
* 支持ByteTalk包
*
* @author Jaemon
* @since 1.0
*/
package com.github.jaemon.dinger.bytetalk;
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ public interface DingerConstant {

String DINGER_PROP_PREFIX = "spring.dinger";
String DINGER_PROPERTIES_PREFIX = DINGER_PROP_PREFIX + SPOT_SEPERATOR;

String DINGER_COMMA = ",";
}
14 changes: 9 additions & 5 deletions src/main/java/com/github/jaemon/dinger/core/DingerRobot.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.github.jaemon.dinger.core.entity.DingerRequest;
import com.github.jaemon.dinger.core.entity.DingerResponse;
import com.github.jaemon.dinger.support.sign.SignBase;
import com.github.jaemon.dinger.support.client.MediaTypeEnum;
import com.github.jaemon.dinger.core.entity.DingerProperties;
import com.github.jaemon.dinger.core.entity.enums.DingerType;
Expand All @@ -27,6 +26,7 @@
import com.github.jaemon.dinger.exception.AsyncCallException;
import com.github.jaemon.dinger.exception.SendMsgException;
import com.github.jaemon.dinger.support.CustomMessage;
import com.github.jaemon.dinger.support.sign.SignBase;
import com.github.jaemon.dinger.utils.DingerUtils;
import org.springframework.beans.BeanUtils;

Expand Down Expand Up @@ -106,17 +106,21 @@ protected <T extends MsgType> DingerResponse send(T message) {
}

StringBuilder webhook = new StringBuilder();
webhook.append(dinger.getRobotUrl()).append("=").append(dinger.getTokenId());
webhook.append(dinger.getRobotUrl()).append(dinger.getTokenId());

if (log.isDebugEnabled()) {
log.debug("dingerId={} send message and use dinger={}, tokenId={}.", dkid, dingerType, dinger.getTokenId());
}

// 处理签名问题(只支持DingTalk)
if (dingerType == DingerType.DINGTALK &&
DingerUtils.isNotEmpty((dinger.getSecret()))) {
if (DingerUtils.isNotEmpty((dinger.getSecret()))) {
SignBase sign = dingTalkManagerBuilder.dingerSignAlgorithm.sign(dinger.getSecret().trim());
webhook.append(sign.transfer());

if (dingerType == DingerType.DINGTALK) {
webhook.append(sign.transfer());
} else if (dingerType == DingerType.BYTETALK) {
message.signAttributes(sign);
}
}

Map<String, String> headers = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.github.jaemon.dinger.core.entity;

import com.github.jaemon.dinger.core.entity.enums.DingerType;
import com.github.jaemon.dinger.support.sign.SignBase;
import com.github.jaemon.dinger.support.sign.SignResult;

import java.io.Serializable;
import java.util.Map;
Expand Down Expand Up @@ -62,6 +64,8 @@ public void setMsgtype(String msgtype) {
public void transfer(Map<String, Object> params) {
}

public void signAttributes(SignBase sign) {}

/**
* 转换文本内容
*
Expand Down
Loading

0 comments on commit 9f77352

Please sign in to comment.