如何零成本实现微信公众号自助查券返利机器人(二)

前提概要

	手把手教你制作一个属于自己的自助查券机器人,后台服务基于java(springboot+maven),需要一定的开发能力或运维能力

总体流程

  1. 注册微信公众号;
  2. 对接微信公众平台自动回复功能;
  3. 注册natapp获取免费隧道;
  4. 注册淘宝联盟获取应用相关参数;
  5. 注册折淘客网站获取我们所需的api接口;
  6. 对接api接口;
  7. 如果有服务器部署到服务器上,没有就在本地运行

上一步

如何零成本制作一个java版微信公众号自助查券机器人(一)

对接微信公众平台自动回复功能

代码结构:
代码结构
注意
需要安装lombok插件

maven依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.9</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>  
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <!--常用库依赖-->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>   
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!-- dozer对象拷贝 -->
        <dependency>
            <groupId>net.sf.dozer</groupId>
            <artifactId>dozer</artifactId>
            <version>5.5.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>com.github.dongfg.plugin</groupId>
            <artifactId>mybatis-generator-lombok-plugin</artifactId>
            <version>1.0.0</version>
            <scope>provided</scope>
        </dependency>

WechatIndexController.class

@Slf4j
@RestController
@RequestMapping("/index")
public class WechatIndexController {

    @Autowired
    private MessageService messageService;

    @GetMapping
    public void get(HttpServletRequest request, HttpServletResponse response) {
        // 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        log.info("请求参数:signature为[{}],timestamp为[{}],nonce为[{}],echostr为[{}]", signature, timestamp, nonce, echostr);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,否则接入失败
            if (SignUtil.checkSignature(signature, timestamp, nonce)) {
                out.print(echostr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
            out = null;
        }
    }



    @PostMapping
    public void post(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
        }
        response.setContentType("text/html;charset=UTF-8");

        // 调用核心业务类接收消息、处理消息
        String respMessage = messageService.newMessageRequest(request);

        // 响应消息
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.print(respMessage);
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
        } finally {
            out.close();
            out = null;
        }
    }
}

MessageService.class

public interface MessageService {

    /**
     * 微信公众号处理
     * @param request
     * @return
     */
    String newMessageRequest(HttpServletRequest request);
}

MessageServiceImpl.class

@Slf4j
@Service("messageService")
public class MessageServiceImpl implements MessageService {

    /**
     * 注入淘宝服务
     */
    @Autowired
    TbService tbService;

    @Override
    public String newMessageRequest(HttpServletRequest request) {
        String respMessage = null;
        try {
            String decrypt = MessageDecrypt.decrypt(request);
            // xml请求解析
            Map<String, String> requestMap = MessageUtil.xmlToMap(decrypt);
            // 发送方帐号(open_id)
            String fromUserName = requestMap.get("FromUserName");
            // 公众帐号
            String toUserName = requestMap.get("ToUserName");
            // 消息类型
            String msgType = requestMap.get("MsgType");
            // 消息内容
            String content = requestMap.get("Content");
            log.info("FromUserName is:" + fromUserName + ", ToUserName is:" + toUserName + ", MsgType is:" + msgType + ", content is:" + content);
            // 文本消息
            if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
                //这里根据关键字执行相应的逻辑
                /*if(content.equals("xxx")){
                }*/
                //自动回复
                VxTextMassage text = new VxTextMassage();
                text.setContent(dealContent(content));
                text.setToUserName(fromUserName);
                text.setFromUserName(toUserName);
                text.setCreateTime(new Date().getTime());
                text.setMsgType(msgType);
                respMessage = MessageUtil.textMessageToXml(text);
            }
            // 事件推送
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
                String eventType = requestMap.get("Event");// 事件类型
                // 订阅
                if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
                    //文本消息
                    VxTextMassage text = new VxTextMassage();
                    text.setContent(genMsg());
                    text.setToUserName(fromUserName);
                    text.setFromUserName(toUserName);
                    text.setCreateTime(new Date().getTime());
                    text.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
                    respMessage = MessageUtil.textMessageToXml(text);
                }
                // 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息
                else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {// 取消订阅

                }
            }
        }
        catch (Exception e) {
            log.error("error......");
        }
        return respMessage;
    }

    /**
     * 处理查询逻辑
     * @param content
     * @return
     */
    private String dealContent(String content) {
        try {
            String couponMsg = tbService.queryCoupon(content);
            if (couponMsg == null || couponMsg.length() == 0 ) {
                return "暂时找不到该商品的优惠卷哦,请点击公众号下方联系某某";
            }
            return couponMsg;
        } catch (Exception e) {
            log.info(e.getMessage());
            return "暂时找不到该商品的优惠卷哦,请点击公众号下方联系某某";
        }
    }

    /**
     * 关注自动回复
     * @return
     */
    private String genMsg() {
        StringBuffer sb = new StringBuffer();
        sb.append("欢迎关注正好想买\n");
        sb.append("搜罗全网优惠,省钱即赚钱 \n\n");
        sb.append("如何查卷:\n");
        sb.append("1.复制商品链接\n");
        sb.append("2.粘贴到公众号\n");
        sb.append("3.将返回的优惠卷信息复制打开陶宝即可\n\n");
        sb.append("如何反俐:\n");
        sb.append("1.确认收货后,添加客服获取反俐虹包\n");
        sb.append("客服vx号:zhxmfq");
        return sb.toString();
    }
}

注意
由于工具类的代码过多,以免篇幅过长,秋秋群文件获取完整【代码】
秋秋群沟通:719023986
看效果:威x关注:正好想买

接下来完成微信服务器发送消息到本地功能
传送门:如何零成本制作一个java版微信公众号自助查券机器人(三)

写在最后

非常感谢大家的认真阅读,写的好的话可以支持一下博主=_=

微x关注:干饭必备外卖神券,每天领大额外卖卷
微x关注:正好想买,自助查桃宝京d卷

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值