java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot钉钉发送带图片消息

SpringBoot应用实现向钉钉发送带图片的消息的两种方法总结

作者:IT界Tony哥

在Spring Boot应用中向钉钉发送带图片的消息,主要有两种主流实现方式,一个是使用机器人Webhook,一个是通过钉钉开放平台SDK,下面我们就来看看具体实现步骤吧

在Spring Boot应用中向钉钉发送带图片的消息,主要有两种主流实现方式:一种是使用机器人Webhook发送包含图片链接的消息,另一种是通过钉钉开放平台SDK发送图片消息。下面我为你详细说明这两种方法的实现步骤。

两种发送钉钉图片消息的方案对比

方案类型实现方式图片处理适用场景主要限制
机器人Webhook方案通过群机器人Webhook发送图文消息图片需先上传至公网可访问的URL群聊通知、监控告警图片需公网可访问;每分钟限20条
钉钉SDK方案使用官方SDK上传媒体文件并发送图片上传至钉钉服务器获取media_id企业内部应用、精确到人需要企业应用权限;流程更复杂

方案一:使用钉钉机器人Webhook发送图文消息

这是最常用和简便的方法,适合向钉钉群发送带图片的通知消息。

1. 创建钉钉群机器人

首先需要在钉钉群中创建自定义机器人:

2. 添加Maven依赖

根据不同的starter库,可以选择以下依赖之一:

<!-- 方案A: snowheart的starter -->
<dependency>
    <groupId>cn.snowheart</groupId>
    <artifactId>spring-boot-dingtalk-robot-starter</artifactId>
    <version>1.0.3.RELEASE</version>
</dependency>

<!-- 方案B: javafamily的starter -->
<dependency>
    <groupId>club.javafamily</groupId>
    <artifactId>dingtalk-notification-spring-boot-starter</artifactId>
    <version>2.3.2-beta.13</version>
</dependency>

3. 配置Webhook参数

application.yml中配置机器人信息:

# 如果使用snowheart的starter
dingtalk:
  robot:
    prefix: https://oapi.dingtalk.com/robot/send
    access-token: your_access_token_here
    secret:
      secret-enabled: true
      secret-token: your_secret_here

# 如果使用javafamily的starter  
javafamily:
  notify:
    dingtalk:
      hook-url: https://oapi.dingtalk.com/robot/send?access_token=your_token
      enabled: true

4. 实现图片消息发送

以下是几种常见的带图片消息类型的实现代码:

A. 发送Link消息(图片+标题+描述)

@RestController
public class DingTalkController {
    
    @Autowired
    private DingTalkRobotClient dingTalkRobotClient; // snowheart starter
    
    /**
     * 发送带图片的Link消息
     */
    @GetMapping("/sendLinkMessage")
    public void sendLinkMessageWithImage() {
        // 图片必须先上传到公网可访问的URL
        String imageUrl = "https://example.com/your-image.jpg";
        String title = "系统监控告警";
        String text = "服务器CPU使用率超过90%,请及时处理!";
        String messageUrl = "http://your-system.com/detail"; // 点击跳转链接
        
        LinkMessage linkMessage = new LinkMessage(title, text, imageUrl, messageUrl);
        DingTalkResponse response = dingTalkRobotClient.sendLinkMessage(linkMessage);
        
        if (response.getErrcode().longValue() == 0L) {
            System.out.println("Link消息发送成功");
        }
    }
}

B. 发送Markdown消息(图片嵌入正文)

/**
 * 发送Markdown格式的图片消息
 */
@GetMapping("/sendMarkdownWithImage")
public void sendMarkdownWithImage() {
    String imageUrl = "https://example.com/chart.png";
    
    String markdownText = "### 业务数据报表\n\n" +
                         "最新业务数据统计图如下:\n\n" +
                         "![业务图表](" + imageUrl + ")\n\n" +
                         "**统计时间:** " + new Date();
    
    MarkdownMessage markdownMessage = new MarkdownMessage(
        "业务数据通知", 
        markdownText
    );
    
    DingTalkResponse response = dingTalkRobotClient.sendMarkdownMessage(markdownMessage);
    
    if (response.getErrcode().longValue() == 0L) {
        System.out.println("Markdown消息发送成功");
    }
}

