肝了一晚上搞出來一個微信訂閱號鑒黃機器人
閱讀本文大概需要 4?分鐘。
來自:網(wǎng)絡(luò)
2、開發(fā)服務(wù)端,并驗證服務(wù)器地址的有效性
3、處理具體的業(yè)務(wù)邏輯
1. 配置微信公眾號


開發(fā)者ID,開發(fā)者調(diào)用的唯一標(biāo)示,調(diào)用接口的時候需要傳遞。
開發(fā)者密碼,這個很重要一定要保存在自己的服務(wù)器上面,用于驗證安全性。
服務(wù)地址,這個就是我們用來接收微信平臺轉(zhuǎn)發(fā)的用戶消息的服務(wù)的地址
令牌,用戶接收信息時候做驗證是否請求來自微信平臺
用于加密消息,防止被截獲,如果 6 設(shè)置為明文模式不需要這個配置。
是否加密傳輸消息

是我們具體的服務(wù)器地址,path是 weixin/receive 這個下文中具體代碼部分會詳細(xì)講解
Token 隨便生成一個 UUID 就可以
隨機生成,后面如果調(diào)用 API 會用到。

2. 編寫服務(wù)端
<repositories>
????<repository>
????????<id>developer-weapons-repositoryid>
????????<url>https://raw.githubusercontent.com/developer-weapons/repository/masterurl>
????repository>
repositories>
<dependency>
????<groupId>com.github.developer.weaponsgroupId>
????<artifactId>wechat-spring-boot-starterartifactId>
????<version>1.2.6version>
dependency>
@Autowired
private?WechatOfficialService?wechatOfficialService;
@Value("${weixin.token}")
private?String?token;
@RequestMapping(value?=?"/weixin/receive",?method?=?RequestMethod.GET)
public?void?receive(
????????@RequestParam(value?=?"signature")?String?signature,
????????@RequestParam(value?=?"timestamp")?String?timestamp,
????????@RequestParam(value?=?"nonce")?String?nonce,
????????@RequestParam(value?=?"echostr")?String?echostr,
????????HttpServletResponse?response)?throws?IOException?{
????boolean?valid?=?wechatOfficialService.isValid(signature,?timestamp,?nonce,?token);
????PrintWriter?writer?=?response.getWriter();
????if?(valid)?{
????????writer.print(echostr);
????}?else?{
????????writer.print("error");
????}
????writer.flush();
????writer.close();
}

3. 處理業(yè)務(wù)邏輯
@RequestMapping(value?=?"/weixin/receive",?method?=?RequestMethod.POST)
public?void?receive(
????????@RequestParam(value?=?"signature")?String?signature,
????????@RequestParam(value?=?"timestamp")?String?timestamp,
????????@RequestParam(value?=?"nonce")?String?nonce,
????????HttpServletRequest?request,
????????HttpServletResponse?response)?throws?IOException?{
????request.setCharacterEncoding("UTF-8");
????response.setCharacterEncoding("UTF-8");
????boolean?valid?=?wechatOfficialService.isValid(signature,?timestamp,?nonce,?token);
????PrintWriter?writer?=?response.getWriter();
????if?(!valid)?{
????????writer.print("error");
????????writer.flush();
????????writer.close();
????????return;
????}
????try?{
????????Map?map?=?wechatOfficialService.toMap(request.getInputStream());
????????if?(map.get("MsgType").equals("image"))?{
????????????String?msg?=?OfficialAutoReplyMessage.build()
????????????????????.withContent("接收到圖片鏈接為:"?+?map.get("PicUrl"))
????????????????????.withMsgtype(MessageTypeEnum.TEXT)
????????????????????.withFromUserName(map.get("ToUserName"))
????????????????????.withToUserName(map.get("FromUserName"))
????????????????????.toXml();
????????????writer.print(msg);
????????????writer.flush();
????????????writer.close();
????????????return;
????????}
????}?catch?(Exception?e)?{
????????log.error("WeixinController?receive?error",?e);
????}
????writer.print("success");
????writer.flush();
????writer.close();
}
wechatOfficialService.toMap?方法解析出接收消息的內(nèi)容,當(dāng)前判斷比較簡單,直接判斷是否是圖片消息,然后返回圖片的 URL 給發(fā)送消息的用戶。效果圖如下
if?(map.get("MsgType").equals("image"))?{
??String?res?=?checkService.check(publicKey,?privateKey,?map.get("PicUrl"));
??OfficialAutoReplyMessage?officialAutoReplyMessage?=
??????????OfficialAutoReplyMessage.build()
??????????????????.withMsgtype(MessageTypeEnum.TEXT)
??????????????????.withFromUserName(map.get("ToUserName"))
??????????????????.withToUserName(map.get("FromUserName"));
??if?(StringUtils.equals("forbid",?res))?{
??????officialAutoReplyMessage.withContent("小哥,你的圖片有點問題哦");
??}?else?{
??????officialAutoReplyMessage.withContent("騷年,你這圖片剛剛的沒問題");
??}
??writer.print(officialAutoReplyMessage.toXml());
??writer.flush();
??writer.close();
??return;
}

推薦閱讀:
內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper、數(shù)據(jù)結(jié)構(gòu)、限流熔斷降級......等技術(shù)棧!
?戳閱讀原文領(lǐng)取!? ? ? ? ? ? ? ??? ??? ? ? ? ? ? ? ? ? ?朕已閱?

