肝了一晚上搞出來一個微信訂閱號鑒黃機器人
顧名思義,我們就是來做一個訂閱號機器人,大致是這樣一個過程
公眾號接收用戶消息 -> 微信平臺發(fā)送消息給我們的服務器 -> 我們的服務器處理消息 -> 返回處理結果給微信平臺 -> 微信平臺發(fā)送內容給用戶。
基于這樣一個大前提就有了下面的步驟。
1、填寫服務器配置,可以接收微信平臺發(fā)送的內容
2、開發(fā)服務端,并驗證服務器地址的有效性
3、處理具體的業(yè)務邏輯
1. 配置微信公眾號
首先肯定需要有一個訂閱號,然后在訂閱號后臺點擊 開發(fā)者->基本配置進入如下頁面,點擊確定

然后進入配置頁面,我們一一對配置進行講解

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

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

2. 編寫服務端
<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è)務邏輯
@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<String,?String> 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?方法解析出接收消息的內容,當前判斷比較簡單,直接判斷是否是圖片消息,然后返回圖片的 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;
}
評論
圖片
表情
