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

me.chanjar.weixin.cp.api.impl.WxCpGroupRobotServiceImpl Maven / Gradle / Ivy

There is a newer version: 4.6.7.B
Show newest version
package me.chanjar.weixin.cp.api.impl;

import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpGroupRobotService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.article.NewArticle;
import me.chanjar.weixin.cp.bean.message.WxCpGroupRobotMessage;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType;

/**
 * 企业微信群机器人消息发送api 实现
 *
 * @author yr  created on  2020-08-20
 */
@RequiredArgsConstructor
public class WxCpGroupRobotServiceImpl implements WxCpGroupRobotService {
  private final WxCpService cpService;

  private String getWebhookUrl() throws WxErrorException {
    WxCpConfigStorage wxCpConfigStorage = this.cpService.getWxCpConfigStorage();
    final String webhookKey = wxCpConfigStorage.getWebhookKey();
    if (StringUtils.isEmpty(webhookKey)) {
      throw new WxErrorException("请先设置WebhookKey");
    }
    return wxCpConfigStorage.getApiUrl(WxCpApiPathConsts.WEBHOOK_SEND) + webhookKey;
  }

  @Override
  public void sendText(String content, List mentionedList, List mobileList) throws WxErrorException {
    this.sendText(this.getWebhookUrl(), content, mentionedList, mobileList);
  }

  @Override
  public void sendMarkdown(String content) throws WxErrorException {
    this.sendMarkdown(this.getWebhookUrl(), content);
  }

  @Override
  public void sendImage(String base64, String md5) throws WxErrorException {
    this.sendImage(this.getWebhookUrl(), base64, md5);
  }

  @Override
  public void sendNews(List articleList) throws WxErrorException {
    this.sendNews(this.getWebhookUrl(), articleList);
  }

  @Override
  public void sendText(String webhookUrl, String content, List mentionedList, List mobileList) throws WxErrorException {
    this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
      .setMsgType(GroupRobotMsgType.TEXT)
      .setContent(content)
      .setMentionedList(mentionedList)
      .setMentionedMobileList(mobileList)
      .toJson());
  }

  @Override
  public void sendMarkdown(String webhookUrl, String content) throws WxErrorException {
    this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
      .setMsgType(GroupRobotMsgType.MARKDOWN)
      .setContent(content)
      .toJson());
  }

  @Override
  public void sendImage(String webhookUrl, String base64, String md5) throws WxErrorException {
    this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
      .setMsgType(GroupRobotMsgType.IMAGE)
      .setBase64(base64)
      .setMd5(md5).toJson());
  }

  @Override
  public void sendNews(String webhookUrl, List articleList) throws WxErrorException {
    this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
      .setMsgType(GroupRobotMsgType.NEWS)
      .setArticles(articleList).toJson());
  }

  @Override
  public void sendFile(String webhookUrl, String mediaId) throws WxErrorException {
    this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
      .setMsgType(GroupRobotMsgType.FILE)
      .setMediaId(mediaId).toJson());
  }


  @Override
  public void sendVoice(String webhookUrl, String mediaId) throws WxErrorException {
    this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
      .setMsgType(GroupRobotMsgType.VOICE)
      .setMediaId(mediaId).toJson());
  }

  @Override
  public void sendTemplateCardMessage(String webhookUrl, WxCpGroupRobotMessage wxCpGroupRobotMessage) throws WxErrorException {
    this.cpService.postWithoutToken(webhookUrl, wxCpGroupRobotMessage.toJson());
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy