代码拉取完成,页面将自动刷新
该项目已暂停更新,新项目增加钉钉、飞书、企业微信对单独用户的信息发送,请查看消息中间件
一个消息群发中间件
只需要简单的配置和编码,即可将相同的消息发送到钉钉、飞书、企业微信聊天群以及邮箱
统一消息维护方式,发送时会按照对应的平台类型自动进行转换
钉钉和飞书需要使用加签,配置时需要维护secret
目前支持两种消息模式 文本 和 markdown(飞书对应为富文本,邮箱对应为html)
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.gitee.wb04307201</groupId>
<artifactId>chatbot-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
@EnableChatbot
注解
@EnableChatbot
@SpringBootApplication
public class ChatbotDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringTestApplication.class, args);
}
}
application.yml
配置文件中添加以下相关配置,可以配置多个群chatbot:
config:
chatbot-info:
- alias: dd-1
chatbot-type: dingtalk
# webhook地址 https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxx
token: 为webhook地址中xxxxxxxxxxxxxxxxx部分
secret: secret
- alias: wx-1
chatbot-type: weixin
# webhook地址 https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxxxxx
token: 为webhook地址中xxxxxxxxxxxxxxxxx部分
- alias: fs-1
chatbot-type: feishu
# webhook地址 https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxxx
token: 为webhook地址中xxxxxxxxxxxxxxxxx部分
secret: secret
- alias: mail-1
chatbot-type: mail
from: 发件邮箱
to: 收件邮箱1,收件邮箱2
host: stmp地址
username: 用户名
password: 密码
@RestController
public class DemoController {
@Autowired
ChatbotService chatbotService;
@GetMapping(value = "/chat/robot/test")
public String send() {
//发送到全部平台
/*return chatbotService.send(
RequestContent.buildMarkdown()
.title("测试群发")
.addLine(SubLine.title("这是一行标题1",1))
.addLine(SubLine.title("这是一行标题2",2))
.addLine(SubLine.text("这是一行文本"))
.addLine(SubLine.link("这是一行链接","https://gitee.com/wb04307201/chatbot-spring-boot-starter"))
.addLine(SubLine.quote("这是一行引用"))
.addLine(SubLine.bold("这是一行加粗"))
.atAll(true)
).toString();*/
//发送到某类平台
/*return chatbotService.send(
RequestContent.buildMarkdown()
.addChatbotType(ChatbotType.DINGTALK)
.title("测试群发")
.addLine(SubLine.title("这是一行标题1",1))
.addLine(SubLine.title("这是一行标题2",2))
.addLine(SubLine.text("这是一行文本"))
.addLine(SubLine.link("这是一行链接","https://gitee.com/wb04307201/chatbot-spring-boot-starter"))
.addLine(SubLine.quote("这是一行引用"))
.addLine(SubLine.bold("这是一行加粗"))
.atAll(true)
).toString();*/
//发送到某几个平台
return chatbotService.send(
RequestContent.buildMarkdown().addAlias("dd-1", "mail-1")
.title("测试群发")
.addLine(SubLine.title("这是一行标题1", 1))
.addLine(SubLine.title("这是一行标题2", 2))
.addLine(SubLine.text("这是一行文本"))
.addLine(SubLine.link("这是一行链接", "https://gitee.com/wb04307201/chatbot-spring-boot-starter"))
.addLine(SubLine.quote("这是一行引用"))
.addLine(SubLine.bold("这是一行加粗"))
.atAll(true)
).toString();
}
}
目前支持的类型与转换格式对照如下表
chatbot | 钉钉 | 微信 | 飞书 | 邮件 |
---|---|---|---|---|
SubLine.text | 文字 | 文字 | text | |
SubLine.title | 标题 | 标题 | text | ~ |
SubLine.link | 链接 | 链接 | a | |
SubLine.quote | 引用 | 引用文字 | text | |
SubLine.bold | 加粗 | 加粗 | text |
发送的消息可通过http://ip:端口/chat/robot/list进行查看
注意:如配置了context-path需要在地址中对应添加
//可以通过如下方法添加平台信息
chatbotService.addDingtalk
chatbotService.addFeishu
chatbotService.addWeixin
chatbotService.addMail
//可以通过如下方法删除平台信息
chatbotService.removeByAlias
继承IChatbotRecord并实现方法,例如
@Component
public class H2ChatbotRecordImpl implements IChatbotRecord {
private static final String HISTORY = "chat_robot_history";
private static ConnectionPool connectionPool = new ConnectionPool(new ConnectionParam());
@Override
public ChatbotHistory save(ChatbotHistory chatbotHistory) {
try {
Connection conn = connectionPool.getConnection();
if (!StringUtils.hasLength(chatbotHistory.getId())) {
chatbotHistory.setId(UUID.randomUUID().toString());
ExecuteSqlUtils.executeUpdate(conn, ModelSqlUtils.insertSql(HISTORY, chatbotHistory), new HashMap<>());
} else {
ExecuteSqlUtils.executeUpdate(conn, ModelSqlUtils.updateByIdSql(HISTORY, chatbotHistory), new HashMap<>());
}
connectionPool.returnConnection(conn);
} catch (SQLException | InterruptedException e) {
throw new RuntimeException(e);
}
return chatbotHistory;
}
@Override
public List<ChatbotHistory> list(ChatbotHistory chatbotHistory) {
try {
Connection conn = connectionPool.getConnection();
String sql = ModelSqlUtils.selectSql(HISTORY, new ChatbotHistory());
List<String> condition = new ArrayList<>();
if (StringUtils.hasLength(chatbotHistory.getType()))
condition.add(" type = '" + chatbotHistory.getType() + "'");
if (StringUtils.hasLength(chatbotHistory.getAlias()))
condition.add(" alias like '%" + chatbotHistory.getAlias() + "%'");
if (StringUtils.hasLength(chatbotHistory.getRequest()))
condition.add(" request like '%" + chatbotHistory.getRequest() + "%'");
if (StringUtils.hasLength(chatbotHistory.getResponse()))
condition.add(" response like '%" + chatbotHistory.getResponse() + "%'");
if (!condition.isEmpty()) sql = sql + " where " + String.join("and", condition);
List<ChatbotHistory> res = ExecuteSqlUtils.executeQuery(conn, sql, new HashMap<>(), ChatbotHistory.class);
connectionPool.returnConnection(conn);
return res;
} catch (SQLException | InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void init() {
try {
Connection conn = connectionPool.getConnection();
if (!ExecuteSqlUtils.isTableExists(conn, HISTORY, connectionPool.getDbType())) {
ExecuteSqlUtils.executeUpdate(conn, ModelSqlUtils.createSql(HISTORY, new ChatbotHistory()), new HashMap<>());
}
} catch (SQLException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
并添加配置指向类
chatbot:
config:
chatbot-record: cn.wubo.chatbot.demo.H2ChatbotRecordImpl
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。