【Camunda七】Camunda实现抄送环节给钉钉发消息

本文详细介绍了如何通过Java实现钉钉企业内部应用的消息发送功能。首先在钉钉开发者后台获取CorpId、AgentId、AppKey和AppSecret四个关键参数。接着,利用这些参数实现获取token并发送消息的流程,包括调用DingTalkClient进行接口交互,发送消息给指定用户。最后,文章提供了一个测试场景,展示了在流程实例发起后,消息将被正确抄送给钉钉指定人员。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、钉钉参数获取

 本文主要介绍如何实现这一功能,对钉钉其他使用不做过度解读。

首先进入钉钉的开发者后台,需要创建一个企业内部应用,在右侧可以看见一个CorpId 这是我们需要的。

然后点击这个企业内部应用进入,可以看到AgentId、AppKey、AppSecret 这三个参数

以上就拿到了需要的四个参数。

二、流程图

在抄送环节我们使用SendTask【发送任务】。发送任务用于发送消息。在Camunda中,这是通过调用Java代码来完成的。

 填写相关参数,也就是告诉它使用java的哪个类去处理

 三、功能实现

 首先在配置文件中敲上相关参数

 pom中新增依赖

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

 直接上这个委托类的代码

@Component
public class SendMessageTaskDelegate implements JavaDelegate {

    @Value("${ding.talk.agentId}")
    private Long agentId;

    @Value("${ding.talk.appKey}")
    private String appKey;

    @Value("${ding.talk.appSecret}")
    private String appSecret;

    @Value("${ding.talk.corpId}")
    private String corpId;

    @Override
    public void execute(DelegateExecution delegateExecution) throws Exception {

        //获取token  appkey+appsecret通过下面接口就能获取到token
        DingTalkClient client = new DefaultDingTalkClient(DingUrlConstant.URL_GET_TOKKEN);
        OapiGettokenRequest tokenRequest = new OapiGettokenRequest();
        tokenRequest.setAppkey(appKey);
        tokenRequest.setAppsecret(appSecret);
        tokenRequest.setHttpMethod("GET");
        OapiGettokenResponse response = client.execute(tokenRequest);
        System.out.println(response.getBody());
        //获取到token
        String accessToken = response.getAccessToken();
        //发送消息接口
        DefaultDingTalkClient clientOne = new DefaultDingTalkClient(DingUrlConstant.MESSAGE_ASYNCSEND);
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);//微应用ID
        request.setToAllUser(false);//是否发给所有人 false否 ,否的情况下要获取接受者id 可以是多个和接收者部门id
        request.setUseridList("manager1374");//接收者ID
        request.setHttpMethod("GET");
        OapiMessageCorpconversationAsyncsendV2Request.Msg message = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        message.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
        message.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
        message.getOa().getHead().setText("head");
        message.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
        message.getOa().getBody().setContent("您在业务系统有待审批任务");
        message.setMsgtype("oa");
        request.setMsg(message);
        OapiMessageCorpconversationAsyncsendV2Response rsp = clientOne.execute(request, accessToken);
        System.out.println(rsp.getBody());
    }
}

大体意思就是获取到token,然后给指定的人发消息,通过delegateExecution 也是可以拿到很多流程实例相关的参数的。这样就可以处理更多的业务需求,具体的大家自己试一试。

钉钉接口常量类DingUrlConstant也贴出来一下

 /**
     * 钉钉网关gettoken地址
     */
    public static final String URL_GET_TOKKEN = "https://oapi.dingtalk.com/gettoken";

    /**
     *获取用户在企业内userId的接口URL
     */
    public static final String URL_GET_USER_INFO = "https://oapi.dingtalk.com/user/getuserinfo";

    /**
     *获取用户姓名的接口url
     */
    public static final String URL_USER_GET = "https://oapi.dingtalk.com/user/get";

    /**
     * 发起审批实例的接口url
     */
    public static final String URL_PROCESSINSTANCE_START = "https://oapi.dingtalk.com/topapi/processinstance/create";

    /**
     * 获取审批实例的接口url
     */
    public static final String URL_PROCESSINSTANCE_GET = "https://oapi.dingtalk.com/topapi/processinstance/get";

    /**
     * 发送企业通知消息的接口url
     */
    public static final String MESSAGE_ASYNCSEND = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";

    /**
     * 删除企业回调接口url
     */
    public static final String DELETE_CALLBACK = "https://oapi.dingtalk.com/call_back/delete_call_back";

    /**
     * 注册企业回调接口url
     */
    public static final String REGISTER_CALLBACK = "https://oapi.dingtalk.com/call_back/register_call_back";

四、测试

部署流程定义后发起流程实例

 期望效果:发起人提交后抄送给钉钉指定人员

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LoneWalker、

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值