美文网首页
App Store审核结果-发消息提醒到钉钉群

App Store审核结果-发消息提醒到钉钉群

作者: TroyZhang | 来源:发表于2017-10-18 16:38 被阅读142次

    目的

    在App被App Store审核通过或者被拒绝时,及时发送推送消息到钉钉群,提醒开发者及时跟进。

    原理

    原理

    钉钉自定义机器人

    脚本

    // index.js
    var MailListener = require("mail-listener2");
    var request = require('request');
    var fs = require('fs');
    
    // 163邮箱帐号
    var emailAccount = "java-koma@163.com";
    
    // 你的163邮箱登录授权码,和登录密码可能不同。
    var emailAuthCode = "xxxxxx";
    
    var appMap = {};
    var filePath = 'app_list.json';
    
    function getAppInfoByAppId(appId, callback)
    {
      request("https://itunes.apple.com/cn/lookup?id=" + appId, function(error,response,body){
        if(!error && response.statusCode == 200){
          //输出返回的内容
          var resp = JSON.parse(body)
          if(callback && resp && resp.results && resp.results.length > 0)
          {
            callback(resp.results[0].trackName);
            appMap[appId] = resp.results[0].trackName;
    
            fs.writeFile(filePath, JSON.stringify(appMap), function(err){
              if(err) return;
              console.log('save appMap successfully');
            });
          }
        }
      });
    }
    
    function sendMessage(msg)
    {
      if(!msg)
      {
        return;
      }
      var url = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx";
      var jsonMsg = {
        "msgtype": "text",
        "text": {
          "content": msg
        },
        "at": {
          "isAtAll": true
        }
      };
      request({
        url: url,
        method: "POST",
        json: jsonMsg
      }, function (error, resp, body) {
        // console.log(body);
        console.log(resp);
      });
    }
    
    var mailListener = new MailListener({
      username: emailAccount,
      password: emailAuthCode,             // 你的163邮箱授权码
      host: "imap.163.com",
      port: 993, // imap port
      tls: true,
      connTimeout: 10000, // Default by node-imap
      authTimeout: 5000, // Default by node-imap,
      // debug: console.log, // Or your custom function with only one incoming argument. Default: null
      tlsOptions: { rejectUnauthorized: false },
      mailbox: "AppStore", // 我把 App Store的2个邮箱都分类到AppStore中
      searchFilter: ["UNSEEN"], // the search filter being used after an IDLE notification has been retrieved
      markSeen: true, // all fetched email willbe marked as seen and not fetched next time
      fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
      mailParserOptions: {streamAttachments: false}, // options to be passed to mailParser lib.
      attachments: false, // download attachments as they are encountered to the project directory
    });
    
    mailListener.start(); // start listening
    
    // stop listening
    //mailListener.stop();
    
    mailListener.on("server:connected", function(){
      console.log("imapConnected");
    });
    
    mailListener.on("server:disconnected", function(){
      console.log("imapDisconnected");
    });
    
    mailListener.on("error", function(err){
      console.log(err);
    });
    
    mailListener.on("mail", function(mail, seqno, attributes){
      // do something with mail object including attachments
      console.log("##########################");
      console.log(mail.from);
      var res = /your app, (.+) \((\d+)\).+is now Ready for Sale/.exec(mail.subject);
      if(res && res.length > 2)
      {
        getAppInfoByAppId(res[2], function(appName){
          console.log(appName + "审核通过了!");
          sendMessage(appName + "审核通过了!");
        });
      }
      
      res = /New message from App Review for (.+)/.exec(mail.subject);
      if(res && res.length > 1)
      {
        let appName = res[1];
        console.log(appName + "审核被拒了!");
        sendMessage(appName + "审核被拒了!");
      }
    
      res = /Your app "(.+)" \(Apple ID: (\d+)\) has one or more issues/.exec(mail.subject);
      if(res && res.length > 2)
      {
        getAppInfoByAppId(res[2], function(appName){
          console.log(appName + "审核被拒了!");
          sendMessage(appName + "审核被拒了!");
        });
      }
    
      // for(var key in mail)
      // {
      //   console.log("key: " + key);
      // }
      // console.log("emailParsed", mail);
      // mail processing code goes here
    });
    
    fs.readFile(filePath, 'utf-8', function(err, data){
      if(err) return;
      if(data && data.length > 0)
      {
        appMap = JSON.parse(data);
      }
      console.log('appMap: ' + appMap);
    });
    

    运行

    $ touch app_list.json
    
    $ node index.js
    
    // 或者安装`forever`守护进程服务后,以守护进程的方式启动
    $ forever start index.js
    

    相关文章

      网友评论

          本文标题:App Store审核结果-发消息提醒到钉钉群

          本文链接:https://www.haomeiwen.com/subject/risqottx.html