钉钉消息推送

建个群,申请个机器人,拿到token;

package com.ximalaya.firework.auth.sender;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nali.common.util.CollectionUtils;
import com.ximalaya.firework.auth.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Component
@Slf4j
public class DingDingRobot {


    @Value("${robot.accessToken}")
    private String accessToken ;


    private final String BASE_URL = "https://oapi.dingtalk.com/robot/send?access_token=";

    private final String FIREWORK_URL = "http://ops.ximalaya.com/firework-backend/home#/page/approve-manage";

    private RestTemplate restTemplate = new RestTemplate();


    public void sendAuditNotification(String planName, List locationNames ,List users ){
        DingMessage dingMessage = new DingMessage();
        StringBuilder content = new StringBuilder();
        content.append("弹屏 ").append(planName).append(" 投放 ");
        for(String locationName:locationNames){
            content.append(locationName).append(",");
        }
        if(CollectionUtils.isEmpty(users)){
            return ;
        }
        for(User user:users){
            content.append("@").append(user.getPhonenumber());
            dingMessage.getAt().getAtMobiles().add(user.getPhonenumber());
        }
        content.append("请及时审核。审核链接 ").append(FIREWORK_URL);
        dingMessage.getText().setContent(content.toString());
        doSend(dingMessage);
    }

    public void sendApproveNotification(String planName,User creator,User auditor){
        DingMessage dingMessage = new DingMessage();
        StringBuilder content = new StringBuilder();
        content.append("@").append(creator.getPhonenumber()).append("申请的弹屏 ").append(planName)
                .append("@").append(auditor.getPhonenumber()).append("已审核通过,弹屏开始下发。");
        dingMessage.getText().setContent(content.toString());
        dingMessage.getAt().getAtMobiles().add(creator.getPhonenumber());
        dingMessage.getAt().getAtMobiles().add(auditor.getPhonenumber());
        doSend(dingMessage);
    }




    public void doSend(DingMessage dingMessage){
        log.info("Send dingding message: {} ", JSON.toJSONString(dingMessage));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity entity = new HttpEntity<>(JSONObject.toJSONString(dingMessage),headers);
        String base_url = BASE_URL;
        ResponseEntity responseEntity = restTemplate.postForEntity(base_url +accessToken,entity,String.class);
    }

}



package com.ximalaya.firework.auth.sender;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;


@Data
public class DingMessage {

    private String msgtype = "text";

    private Text text = new Text();

    private At at = new At();

    @Data
    public  static class Text{
        private String content;
    }

    @Data
    public static class At{
        private List atMobiles = new ArrayList<>();
        private boolean isAtAll = false;
    }

}

你可能感兴趣的:(javaweb)