3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【GAS】IPAのセキュリティ情報を自動配信する仕組み

Last updated at Posted at 2019-07-18

背景

「セキュリティに関する知識共有」の一環として、
IPAから発信されている重要なセキュリティ情報を社員同報にて送信する仕組みを構築する

※既存のRSSサービスでは不要な情報が混じるので、必要な情報に絞って抽出することが目的

構成

gsuit (2).png

※構成図は、▽のツールがアイコン豊富で、便利でした☺
https://www.draw.io

| 項目 | 内容 |
|:-----------|------------:|:------------:|
| 言語 | GAS | This |
| DB | GSS| column |

スプレッドシートをDBとする
・送信文面、日付、配信するニュースをスプレッドシートに平日の毎朝 8:39に書き込む(8:50のトリガーで設定しているが、なぜか8:39...)

・スプレッドシートを参照し、スクレイピングして取得したデータとの差分を見て、相違があればメール配信する方式

機能ごと

①スクレイピング

UrlFetchApp.fetch()を使って、POST

スクレイピング

function myFunction() {
  //毎日8時50分頃に実行する
 var response = UrlFetchApp.fetch("https://www.ipa.go.jp/security/rss/alert.rdf");
 var content = response.getContentText();
 );

②日付比較

あらかじめ書き込まれている前回日付をチェック

スプレッドシートと日付比較
SpreadsheetApp.openById(PropertiesService.getScriptProperties().getProperty("key")
 var sheet = spreadsheet.getSheetByName('IPA情報セキュリティ');
 var today =new Date();

  // スプレッドシートから前回送信日時を取得する
  var columnBVals = sheet.getRange('B:B').getValues(); 
  var LastRow = columnBVals.filter(String).length; 
  Logger.log(LastRow)
  var beforeday = sheet.getRange('B'+LastRow).getValue();

③パース

戻り電文を整形

XMLパース
  //XMLパース(ライブラリを使用)
   var doc   = XmlService.parse(content),
       xml   = doc.getRootElement(),
       title = parser.getElementsByTagName(xml, 'title');
       link = parser.getElementsByTagName(xml, 'link');
       date = parser.getElementsByTagName(xml, 'date');

④メール送信

ニュースが一件以上で、送信

メール送信

       var news = [];
       var links =[];
         
       var count =0;
       for (var i=1; i<=10; i++) {       
       //ニュースの発表日取得
         year = date[i].getValue().substring(0,4);
         month =  date[i].getValue().substring(5, 7);
         day = date[i].getValue().substring(8, 10);
         newday = new Date(year,month-1,day) ;
        
   if(newday >= beforeday){
     sendday = newday;
     news.push("No."+(count+1)+""+title[i].getValue()+"\n");
     links.push(link[i].getValue()+"\n");
     count++;
    }
    }
  //todayの内容があればメール送信 
   var mailTitle = "【自動送信】IPAセキュリティセンター:重要なセキュリティ情報";
   var mailBody = "";
  
  if(news.length>0){
    
    mailBody += "各位\n\n";
    mailBody += "お疲れさまです。
    mailBody += "IPAより重要なセキュリティ情報が発表されましたので、\nお知らせいたします。\n";
    mailBody += "\n以下のリンクよりご確認お願いします。\n\n";
    mailBody += trim(news.toString());
    mailBody += trim(links.toString())+"\n\n";
    mailBody += "以上です宜しくお願いいたします";

 //送信する
    var to = "";
   MailApp.sendEmail(to,mailTitle,mailBody)
    Logger.log(mailBody)

⑤転記

日付、配信するニュース、送信文面をスプレッドシートに書き込む

スプレッドシートに送信内容を転記
  
   //スプレッドシートに送信する時間・ニュース・メール本文を転記※
    sheet.getRange("B"+(LastRow+1)).setValue(today) ;
    sheet.getRange("C"+(LastRow+1)).setValue(news) ;
    sheet.getRange("D"+(LastRow+1)).setValue(mailBody) ;
  
  }

⑥その他

配列で複数ニュースがあるとき、カンマが余分に入ってしまうので
除く

その他(ニュース複数件の場合、カンマ除去)
  // trim関数を定義する
function trim(target){
  if (target == null || target == undefined){
    return "";
  }
  return target.replace(",", "");
}
} 
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?