微信平臺發(fā)送消息給我們的服務(wù)器 -> 我們的服務(wù)器處理消息 -> 返..." />
<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          厲害了!手?jǐn)]一個微信訂閱號鑒黃機器人!用起來很可以!

          共 8587字,需瀏覽 18分鐘

           ·

          2020-12-05 20:51

          點擊藍色“Python交流圈”關(guān)注我丫

          加個“星標(biāo)”,每天一起進步一點點

          顧名思義,我們就是來做一個訂閱號機器人,大致是這樣一個過程

          公眾號接收用戶消息 -> 微信平臺發(fā)送消息給我們的服務(wù)器 -> 我們的服務(wù)器處理消息 -> 返回處理結(jié)果給微信平臺 -> 微信平臺發(fā)送內(nèi)容給用戶。

          基于這樣一個大前提就有了下面的步驟。

          1、填寫服務(wù)器配置,可以接收微信平臺發(fā)送的內(nèi)容
          2、開發(fā)服務(wù)端,并驗證服務(wù)器地址的有效性
          3、處理具體的業(yè)務(wù)邏輯

          1. 配置微信公眾號

          首先肯定需要有一個訂閱號,然后在訂閱號后臺點擊 開發(fā)者->基本配置進入如下頁面,點擊確定

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

          1. 開發(fā)者ID,開發(fā)者調(diào)用的唯一標(biāo)示,調(diào)用接口的時候需要傳遞。

          2. 開發(fā)者密碼,這個很重要一定要保存在自己的服務(wù)器上面,用于驗證安全性。

          3. 服務(wù)地址,這個就是我們用來接收微信平臺轉(zhuǎn)發(fā)的用戶消息的服務(wù)的地址

          4. 令牌,用戶接收信息時候做驗證是否請求來自微信平臺

          5. 用于加密消息,防止被截獲,如果 6 設(shè)置為明文模式不需要這個配置。

          6. 是否加密傳輸消息

          我們本期只做接收圖片消息,驗證完成以后回復(fù)消息,所以只需要配置 3、4。

          1. 是我們具體的服務(wù)器地址,path是 weixin/receive 這個下文中具體代碼部分會詳細講解

          2. Token 隨便生成一個 UUID 就可以

          3. 隨機生成,后面如果調(diào)用 API 會用到。

          這時候你點擊提交會提示驗證失敗,是因為你還沒有部署 API,配置到這里我們就開始編寫代碼。

          2. 編寫服務(wù)端

          服務(wù)器端使用現(xiàn)有的輪子非常簡單,因為是 spring-boot 項目,直接引入一個現(xiàn)成的微信 starter,一定要添加 repository ,這個是依托 Github 自帶的倉庫。

          <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>

          然寫兩個接口,一個 GET 用于第一次綁定微信后臺驗證用,一個 POST 用于以后接收消息 /weixin/receive

          把之前準(zhǔn)備好的 token 配置到 application.properties 然后注入到 Controller 里面,大致的驗證代碼如下,如果驗證簽名成功就返回 echostr,算是通信的標(biāo)示,如果驗證失敗返回 error。

          @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();
          }

          編寫到這里就可以找一個服務(wù)器部署起來,點擊驗證嘍,這時候點擊提交直接成功了,點擊啟用以后就生效了,生效以后你原來配置的自動回復(fù)就會生效,所以這個操作請謹(jǐn)慎。

          3. 處理業(yè)務(wù)邏輯

          處理業(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ā)送消息的用戶。效果圖如下

          那么接下來就到了最關(guān)鍵的一步。

          按照上面的文章修改代碼后結(jié)果如下,具體的 publicKey 和 privateKey 自己參考下哦

          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;
          }

          最終效果如下

          所以你會搭建自己的鑒黃機器人了嗎?

          獲取項目源碼:

          掃描下方二維碼,回復(fù)源碼


          --End--


                 
          近期熱門推薦?

          1、爬蟲:使用 MySQL 維護 IP 代理池

          2、大廠的 404 頁面都長啥樣?看到最后一個,我笑了。。。

          3、爬取QQ音樂全部歌曲

          4、11 月全國程序員平均工資出爐,看你拉后退了沒

          5、用 Python 使用 Google Colab?豈止是炫酷

          6、Ubuntu被曝嚴(yán)重漏洞:切換系統(tǒng)語言+輸入幾行命令,就能獲取root權(quán)限

          7、Python 爬蟲之某殼公寓投訴分析

          點贊最大的支持 

          瀏覽 51
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日本黄色视频网站在线观看 | 色网站免费 | 2024黄色网址 | 一区二区三区网站 | 天天日天天肏 |