com.admin4j.chatbot.qwx.QyWeiXinChatBot Maven / Gradle / Ivy
The newest version!
package com.admin4j.chatbot.qwx;
import com.admin4j.chatbot.qwx.core.BotMsg;
import com.admin4j.chatbot.qwx.core.msg.TextBotMsg;
import io.github.admin4j.http.ApiJsonClient;
import io.github.admin4j.http.core.HttpDefaultConfig;
import lombok.Setter;
/**
* 企业微信群聊机器人
* https://developer.work.weixin.qq.com/document/path/99110
*
* @author andanyang
* @since 2022/10/15 22:00
*/
public class QyWeiXinChatBot {
private static final String BASE_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";
private String webhookUrl = "";
@Setter
private ApiJsonClient apiJsonClient;
public QyWeiXinChatBot(String webhookUrl) {
setWebhookUrl(webhookUrl);
apiJsonClient = new ApiJsonClient(HttpDefaultConfig.get());
}
public void setWebhookUrl(String webhookUrl) {
if (webhookUrl.contains("http")) {
this.webhookUrl = webhookUrl;
} else {
this.webhookUrl = BASE_URL + webhookUrl;
}
}
public void sendMsg(BotMsg msg) {
apiJsonClient.post(webhookUrl, msg);
}
public void sendTextMsg(String msg) {
TextBotMsg textBotMsg = new TextBotMsg(msg);
sendMsg(textBotMsg);
}
}