钉钉机器人调研(企业微应用机器人)

钉钉机器人调研(企业微应用机器人)

package demo.test.ding;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import java.util.ArrayList;
import java.util.List;

public class RobotChatController {

    /*
    1.通过后台的AppKey和AppSecret获取到请求的access_token:
    文档地址:https://ding-doc.dingtalk.com/document#/org-dev-guide/obtain-access_token
    */
    /**
     2. 获取token请求接口
     3.  4. @param appKey    密钥
     5. @param appSecret 密钥
     6. @return token
     */
    public static String getToken(String appKey, String appSecret) {
        //获取token值
        String token = "";
        try {
            DefaultDingTalkClient 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);
            token = response.getAccessToken();
            return token;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return token;
    }

    /*
    2.创建群聊前需要知道用户的userId,可通过用户手机号获取:
    文档地址:https://ding-doc.dingtalk.com/document#/org-dev-guide/get-userid-By-Mobile
    */
    /**
     1. 获取用户的userId
     2. @param phone 手机号
     3. @param token token值
     4. @return userId
     */
    public static String getUserId(String phone, String token) {
        String userId = "";
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
            OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
            req.setMobile(phone);
            OapiV2UserGetbymobileResponse rsp = client.execute(req, token);
            userId = rsp.getResult().getUserid();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return userId;
    }

    /*
    3.带着access_token创建群聊,保存群聊id:
    文档地址:https://ding-doc.dingtalk.com/document#/org-dev-guide/create-chat
    */
    /**
     1. 创建群聊
     2. @param userId 群主id
     3. @param memberList 群成员ids
     4. @param chatroomName 群聊名称
     5. @param token token值
     6. @return 群聊id
     */
    public static String createChatroom(String userId, List<String> memberList, String chatroomName, String token){
        String chatroomId = "";
        try{
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/create");
            OapiChatCreateRequest req = new OapiChatCreateRequest();
            req.setName(chatroomName);
            req.setOwner(userId);
            req.setUseridlist(memberList);
            OapiChatCreateResponse rsp = client.execute(req, token);
            chatroomId = rsp.getChatid();//获取到群聊id
            return chatroomId;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return chatroomId;
    }

    /*
    4.根据群聊id,按照自己所需要格式发送消息:
    文档地址:https://ding-doc.dingtalk.com/document#/org-dev-guide/send-chat-messages
    */
    /**
     * 发送群消息
     * @param chatId 群聊id
     * @param content 发送内容
     * @param msgType 内容类型
     * @param token token值
     * @return 发送状态码
     */
    public String sendMessage(String chatId,String content,String msgType,String token){
        String msg="";
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/send");
            OapiChatSendRequest req = new OapiChatSendRequest();
            req.setChatid(chatId);
            OapiChatSendRequest.Msg obj1 = new OapiChatSendRequest.Msg();
            OapiChatSendRequest.Text obj2 = new OapiChatSendRequest.Text();
            obj2.setContent(content);
            obj1.setText(obj2);
            obj1.setMsgtype(msgType);
            req.setMsg(obj1);
            OapiChatSendResponse rsp = client.execute(req, token);
            return rsp.getErrorCode();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return msg;
    }

    public static void main(String[] args) {
        String agentId = "3268888";
        String appKey = "ding2h8z8hdhaaaabbbb";
        String AppSecret = "2MbsExjaAgf6gyZcJjQRhJ_ryaJ-b42ux2bGIBPkcLz-aaaabbbb";
        String token = getToken(appKey, AppSecret);
        System.out.println(token);//41f557e159ad376aaaabbbb

        String phone = "18288889999";
        String userId = getUserId(phone, token);
        System.out.println(userId);//015412088889999

        List<String> memberList = new ArrayList<>();
        memberList.add("015412088889999");
        memberList.add("22686633311788889999");
        memberList.add("185739510788889999");

        String chatroomName = "公司员工关怀群";
        String chatroom = createChatroom(userId, memberList, chatroomName, token);
        System.out.println(chatroom);//chat33ad811d7df32aaaabbbb
    }

}

你可能感兴趣的:(钉钉,java)