微信公众搭建笔记
1、搭建内网穿透
下载http://www.ngrok.cc/user.html
http://lsj.free.idcfengye.com
id:bf06bcc47852ab8b
2、创建订阅号
https://mp.weixin.qq.com/?token=&lang=zh_CN
3、申请测试号、开始接入
https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
4、接入eclipse新键动态工程
1、servlet
System.out.println(“get”);
String signature=request.getParameter(“signature”);
String timestamp=request.getParameter(“timestamp”);
String nonce=request.getParameter(“nonce”);
String echostr=request.getParameter(“echostr”);
//校验请求
if(WxService.check(timestamp,nonce,signature)) {
System.out.println(“接入成功”);
}else {
System.out.println(“接入失败”);
}
}
2、service
package service;
public class WxService {
private static final String TOKEN=“lsj”;
/**验证签名
* @param timestamp
* @param nonce
* @param signature
* @return
*/
public static boolean check(String timestamp, String nonce, String signature) {
// TODO Auto-generated method stub
String []strs =new String [] {TOKEN,timestamp,nonce};
Arrays.sort(strs);
String str =strs[0]+strs[1]+strs[2];
//进行加密
String mysig =sha1(str);
System.out.println(mysig);
System.out.println(signature);
return false;
}
private static String sha1(String str) {
// TODO Auto-generated method stub
try {
//获取一个加密对象
MessageDigest md =MessageDigest.getInstance("sha1");
//加密
byte[]digest =md.digest(str.getBytes());
char[]chars = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
StringBuilder sb=new StringBuilder();
//处理结果
for(byte b:digest) {
sb.append(chars[(b>>4)&15]);
sb.append(chars[b&15]);
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
5、用户接受消息来自微信
1、导入domo4j
2、解析xml数据包
6、被动消息返回
<![CDATA[toUser]]>
<![CDATA[fromUser]]>
12345678
<![CDATA[text]]>
<![CDATA[你好]]>
7、回复消息的封装
1、创建类,类的设计,如文本回复,视频回复,音乐回复等等
8、聊天机器人
1、聊天消息搭建失败原因,Ip接入错误
2、图文消息
9、token的获取与处理
10、菜单栏的封装
1、封装按钮
{“errcode”:0,“errmsg”:“ok”}
2、解析成json格式
JSONObject jsonObject = JSONObject.fromObject(btn);
3、发送请求(要带tokenh和输入流的内容)
String url = “https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN”;
4、成功
String result = Util.post(url, jsonObject.toString());
11、图片识别
1、https://console.bce.baidu.com/ai/?_=&fromai=1#/ai/imagerecognition/app/list
private static final String TOKEN = "lsj"; //换成自己的我微信
private static final String APPKEY="6e0f0e2937617e4b47ab72f33c9836d4"; //天气预报
private static final String GET_TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
//微信公众号
private static final String APPID="wx70e8bda00e6d4d98"; //appid
private static final String APPSECRET="dd5db9041b70be666a3c59bdb673be37";
//百度AI
public static final String APP_ID = "23621588";
public static final String API_KEY = "o2MhHqR2ACzCbxTzByuQvsiH";
public static final String SECRET_KEY = "0kIkb9mtFpkAUBTBe6ikeK5olILND7kZ";
12、模板消息
1、
{{first.DATA}} 公司名:{{company.DATA}} 投递时间:{{time.DATA}} 反馈结果:{{result.DATA}}{{remark.DATA}}
13、获取临时素材
1、图片 by3SMiQS9mGLPzGSYtwh88vPrtx0KBCAARqtjsK3mQWgFw_eLelCvfzLYx5uGkyt
2、获取token()41_41aJOBin0B-CsPi0mDuly0aIqtYY7hDtFmH32tz_u5FnCQ1qnfeBbU9EM4FlLbu7PN53NkUoqWYiPXEirXXyEtMrhN1sUhi0r4Gjqy_CmuMaG80faoyAG3fs4U2O-v8Eny-dTekq-SzQih9sGGXdACAKKB
3、https://api.weixin.qq.com/cgi-bin/media/get?access_token=41_41aJOBin0B-CsPi0mDuly0aIqtYY7hDtFmH32tz_u5FnCQ1qnfeBbU9EM4FlLbu7PN53NkUoqWYiPXEirXXyEtMrhN1sUhi0r4Gjqy_CmuMaG80faoyAG3fs4U2O-v8Eny-dTekq-SzQih9sGGXdACAKKB&media_id=by3SMiQS9mGLPzGSYtwh88vPrtx0KBCAARqtjsK3mQWgFw_eLelCvfzLYx5uGkyt
4、
public void testUpload() {
String file = "F:/DailyLife/picture/lsj.jpg";
String result = WxService.upload(file, "image");
System.out.println(result);
//by3SMiQS9mGLPzGSYtwh88vPrtx0KBCAARqtjsK3mQWgFw_eLelCvfzLYx5uGkyt
}
@Test
public void test() {
System.out.println(WxService.getAccessToken());
//41_41aJOBin0B-CsPi0mDuly0aIqtYY7hDtFmH32tz_u5FnCQ1qnfeBbU9EM4FlLbu7PN53NkUoqWYiPXEirXXyEtMrhN1sUhi0r4Gjqy_CmuMaG80faoyAG3fs4U2O-v8Eny-dTekq-SzQih9sGGXdACAKKB
}
14、二维码生成
15、获取关注
16、获取没有关注