package com.template.common.utils;

import cn.snowheart.dingtalk.robot.starter.client.DingTalkRobotClient;
import cn.snowheart.dingtalk.robot.starter.entity.DingTalkResponse;
import cn.snowheart.dingtalk.robot.starter.entity.TextMessage;
import com.template.controller.DingTalkController;
import com.template.model.result.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class DingTalkUtils {

    private static Logger logger = LoggerFactory.getLogger(DingTalkUtils.class);

    public CommonResult sendMessage(String phone, String content,DingTalkRobotClient dingTalkRobotClient ) {
        logger.info("钉钉消息推送phone：" + phone+"；content："+content);
        String[] arr = new String[1];
        arr[0] = phone;
        Boolean isAll = false;
        TextMessage textMessage = new TextMessage();
        textMessage.setContent(content);
        textMessage.setIsAtAll(isAll);
        textMessage.setAtMobiles(arr);
        logger.info("Message:" + textMessage.toMessageMap().toString());
        DingTalkResponse response = dingTalkRobotClient.sendMessage(textMessage);
        logger.info(response.toString());
        if (response.getErrcode().intValue() != 0) {
            logger.info("钉钉消息推送失败：" + response.getErrmsg());
            return CommonResult.fail(response.getErrmsg());
        }
        return CommonResult.ok(response);
    }

}
