Search in sources :

Example 1 with TextBuilder

use of com.github.binarywang.demo.wx.mp.builder.TextBuilder 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 TextBuilder

use of com.github.binarywang.demo.wx.mp.builder.TextBuilder in project weixin-java-mp-demo-springboot by binarywang.

the class SubscribeHandler method handle.

@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService, WxSessionManager sessionManager) throws WxErrorException {
    this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
    // 获取微信用户基本信息
    try {
        WxMpUser userWxInfo = weixinService.getUserService().userInfo(wxMessage.getFromUser(), null);
        if (userWxInfo != null) {
        // TODO 可以添加关注用户到本地数据库
        }
    } catch (WxErrorException e) {
        if (e.getError().getErrorCode() == 48001) {
            this.logger.info("该公众号没有获取用户信息权限!");
        }
    }
    WxMpXmlOutMessage responseResult = null;
    try {
        responseResult = this.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) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Example 3 with TextBuilder

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

the class LocationHandler method handle.

@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
    if (wxMessage.getMsgType().equals(XmlMsgType.LOCATION)) {
        // TODO 接收处理用户发送的地理位置消息
        try {
            String content = "感谢反馈,您的的地理位置已收到!";
            return new TextBuilder().build(content, wxMessage, null);
        } catch (Exception e) {
            this.logger.error("位置消息接收处理失败", e);
            return null;
        }
    }
    // 上报地理位置事件
    this.logger.info("\n上报地理位置 。。。 ");
    this.logger.info("\n纬度 : " + wxMessage.getLatitude());
    this.logger.info("\n经度 : " + wxMessage.getLongitude());
    this.logger.info("\n精度 : " + String.valueOf(wxMessage.getPrecision()));
    return null;
}
Also used : TextBuilder(com.github.binarywang.demo.wx.mp.builder.TextBuilder)

Example 4 with TextBuilder

use of com.github.binarywang.demo.wx.mp.builder.TextBuilder 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 5 with TextBuilder

use of com.github.binarywang.demo.wx.mp.builder.TextBuilder 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)5 BaseWxService (com.github.binarywang.demo.wx.mp.service.BaseWxService)3 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)2 WxMpXmlOutMessage (me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage)2 WxMpUser (me.chanjar.weixin.mp.bean.result.WxMpUser)2 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