gas
gmail
Slack
SLOGANDay 22

slackのログをGmailで送る

ログ残してるけど、あんま見ないのでgmailでみんなに見える化する

スプレッドシートにslackのログを残す

これでやりました、ありがとうございます。
http://motemen.hatenablog.com/entry/2015/11/gas-slack-log-spreadsheet

gmailで指定のチャンネルのログを流す

スプレッドシートからメール文面を作成し、送信処理を行う

send_mail.gas
// Configuration: Obtain Slack web API token at https://api.slack.com/web
var API_TOKEN = PropertiesService.getScriptProperties().getProperty('slack_api_token');
var FOLDER_NAME = 'Slack Logs';

// メール送信先
var mail_to = [
  "hogehoge@gmail.com",
  "fugafuga@gmail.com"
];

var dObj = new Date();
dObj.setDate(0);

// ここでシートを指定する
var sheetName = "no-name-sheet (C2SDE2D2)";
var body=sheetName+"のaログ\n\n";

var subject = "【slacklog】";

function StoreLogsDelta() {
    var logger = new SlackChannelHistoryLogger();
    logger.run();
}
;
var SlackChannelHistoryLogger = (function () {
    function SlackChannelHistoryLogger() {
        this.memberNames = {};
    }
    // slackの情報をとってくる
    SlackChannelHistoryLogger.prototype.requestSlackAPI = function (path, params) {
        if (params === void 0) { params = {}; }
        var url = "https://slack.com/api/" + path + "?";
        var qparams = [("token=" + encodeURIComponent(API_TOKEN))];
        for (var k in params) {
            qparams.push(encodeURIComponent(k) + "=" + encodeURIComponent(params[k]));
        }
        url += qparams.join('&');
        Logger.log("==> GET " + url);
        var resp = UrlFetchApp.fetch(url);
        var data = JSON.parse(resp.getContentText());
        if (data.error) {
            throw "GET " + path + ": " + data.error;
        }
        return data;
    };
    SlackChannelHistoryLogger.prototype.run = function () {
        var _this = this;
        var usersResp = this.requestSlackAPI('users.list');
        usersResp.members.forEach(function (member) {
            _this.memberNames[member.id] = member.name;
        });
        var teamInfoResp = this.requestSlackAPI('team.info');
        this.teamName = teamInfoResp.team.name;
        this.importChannelHistoryDelta();
    };
    // ログのフォルダ関連
    SlackChannelHistoryLogger.prototype.getLogsFolder = function () {
        var folder = DriveApp.getRootFolder();
        var path = [FOLDER_NAME, this.teamName];
        path.forEach(function (name) {
            var it = folder.getFoldersByName(name);
            if (it.hasNext()) {
                folder = it.next();
            }
            else {
                folder = folder.createFolder(name);
            }
        });
        return folder;
    };
    SlackChannelHistoryLogger.prototype.getSheet = function (d, readonly) {
        if (readonly === void 0) { readonly = false; }
        var dateString;
        if (d instanceof Date) {
            dateString = this.formatDate(d);
        }
        else {
            dateString = '' + d;
        }
        //シートの取得
        var spreadsheet;
        var sheetByID = {};
        var spreadsheetName = dateString;
        var folder = this.getLogsFolder();
        var it = folder.getFilesByName(spreadsheetName);
        if (it.hasNext()) {
            var file = it.next();
            spreadsheet = SpreadsheetApp.openById(file.getId());    
            Logger.log(file.getId());
        }

        var sheets = spreadsheet.getSheets();

        for ( var i in sheets ){
          if ( sheets[i].getSheetName() == sheetName ) {
            var sheet = sheets[i];

            var startrow = 1;
            var startcol = 1;
            var lastrow = sheet.getLastRow();
            var lastcol = sheet.getLastColumn();
            var sheetdata = sheet.getSheetValues(startrow, startcol, lastrow, lastcol);
            // ログの整形
            for (var k=0; k<lastrow; k++){
              for (var j=0; j<3; j++){
                if(j==0){
                  date=sheetdata[k][j];

                  body += '[' + (date.getMonth() + 1) + '/' + date.getDate() + ' ' + date.getHours()+ ':' + date.getMinutes()+':'+ date.getSeconds()+' ] ';
                }
                else{  
                  body+=" | ";
                  body+=(sheetdata[k][j]);
                  body+='\n';
                }
              }
              body+='\n';
            }
          }
        }

      MailApp.sendEmail(
        mail_to,
        subject,
        body
      );

    };
    SlackChannelHistoryLogger.prototype.importChannelHistoryDelta = function () {
        var _this = this;
        var now = new Date();
      // 指定した日付のログををとりたいとき用
      var year = now.getYear();
      var month = now.getMonth();

      subject += (month+1) +"月分";
        var existingSheet = this.getSheet(now, true);
    };
    SlackChannelHistoryLogger.prototype.formatDate = function (dt) {
        return Utilities.formatDate(dt, Session.getScriptTimeZone(), 'yyyy-MM');
    };
    return SlackChannelHistoryLogger;
})();

感想

もっときれいに整形できるはず
無料枠の限界を感じる