Search in sources :

Example 1 with BaseWxService

use of com.github.binarywang.demo.wx.mp.service.BaseWxService in project weixin-java-mp-multi-demo by binarywang.

the class MsgHandler method handle.

@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
    BaseWxService weixinService = (BaseWxService) wxMpService;
    if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
    // TODO 可以选择将消息保存到本地
    }
    // 当用户输入关键词如“你好”,“客服”等,并且有客服在线时,把消息转发给在线客服
    if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服") && weixinService.hasKefuOnline()) {
        return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
    }
    // TODO 组装回复消息
    String content = "回复信息内容";
    return new TextBuilder().build(content, wxMessage, weixinService);
}
Also used : TextBuilder(com.github.binarywang.demo.wx.mp.builder.TextBuilder) BaseWxService(com.github.binarywang.demo.wx.mp.service.BaseWxService)

Example 2 with BaseWxService

use of com.github.binarywang.demo.wx.mp.service.BaseWxService in project weixin-java-mp-multi-demo by binarywang.

the class MenuHandler method handle.

@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
    BaseWxService weixinService = (BaseWxService) wxMpService;
    String key = wxMessage.getEventKey();
    WxMenuKey menuKey = null;
    try {
        menuKey = JSON.parseObject(key, WxMenuKey.class);
    } catch (Exception e) {
        return WxMpXmlOutMessage.TEXT().content(key).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
    }
    AbstractBuilder builder = null;
    switch(menuKey.getType()) {
        case XmlMsgType.TEXT:
            builder = new TextBuilder();
            break;
        case XmlMsgType.IMAGE:
            builder = new ImageBuilder();
            break;
        case XmlMsgType.VOICE:
            break;
        case XmlMsgType.VIDEO:
            break;
        case XmlMsgType.NEWS:
            break;
        default:
            break;
    }
    if (builder != null) {
        try {
            return builder.build(menuKey.getContent(), wxMessage, weixinService);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }
    }
    return null;
}
Also used : WxMenuKey(com.github.binarywang.demo.wx.mp.dto.WxMenuKey) TextBuilder(com.github.binarywang.demo.wx.mp.builder.TextBuilder) AbstractBuilder(com.github.binarywang.demo.wx.mp.builder.AbstractBuilder) ImageBuilder(com.github.binarywang.demo.wx.mp.builder.ImageBuilder) BaseWxService(com.github.binarywang.demo.wx.mp.service.BaseWxService)

Example 3 with BaseWxService

use of com.github.binarywang.demo.wx.mp.service.BaseWxService in project weixin-java-mp-multi-demo by binarywang.

the class SubscribeHandler method handle.

@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
    this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
    BaseWxService weixinService = (BaseWxService) wxMpService;
    // 获取微信用户基本信息
    WxMpUser userWxInfo = weixinService.getUserService().userInfo(wxMessage.getFromUser(), null);
    if (userWxInfo != null) {
    // TODO 可以添加关注用户到本地
    }
    WxMpXmlOutMessage responseResult = null;
    try {
        responseResult = handleSpecial(wxMessage);
    } catch (Exception e) {
        this.logger.error(e.getMessage(), e);
    }
    if (responseResult != null) {
        return responseResult;
    }
    try {
        return new TextBuilder().build("感谢关注", wxMessage, weixinService);
    } catch (Exception e) {
        this.logger.error(e.getMessage(), e);
    }
    return null;
}
Also used : TextBuilder(com.github.binarywang.demo.wx.mp.builder.TextBuilder) WxMpUser(me.chanjar.weixin.mp.bean.result.WxMpUser) BaseWxService(com.github.binarywang.demo.wx.mp.service.BaseWxService) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Aggregations

TextBuilder (com.github.binarywang.demo.wx.mp.builder.TextBuilder)3 BaseWxService (com.github.binarywang.demo.wx.mp.service.BaseWxService)3 AbstractBuilder (com.github.binarywang.demo.wx.mp.builder.AbstractBuilder)1 ImageBuilder (com.github.binarywang.demo.wx.mp.builder.ImageBuilder)1 WxMenuKey (com.github.binarywang.demo.wx.mp.dto.WxMenuKey)1 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)1 WxMpXmlOutMessage (me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage)1 WxMpUser (me.chanjar.weixin.mp.bean.result.WxMpUser)1