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

然后進(jìn)入配置頁(yè)面,我們一一對(duì)配置進(jìn)行講解

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

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

2. 編寫(xiě)服務(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<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?方法解析出接收消息的內(nèi)容,當(dāng)前判斷比較簡(jiǎn)單,直接判斷是否是圖片消息,然后返回圖片的 URL 給發(fā)送消息的用戶(hù)。效果圖如下:
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("小哥,你的圖片有點(diǎn)問(wèn)題哦");
??}?else?{
??????officialAutoReplyMessage.withContent("騷年,你這圖片剛剛的沒(méi)問(wèn)題");
??}
??writer.print(officialAutoReplyMessage.toXml());
??writer.flush();
??writer.close();
??return;
}
評(píng)論
圖片
表情
