当前位置:   article > 正文

公众号回复消息 以及消息模板

公众号回复消息 以及消息模板

MsgHandler


import com.clina.matron.component.bulid.ImageBuilder;
import com.clina.matron.component.bulid.TextBuilder;
import com.clina.matron.component.mp.WxMpConfiguration;
import com.clina.matron.model.entity.GzhReplaySetting;
import com.clina.matron.model.entity.WxApp;
import com.clina.matron.model.entity.WxUser;
import com.clina.matron.service.GzhReplaySettingService;
import com.clina.matron.service.WxAppService;
import com.clina.matron.service.WxUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType;

/**
 * Created by Clint
 * Date: 2020-08-17 21:43
 * Description:
 */
@Slf4j
@Component
@AllArgsConstructor
public class MsgHandler extends AbstractHandler {

  private final WxAppService wxAppService;
  private final WxUserService wxUserService;
  private final GzhReplaySettingService gzhReplaySettingService;

  @Override
  public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                  Map<String, Object> context, WxMpService wxMpService,
                                  WxSessionManager sessionManager) {
    //组装回复消息
    if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
      WxMpXmlOutMessage rs;
      //TODO 可以选择将消息保存到本地
      WxApp wxApp = wxAppService.findByWeixinSign(wxMessage.getToUser());
      WxUser wxUser = wxUserService.getByOpenId(wxApp.getId(), wxMessage.getFromUser());
      /**
       * 根据关键词回复消息(精确)
       */
      if (StringUtils.isNotEmpty(wxMessage.getContent())) {
        GzhReplaySetting param = new GzhReplaySetting();
        param.setGzhId(wxApp.getId());
        param.setReplyType("message");
        param.setKeyWord(wxMessage.getContent());
        GzhReplaySetting replay = gzhReplaySettingService.getOneForQuery(param);
        if (replay != null && StringUtils.isNotEmpty(replay.getReplyContent())) {
          // 响应消息
          return senfMessageByReplay(replay,wxMessage,wxMpService);
        }
      }
    }
    return null;
  }

  public WxMpXmlOutMessage senfMessageByReplay(GzhReplaySetting replay, WxMpXmlMessage wxMessage, WxMpService weixinService1){
    if(replay.getMessageType().equals("text")){
      log.info("回复文本消息...");
        return new TextBuilder().build(replay.getReplyContent(), wxMessage,weixinService1);
    }else if(replay.getMessageType().equals("image")){
      log.info("回复图片消息...");
        return new ImageBuilder().build(replay.getReplyImage(), wxMessage,weixinService1);
    }else if(replay.getMessageType().equals("news")){ //发送文本消息
      log.info("回复图文消息...");
      WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
      item.setDescription(replay.getReplyContent());
      item.setPicUrl(replay.getReplyImage());
      item.setTitle(replay.getReplyTitle());
      item.setUrl(replay.getReplyUrl());
        WxMpXmlOutNewsMessage m = WxMpXmlOutMessage.NEWS()
                .fromUser(wxMessage.getToUser())
                .toUser(wxMessage.getFromUser())
                .addArticle(item)
                .build();
        return m;
    }
    return null;
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

AbstractBuilder

package com.clina.matron.component.bulid;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractBuilder {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    public abstract WxMpXmlOutMessage build(String content,
                                            WxMpXmlMessage wxMessage, WxMpService weixinService);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

SubscribeHandler

package com.clina.matron.component.handler;

import cn.hutool.json.JSONUtil;
import com.clina.matron.common.WeChatConstants;
import com.clina.matron.component.bulid.ImageBuilder;
import com.clina.matron.component.bulid.TextBuilder;
import com.clina.matron.component.mp.WxMpConfiguration;
import com.clina.matron.model.entity.GzhReplaySetting;
import com.clina.matron.model.entity.WxApp;
import com.clina.matron.model.entity.WxUser;
import com.clina.matron.service.GzhReplaySettingService;
import com.clina.matron.service.WxAppService;
import com.clina.matron.service.WxUserService;
import com.clina.matron.util.DateTimeUtils;
import lombok.AllArgs
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/232823
推荐阅读
相关标签
  

闽ICP备14008679号