package com.dingtalk.h5app.quickstart.quartz;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.taobao.api.ApiException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * @author Shawn
 */
public class MyJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
            OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
            req.setAgentId(1661857171L);
            req.setUseridList(context.getJobDetail().getJobDataMap().getString("userid"));
//            req.setDeptIdList(deptlist);
            OapiMessageCorpconversationAsyncsendV2Request.Msg obj1 = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            obj1.setMsgtype("action_card");
            OapiMessageCorpconversationAsyncsendV2Request.Link obj2 = new OapiMessageCorpconversationAsyncsendV2Request.Link();
            obj2.setTitle("速来学英语");
            obj1.setLink(obj2);
            OapiMessageCorpconversationAsyncsendV2Request.Markdown obj3 = new OapiMessageCorpconversationAsyncsendV2Request.Markdown();
            obj3.setTitle("速来学英语");
            obj1.setMarkdown(obj3);
            OapiMessageCorpconversationAsyncsendV2Request.ActionCard obj4 = new OapiMessageCorpconversationAsyncsendV2Request.ActionCard();
            obj4.setSingleUrl("http://101.204.146.11:8081/#/");
            obj4.setSingleTitle(context.getJobDetail().getJobDataMap().getString("frequency")+"速来学英语");
            obj4.setMarkdown("![](https://img1.baidu.com/it/u=908218778,3362502617&fm=253&fmt=auto&app=120&f=JPEG?w=1057&h=800)");
            obj4.setTitle("速来学英语");
            obj1.setActionCard(obj4);
            req.setMsg(obj1);
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, context.getJobDetail().getJobDataMap().getString("token"));
            System.out.println(rsp.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }

    }
}
