第一步:配置接口配置信息

测试账号在 http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index网页中配置    接口配置信息

配置参考微信测试账号管理-配置接口信息

如果是开发账号请在https://mp.weixin.qq.com/advanced/advanced?action=interface&t=advanced/interface&token=637985175&lang=zh_CN配置页面如下

第二步:修改接收微信响应接口如下

package com.wl.wechat.contoller;import com.wl.wechat.model.WxMessageTemplate;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import java.io.InputStream;
import java.util.Arrays;/*** Created by wl on 2021/4/10.*/
@RestController
@RequestMapping("/wx")
public class WxController {private static final String TOKEN = "wl123";@RequestMapping("/receiveWx")public void receiveWxToken(HttpServletRequest request,HttpServletResponse response) throws Exception{String signature = request.getParameter("signature");String timestamp = request.getParameter("timestamp");String nonce = request.getParameter("nonce");String echostr = request.getParameter("echostr");System.out.println("signature: " + signature);System.out.println("timestamp: " + timestamp);System.out.println("nonce: " + nonce);System.out.println("echostr: " + echostr);String[] params = new String[]{nonce,timestamp,TOKEN};Arrays.sort(params);String signatureResult = DigestUtils.sha1Hex(params[0]+params[1] + params[2]);//校验签名if(!signatureResult.equals(signature)) {throw new RuntimeException("signature is not the same wechat signature is " + signature + " signatureResult is " + signatureResult);}if(StringUtils.isNotBlank(echostr)) {response.getWriter().write(echostr);}InputStream is = request.getInputStream();if(is != null){JAXBContext context = JAXBContext.newInstance(WxMessageTemplate.class);//解析接收到的微信xml格式消息WxMessageTemplate template = (WxMessageTemplate)context.createUnmarshaller().unmarshal(is);String fromUserName = template.getFromUserName();template.setFromUserName(template.getToUserName());template.setToUserName(fromUserName);template.setContent("你好 hello");template.setMsgType("text");response.setCharacterEncoding("utf-8");//返回微信xml消息context.createMarshaller().marshal(template,response.getWriter());}}@RequestMapping("/login")public String login(HttpServletRequest request){return "ok";}}

WxMessageTemplate 如下

package com.wl.wechat.model;import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;/*** Created by wl on 2021/5/30.*/
@XmlRootElement(name = "xml")
public class WxMessageTemplate implements Serializable {private String toUserName;private String fromUserName;private String createTime;private String msgType;private String content;@XmlElement(name = "ToUserName")public String getToUserName() {return toUserName;}public void setToUserName(String toUserName) {this.toUserName = toUserName;}@XmlElement(name = "FromUserName")public String getFromUserName() {return fromUserName;}public void setFromUserName(String fromUserName) {this.fromUserName = fromUserName;}@XmlElement(name = "CreateTime")public String getCreateTime() {return createTime;}public void setCreateTime(String createTime) {this.createTime = createTime;}@XmlElement(name = "MsgType")public String getMsgType() {return msgType;}public void setMsgType(String msgType) {this.msgType = msgType;}@XmlElement(name = "Content")public String getContent() {return content;}public void setContent(String content) {this.content = content;}}

注意响应给微信的 FromUserName 和 ToUserName要互换,响应给微信的xml文本中不能有空格且服务不能发生异常,否则微信会通知用户:该公众号提供的服务出现故障,请稍后重试 的提醒

上面的代码就是当用户向公众号发送文本信息时,公众号都会向用户发送 "你好 hello"的文本消息

测试截图如下

使用jackson处理xml格式的请求 返回

加入依赖

    <dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId><version>2.8.10</version></dependency>

controller代码如下