C. 使用javafamily starter发送消息

@SpringBootTest
public class DingTalkNotifyTests {
    
    @Autowired
    private DingTalkNotifyHandler dingTalkNotifyHandler; // javafamily starter
    
    @Test
    void testNotifyLinkMessage() {
        String imageUrl = "https://example.com/image.jpg";
        
        DingTalkLinkRequest request = DingTalkLinkRequest.of(
            "系统通知",
            "发现异常请求,请及时查看处理",
            "https://your-system.com/detail",
            imageUrl
        );
        
        String response = dingTalkNotifyHandler.notify(request);
        System.out.println("发送结果:" + response);
    }
}

方案二:使用钉钉官方SDK发送图片消息

这种方法适合企业内部应用,需要用户授权,可以发送给指定用户。

1. 添加官方SDK依赖

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>alibaba-dingtalk-service-sdk</artifactId>
    <version>2.0.0</version>
</dependency>

2. 实现图片上传和发送

@Component
public class DingTalkService {
    
    @Value("${dingding.app-key}")
    private String appKey;
    
    @Value("${dingding.app-secret}")
    private String appSecret;
    @Value("${dingding.agent-id}")
    private Long agentId;
    
    /**
     * 获取AccessToken
     */
    public String getAccessToken() throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appKey);
        request.setAppsecret(appSecret);
        request.setHttpMethod("GET");
        
        OapiGettokenResponse response = client.execute(request);
        return response.getAccessToken();
    }
    
    /**
     * 上传图片并发送
     */
    public void sendImageMessage(String userId, String imageUrl) throws ApiException {
        String accessToken = getAccessToken();
        
        // 1. 先上传图片获取media_id
        String mediaId = uploadImage(accessToken, imageUrl);
        
        // 2. 发送图片消息
        DingTalkClient client = new DefaultDingTalkClient(
            "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        
        OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
        req.setAgentId(agentId);
        req.setUseridList(userId);
        
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = 
            new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("image");
        
        msg.setImage(new OapiMessageCorpconversationAsyncsendV2Request.Image());
        msg.getImage().setMediaId(mediaId);
        
        req.setMsg(msg);
        
        OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, accessToken);
        if (rsp.getErrcode() != 0) {
            throw new RuntimeException("发送失败:" + rsp.getErrmsg());
        }
    }
    
    /**
     * 上传图片到钉钉服务器
     */
    private String uploadImage(String accessToken, String imageUrl) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/media/upload");
        OapiMediaUploadRequest request = new OapiMediaUploadRequest();
        request.setType("image");
        
        // 需要下载图片并上传
        FileItem item = new FileItem(new URL(imageUrl).openStream());
        request.setMedia(item);
        
        OapiMediaUploadResponse response = client.execute(request, accessToken);
        return response.getMediaId();
    }
}

重要注意事项

图片处理要点

安全限制

实际应用示例:监控告警带图表

@Service
public class MonitorAlertService {
    
    @Autowired
    private DingTalkRobotClient dingTalkRobotClient;
    
    public void sendAlertWithChart(String alertMsg, String chartImageUrl) {
        String markdownContent = "### 🚨 系统监控告警\n\n" +
                               "**告警信息:** " + alertMsg + "\n\n" +
                               "**趋势图表:** \n\n" +
                               "![监控图表](" + chartImageUrl + ")\n\n" +
                               "**时间:** " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        
        MarkdownMessage message = new MarkdownMessage("系统监控告警", markdownContent);
        
        // @所有人
        message.setIsAtAll(true);
        
        DingTalkResponse response = dingTalkRobotClient.sendMarkdownMessage(message);
        
        if (response.getErrcode() == 0) {
            System.out.println("告警消息发送成功");
        }
    }
}

方案选择建议

根据你的具体场景选择合适的方案,即可实现Spring Boot应用向钉钉发送带图片的消息。

到此这篇关于SpringBoot应用实现向钉钉发送带图片的消息的两种方法总结的文章就介绍到这了,更多相关SpringBoot钉钉发送带图片消息内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文