All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dengchen2020.message.feishu.FeiShuClientImpl Maven / Gradle / Ivy

There is a newer version: 0.0.42
Show newest version
package io.github.dengchen2020.message.feishu;

import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.dengchen2020.core.utils.JsonUtils;
import io.github.dengchen2020.core.utils.RestClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.StringUtils;

/**
 * 飞书webhook推送默认实现
 *
 * @author dengchen
 * @since 2023/7/4
 */
@Async
public class FeiShuClientImpl implements FeiShuClient {

    private static final Logger log = LoggerFactory.getLogger(FeiShuClientImpl.class);

    private final String webhook;

    public FeiShuClientImpl(String webhook) {
        this.webhook = webhook;
    }

    @Override
    public void sendText(String text) {
        sendText(text, webhook);
    }

    @Override
    public void sendText(final String text, final String webhook) {
        if (!StringUtils.hasText(webhook)) {
            log.error("未配置飞书webhook");
            return;
        }
        try {
            ObjectNode push = JsonUtils.getObjectMapper().createObjectNode();
            push.put("msg_type", "text");
            ObjectNode textObj = JsonUtils.getObjectMapper().createObjectNode();
            push.set("content", textObj);
            textObj.put("text", text);
            RestClientUtils.post().uri(webhook)
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(push).retrieve().toBodilessEntity();
        } catch (Exception e) {
            log.error("飞书机器人发送信息{}失败:{}", text, e.getMessage());
        }
    }

    @Override
    public void sendTextToAll(String text) {
        sendTextToAll(text, webhook);
    }

    @Override
    public void sendTextToAll(final String text, final String webhook) {
        sendText("所有人 " + text, webhook);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy