LoginSignup
37
35

More than 5 years have passed since last update.

株価をSlackのチャンネルへ自動投稿するGoogle Apps Script

Last updated at Posted at 2015-01-13

スクリーンショット_2015_01_13_19_06.png

Google Apps ScriptでスクレイピングしてSlackにPOSTしてます。

GASで提供されているUrlFetchAppが便利です。

定期投稿は各自好きなタイミングでmyFunctionが実行されるようにトリガーを設定してください(GUIだけでできます)。

コード.gs
var code = "1111" // あなたが見たい会社の証券コード
var slack = {
  postUrl: 'https://slack.com/api/chat.postMessage',
  token: 'xoxp-AAAAAAAAAAAAAAAA', // Slackのtoken
  channelId: "C0BBBBBBBBB",       // 株価ちゃんねるのID
  userName: "株価監視君"            // botの名前
}

var postMessage = function(text) {
  UrlFetchApp.fetch(slack["postUrl"], {
    "method" : "post",
    "payload" : {
      token: slack["token"],
      channel: slack["channelId"],
      username: slack["userName"],
      text: text
    }
  });
}

function myFunction() {
  var response = UrlFetchApp.fetch("http://stocks.finance.yahoo.co.jp/stocks/detail/?code=" + code + ".n");

  var myRegexp = /<td class="stoksPrice">([\s\S]*?)<\/td>/i;
  var match = myRegexp.exec(response.getContentText());
  var amount = match[1];

  Logger.log(amount);
  postMessage("現在の株価: " + amount + "");
}
37
35
2

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