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

気象庁のXMLから地震情報を抽出してSlack送信

Last updated at Posted at 2024-03-18

こちらの記事を参考に安否確認Botを作成したのですが、ちょっとコード部分だけ気になるところがあったので修正しました。

const SlackToken ="【REPLAECTHIS】";
const SlackChannel = "【REPLAECTHIS】";
const FormUrl = "https://docs.google.com/forms/【REPLAECTHIS】";

const targetPrefs = ['東京都','神奈川県','埼玉県','千葉県','茨城県'];
const targetIntensity = ['5-','5+','6-','6+','7'];

function test() {
  var preDate = null; // new Date().getTime() - 10*60*1000;

  var result = checkNewsXml("https://www.data.jma.go.jp/developer/xml/data/20240314151730_0_VXSE51_010000.xml", preDate);
  Logger.log(result);
  Logger.log(logs2message(result));
}

function checkNewsXml(url, preDate = null) {
  var namespace = XmlService.getNamespace('http://xml.kishou.go.jp/jmaxml1/body/seismology1/');
  var dataHtml  = UrlFetchApp.fetch(url).getContentText();
  var dataDoc   = XmlService.parse(dataHtml);

  var strReportDateTime  = getElementsByTagName(dataDoc, 'TargetDateTime')[0].getValue();
  var reportDateTime = Date.parse(strReportDateTime);

  if (preDate != null && reportDateTime < preDate ) {
    Logger.log("It's old log");
    return null;
  }

  var logs = [];
  getElementsByTagName(dataDoc, 'Pref').forEach(function(value) {
    var pref = value.getChild("Name", namespace).getText();

    if(!targetPrefs.includes(pref)){
      return;
    }

    var maxInt = value.getChild("MaxInt", namespace).getText();
    if(!targetIntensity.includes(maxInt)) {
      return;
    }

    var log = {"area": pref, "maxInt": maxInt};
    Logger.log(log);
    logs.push(log);
  });
  return logs.length > 0 ? {"Date": strReportDateTime , "Logs": logs} : null;
}

function sendSlack(channel, body) {
    var url = "https://slack.com/api/chat.postMessage";
    var payload = {
    "token" : SlackToken,
    "channel" : channel,
    "text" : body
    };
    var params = {
      "method" : "post",
      "payload" : payload
    };
    UrlFetchApp.fetch(url, params); 
}

// http://xml.kishou.go.jp/xmlpull.html
function getAtom() {
  var preDate = new Date().getTime() - 10*60*1000; //10分前
  console.log(preDate);

  var feedUrl   = 'https://www.data.jma.go.jp/developer/xml/feed/eqvol.xml';
  var feedHtml  = UrlFetchApp.fetch(feedUrl).getContentText();
  var feedDoc   = XmlService.parse(feedHtml);
  var feedXml   = feedDoc.getRootElement();
  var feedentry = getElementsByTagName(feedXml, 'entry');
  var atomns      = XmlService.getNamespace('http://www.w3.org/2005/Atom');

  var logs = [];
  feedentry.forEach(function(value, i) {
    var titleText = value.getChild('title', atomns).getText(); //title
    
    //titleが震度速報の場合(震度3以上が震度速報に該当)
    if('震度速報' != titleText){
      return;
    }
    var linkText  = value.getChild('link', atomns).getAttribute('href').getValue(); //link
    var logs = checkNewsXml(linkText, preDate);
    if (logs == null) {
      return;
    }
    sendSlack(SlackChannel, logs2message(logs));
    Logger.log("送信完了");
  });
}

function logs2message(logs) {
  Logger.log(logs);
  Logger.log(logs.Date);
  Logger.log(logs.Logs.map(log => log.area + "(震度" + log.maxInt + ")").join(", "));
  return "<!channel>\n"
    + "【テストです】\n"
    + "【※重要※】安否確認連絡\n"
    + "震度5以上検知しました。\n"
    + "まずはご自身・ご家族の安全を確保されたうえで、\n"
    + "各自下記URLより安否状況のご回答をお願いします。\n"
    + "安否確認フォーム⇒ " + FormUrl + "\n"
    + "※状況が変わった場合には、再回答をお願いいたします。\n"
    + "【地震情報】\n"
    + "地震発生時刻:" + logs.Date + "\n"
    + "地震発生地域:" + logs.Logs.map(log => log.area + "(震度" + log.maxInt + ")").join(", ") + "\n";
}

function getElementsByTagName(element, tagName) {
  var data = [], descendants = element.getDescendants();
  for(var i in descendants) {
    var elem = descendants[i].asElement();
    if ( elem != null && elem.getName() == tagName) data.push(elem);
  }
  return data;
}
1
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
1
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?