package com.usoftchina.dingtalk.sdk;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.usoftchina.dingtalk.sdk.config.Agent;
import com.usoftchina.dingtalk.sdk.config.DingTalkProperties;
import com.usoftchina.dingtalk.sdk.dto.SendMessageReq;

/**
 * 消息
 *
 * @author yingp
 * @date 2020/2/19
 */
public class MessageSdk extends BaseSdk {
    public MessageSdk(DingTalkProperties properties) {
        super(properties);
    }

    /**
     * 发送消息
     * 应用支持推送文本、图片、视频、文件、图文等类型
     *
     * @param agentCode 应用code
     * @param req
     * @return
     */
    public OapiMessageCorpconversationAsyncsendV2Response send(String agentCode, SendMessageReq req) {
        Agent agent = getAgentMap().get(agentCode);
        if (null == agent) {
            throw new RuntimeException("没有找到应用" + agentCode);
        }
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        req.agentId(agent.getId());
        OapiMessageCorpconversationAsyncsendV2Response resp = cached(() -> client.execute(req.build(), getAccessToken(agentCode)));
        assertOK(resp);
        return resp;
    }
}
