package com.cpop.api.tencent.wxWork.handler;

import com.alibaba.fastjson.JSONObject;
import com.cpop.api.tencent.wxWork.core.config.WxWorkApiConfig;
import com.cpop.api.tencent.wxWork.webHook.WebHookSendTextRequest;
import com.cpop.common.utils.http.HttpUtils;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @author DB
 * @since  2023/09/15 16:24
 * 企微机器人
 */
@Component
public class WebHookSendHandler {

    @Autowired
    private WxWorkApiConfig config;

    /**
     * 机器人发送文本
     * @author DB
     * @since  2023/09/15 17:56
     * @param key 机器人键
     * @param phoneList 通知人手机号
     * @param content 内容
     */
    public void webHookSendText(String key, List<String> phoneList, String content, Boolean isAll) throws IOException {
        if (isAll) {
            phoneList.add("@all");
        }
        WebHookSendTextRequest request = new WebHookSendTextRequest();
        WebHookSendTextRequest.Text text = request.new Text();
        text.setContent(content);
        text.setMentionedMobileList(phoneList);
        request.setText(text);
        Response response = HttpUtils.sendOkHttpPost(config.getWebhook() + key, JSONObject.toJSONString(request));
        response.close();
    }


}
