钉钉推送消息

钉钉推送消息

  public void pushMessage(final String id, final String devNum) {
     
        // 钉钉推送
        String pushIds = mobileCaseReceiveRecordMapper.queryPushIdByEventId(id);
        // String pushIds = mobileArrestBoundMapper.getPushIds(devNum);
        if (!StringUtils.isEmpty(pushIds)) {
            Long send = dingTalkService.send(pushIds);
            // 钉钉推送消息结果
            dingTalkService.getSendResult(send);
        }
    }
package com.microvideo.ewcp.notSceneMove.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiMessageCorpconversationGetsendresultRequest;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiMessageCorpconversationGetsendresultResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.microvideo.ewcp.notSceneMove.constants.Constants;
import com.microvideo.ewcp.notSceneMove.entity.DingTalkUserInfo;
import com.microvideo.ewcp.notSceneMove.service.DingTalkService;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Hsin
 * @version 1.0
 * @date 2022/4/12 14:01
 * @description
 */
@Service
@Slf4j
public class DingTalkServiceImpl implements DingTalkService {

    private static final String APPURL = "http://139.195.212:8089/TZTP_DD/#/pages/index/index";

    @Override
    public String getAccessToken() {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(Constants.APPKEY);
        request.setAppsecret(Constants.APPSECRET);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = null;
        try {
            response = client.execute(request);
            JSONObject object = JSONObject.parseObject(response.getBody());
            if(object != null && "0".equals(object.getString("errcode"))){
                return object.getString("access_token");
            }
        } catch (ApiException e) {
            log.error("钉钉api错误");
            log.error("", e);
        }
        return null;
    }

    public DingTalkUserInfo getUserInfo(final String code){
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
            OapiUserGetRequest req = new OapiUserGetRequest();
            /*通过免登码获取userid*/
            req.setUserid(getUserid(code));
            OapiUserGetResponse rsp = client.execute(req, getAccessToken());

            JSONObject object = JSONObject.parseObject(rsp.getBody());
            if(object != null && "0".equals(object.getString("errcode"))){
                JSONObject result = object.getJSONObject("result");
                DingTalkUserInfo userInfo = new DingTalkUserInfo();
                /*钉钉id*/
                userInfo.setUserId(result.getString("userid"));
                /*姓名*/
                userInfo.setName(result.getString("name"));
                userInfo.setAssociatedUnionId(result.getString("associated_unionid"));
                userInfo.setUnionId(result.getString("unionid"));
                userInfo.setDeviceId(result.getString("device_id"));
                /*手机号*/
                userInfo.setMobile(result.getString("mobile"));
                /*头像*/
                userInfo.setAvatar(result.getString("avatar"));
                return userInfo;
            }
        } catch (ApiException e) {
            log.error("钉钉api错误");
            log.error("", e);
        }
        return  null;
    }

    @Override
    public String getUserid(final String code) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
        OapiUserGetuserinfoRequest req = new OapiUserGetuserinfoRequest();
        req.setCode(code);
        OapiUserGetuserinfoResponse rsp = null;
        try {
            rsp = client.execute(req, getAccessToken());
            JSONObject object = JSONObject.parseObject(rsp.getBody());
            if(object != null && "0".equals(object.getString("errcode"))){
                JSONObject result = object.getJSONObject("result");
                return result.getString("userid");
            }
        } catch (ApiException e) {
            log.error("钉钉api错误");
            log.error("", e);
        }
        return null;
    }

    public Long send(final String userIds){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(Long.parseLong(Constants.AGENTID));
        request.setUseridList(userIds);
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        /*链接消息模板*/
        msg.setMsgtype("link");
        msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
        msg.getLink().setTitle("泰州非现场执法");
        msg.getLink().setText(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "您有一条新的违法信息待处理");
        msg.getLink().setMessageUrl(APPURL);
        msg.getLink().setPicUrl("@lADPDf0i2Po0VBrNAQDNAQA");
        request.setMsg(msg);

        OapiMessageCorpconversationAsyncsendV2Response rsp = null;
        try {
            rsp = client.execute(request, getAccessToken());
        } catch (ApiException e) {
            log.error("钉钉api错误");
            log.error("", e);
        }
//        log.info(rsp.getBody());
        if(rsp==null){
            return null;
        }
        JSONObject object = JSONObject.parseObject(rsp.getBody());
        Long taskId = null;
        if(object != null && "0".equals(object.getString("errcode"))){
            taskId = object.getLong("task_id");
        }
        return taskId;
    }

    public OapiMessageCorpconversationGetsendresultRequest getSendResult(final Long taskId){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/getsendresult");
        OapiMessageCorpconversationGetsendresultRequest req = new OapiMessageCorpconversationGetsendresultRequest();
        req.setAgentId(Long.parseLong(Constants.AGENTID));
        req.setTaskId(taskId);
        OapiMessageCorpconversationGetsendresultResponse rsp = null;
        try {
            rsp = client.execute(req, getAccessToken());
            log.info(rsp.getBody());
        } catch (ApiException e) {
            log.error("钉钉api错误");
            log.error("", e);
        }
        return req;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值