    @RequestMapping(value = "/receiveWx",produces = MediaType.APPLICATION_XML_VALUE)@ResponseBodypublic Object receiveWxToken(@RequestBody(required = false) WxMessageTemplate template,HttpServletRequest request, HttpServletResponse response) throws Exception{String signature = request.getParameter("signature");String timestamp = request.getParameter("timestamp");String nonce = request.getParameter("nonce");String echostr = request.getParameter("echostr");System.out.println("signature: " + signature);System.out.println("timestamp: " + timestamp);System.out.println("nonce: " + nonce);System.out.println("echostr: " + echostr);String[] params = new String[]{nonce,timestamp,TOKEN};Arrays.sort(params);String signatureResult = DigestUtils.sha1Hex(params[0]+params[1] + params[2]);//校验签名if(!signatureResult.equals(signature)) {throw new RuntimeException("signature is not the same wechat signature is " + signature + " signatureResult is " + signatureResult);}if(StringUtils.isNotBlank(echostr)) {response.getWriter().write(echostr);return null;}String fromUserName = template.getFromUserName();template.setFromUserName(template.getToUserName());template.setToUserName(fromUserName);template.setContent("你好 hello");template.setMsgType("text");return template;}

实体类如下(需要加上jackson xml 的注解)

package com.wl.wechat.model;import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;/*** Created by wl on 2021/5/30.*/
@XmlRootElement(name = "xml")
@JacksonXmlRootElement(localName = "xml")
public class WxMessageTemplate implements Serializable {private String toUserName;private String fromUserName;private String createTime;private String msgType;private String content;@XmlElement(name = "ToUserName")@JacksonXmlProperty(localName = "ToUserName")public String getToUserName() {return toUserName;}public void setToUserName(String toUserName) {this.toUserName = toUserName;}@XmlElement(name = "FromUserName")@JacksonXmlProperty(localName = "FromUserName")public String getFromUserName() {return fromUserName;}public void setFromUserName(String fromUserName) {this.fromUserName = fromUserName;}@XmlElement(name = "CreateTime")@JacksonXmlProperty(localName = "CreateTime")public String getCreateTime() {return createTime;}public void setCreateTime(String createTime) {this.createTime = createTime;}@XmlElement(name = "MsgType")@JacksonXmlProperty(localName = "MsgType")public String getMsgType() {return msgType;}public void setMsgType(String msgType) {this.msgType = msgType;}@XmlElement(name = "Content")@JacksonXmlProperty(localName = "Content")public String getContent() {return content;}public void setContent(String content) {this.content = content;}}

微信公众号自动回复简单实现相关推荐

  1. 微信 SHA1 签名_微信公众号自动回复功能开发

    微信公众号自动回复功能开发 本篇主要讲解 微信公众号自动回复功能开发,让我们自己去托管公众号回复的功能,这样可以更加灵活的根据公众号收到的信息来制定特定的回复信息,一起来了解吧! 1.注册公众号 如果 ...

  2. laravel+easywechat对接微信公众号自动回复图文消息

    laravel+easywechat对接微信公众号自动回复图文消息 图文回复消息创建 对接数据库根据关键词返回图文信息 前面我们对接配置了微信公众号,laravel5.4 对接微信公众号使用larav ...

  3. 关于微信公众号自动回复文本、图片以及图文

    1.微信公众号自动回复文本: 首先我们应该增加reply_id以及内容(content)到数据表rh_mp_reply_text,然后根据reply_id查找表rh_mp_rule的信息关键字.然后我 ...

  4. 微信公众号的简单开发

    这里是修真院前端小课堂,每篇分享文从 [背景介绍][知识剖析][常见问题][解决方案][编码实战][扩展思考][更多讨论][参考文献] 八个方面深度解析前端知识/技能. 今天给大家分享一下,修真院官网 ...

  5. java微信公众号自动回复文字加图片

    java微信公众号自动回复文字加图片 开发流程 详细流程,附上代码: 第一步服务器(url)接口配置 服务器(url)接口配置,此步骤就是微信授权接口的过程,如果域名都不改变,微信只会校验一次.此请求 ...

  6. 怎么设置微信公众号自动回复内容显示用户昵称

    对于微信公众号自动回复内容显示用户昵称,第三方平台微号帮提供了粉丝对话定时推送功能实现,支持公众号设置自动回复消息显示用户昵称,除了可以显示对应用户的昵称,还可以显示用户的头像.openID;用户向公 ...

  7. Flask结合werobot实现微信公众号自动回复

    Flask结合werobot实现微信公众号自动回复 下载依赖包 pip install werobot pip install Flask 建立项目文件夹<flask_project> 建 ...

  8. 微信公众号自动回复功能

    微信公众号自动回复功能分为两种,一种自主开发,一种利用公众号自动开发,两种不可兼容,开发状态的自动回复功能,将导致编辑状态下的菜单,自动回复失效. WeixinchatController代码:  这 ...

  9. 苹果cms对接微信公众号自动回复的样式设置

    此教程针对已经成功对接微信公众号的网站,苹果cms后台对接微信公众号自动回复样式的设置详解,如果你还没有成功对接微信公众号请参考此教程进行对接:https://www.mytheme.cn/artic ...

最新文章

  1. Access-Control-Allow-Origin与跨域
  2. Linux(Centos)快速搭建SVN服务器
  3. 计算机网络基础实验简答题,计算机网络基础实验报告.doc
  4. python生词本的生词_【Anki小工具】有道生词本转Anki 1.0
  5. 一些思维的碎片(二)
  6. 使用IDEA逆向生成实体类时注意问题(Maven)
  7. 交通仿真软件测试自学,交通仿真及常用的仿真软件简介
  8. pip下载 离线安装第三方包
  9. Ethernet/IP以太网接M12 X-Coded 协议:port1(Ethernet连接)
  10. html中搜索框提示语,请输入您要搜索的内容(自定义Win10搜索框提示语的技巧)...
  11. open-flash-chart 与 FusionCharts 开发中使用(转)
  12. 新闻文化建设杂志新闻文化建设杂志社新闻文化建设编辑部2022年第14期目录
  13. 数据分析之excel和finebi报表可视化对比
  14. Latex 表格文字居中(垂直和水平居中)
  15. 一个TCP连接总是以1KB的最大段发送TCP段,发送方有足够多的数据要发送。当拥塞窗口为16KB时发生了超时,如果接下来的4个RTT(往返时间)时间内的TCP段的传输都是成功的,那么当第4个RTT时间
  16. 如何解决Windows 无法完成格式化SD卡问题?
  17. pat乙级 1072 开学寄语 (20 分)C
  18. 电子商务跨考计算机复试被刷吗,复试被刷?多半是和他们一样的原因......
  19. SQL server内嵌表值函数与多语句表值函数
  20. 2015互联网女皇报告中文版

热门文章

  1. ubuntu系统中将U盘格式化为exFAT格式
  2. 项目工程重启后,用RedisTemplate获取不了在redis中存在的数据
  3. 【ASP.NET Core】这可能是因为该站点使用过期的或不安全的 TLS 安全设置
  4. 2020爱分析·中国人工智能厂商全景报告
  5. 企业如何高效进行软文营销呢?
  6. python实现带头结点的单链表的就地逆置_设头指针为head,编写算法实现带头结点单链表head的就地逆置...
  7. 2021-12-1 set 、multiset 深度探索
  8. AI大潮之下,“造物主”码农也要失业了?【智库2861】
  9. 买学生台灯应该怎么选择?学生护眼台灯推荐
  10. 2022国内十大正规现货贵金属交易软件排名