微信公众平台接口说明:
http://mp.weixin.qq.com/wiki/index.php?title=%E9%A6%96%E9%A1%B5
1、 注册
如果还没有一个微信公众平台账号,那么我们首先进行注册:
注册页面地址:
http://mp.weixin.qq.com/cgi-bin/readtemplate?t=wxm2-realname-reg&lang=zh_CN
特别要注意,注册个人公共账号时,需要提供证件照片,要求如下:
1、 准备服务器
由于我们要使用微信公众平台提供的接口实现自动回复等功能,所以我们需要有自己的信息处理服务器。一个简单的信息处理过程如下图:
我们的服务器端可以解析收到的微信消息,并对用户动作进行反馈。
当前我们测试用的服务器搭建在sina app engine上,搭建流程见《Sina app engin 简单使用》
服务器端主要java代码如下:
网址接入代码:(用于启用消息接口)
@RequestMapping("checkJoin")
@ResponseBody
public String checkJoin(HttpServletRequest request, HttpServletResponse response,
String signature,String timestamp, String nonce,String echostr) throws IOException {
if(echostr!=null){//为验证请求
return echostr;
}
处理用户文本信息代码:(在消息接口启用后使用)
@RequestMapping("checkJoin")
@ResponseBody
public String checkJoin(HttpServletRequest request, HttpServletResponse response,
String signature,String timestamp, String nonce,String echostr) throws IOException {
String postStr=null;
try{
postStr=this.readStreamParameter(request.getInputStream());
}catch(Exception e){
e.printStackTrace();
}
if (null!=postStr&&!postStr.isEmpty()){
Document document=null;
try{
document = DocumentHelper.parseText(postStr);
}catch(Exception e){
e.printStackTrace();
}
if(null==document){
return"";
}
Element root=document.getRootElement();
String fromUsername = root.elementText("FromUserName");
String toUsername = root.elementText("ToUserName");
String keyword = root.elementTextTrim("Content");
String msgId = root.elementTextTrim("MsgId");
String time = new Date().getTime()+"";
String textTpl = "<xml>"+
"<ToUserName><![CDATA[%1$s]]></ToUserName>"+
"<FromUserName><![CDATA[%2$s]]></FromUserName>"+
"<CreateTime>%3$s</CreateTime>"+
"<MsgType><![CDATA[%4$s]]></MsgType>"+
"<Content><![CDATA[%5$s]]></Content>"+
"<FuncFlag>0</FuncFlag>"+
"</xml>";
if(null!=keyword&&!keyword.equals(""))
{
String msgType = "text";
String contentStr =keyword+ " 你好";
String resultStr = textTpl.format(textTpl, fromUsername, toUsername, time, msgType, contentStr);
weixin.setFromUserName(fromUsername);
weixin.setCreateTime(time);
weixin.setContent(contentStr);
weixin.setMsgType(msgType);
weixin.setToUserName(toUsername);
weixin.setMsgId(msgId);
return resultStr;
}else{
return "Input something...";
}
}else {
return "";
}
}
//从输入流读取post参数
public String readStreamParameter(ServletInputStream in){
StringBuilder buffer = new StringBuilder();
BufferedReader reader=null;
try{
reader = new BufferedReader(new InputStreamReader(in));
String line=null;
while((line = reader.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(null!=reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buffer.toString();
}
1、 启用消息接口
登陆微信公众平台,选择:高级功能,可以看到页面上有‘编辑模式’和‘开发模式’两个选择。默认开启‘编辑模式’。
进入编辑模式并关闭。
进入开发模式,点击成为开发者进入如下页面:
特别注意是URL是你在‘2准备服务器’中自己服务器的访问路径(即接收微信服务器发送信息的url地址),必须以http://开头,且为80端口。
Token 可以自定义。
填好后点击提交,这会有下面这些结果
( 1 ) 你的服务器没有正确响应Token验证,请阅读消息接口使用指南
这样回头检查一下各项配置是否正确。如果确定配置没有问题,请按下面的方法检查
(2)请求URL超时(超过5s)
你的服务器在国外,或者服务器网速不给力,一般多试几次就可以了。如果经常这样,就需要考虑更换服务器
(3)提交成功
恭喜你,配置成功了。
1、 功能测试:
(1) 使用普通账号关注我们的公众账号。
(2) 发送消息‘1’给公众账号。
(3) 如果公众账号返回‘1你好’。则测试成功。