LoginSignup
1
3

More than 3 years have passed since last update.

LINEに自動投稿(GASとSpreadsheetで)

Last updated at Posted at 2020-08-01

動機

LINEにのグループチャットに「今日のギリシャ語単語!」を投げてるんですが、自動投稿にしたくなった。

目標

LINEにgoogle spreadsheetから情報をもらって定期自動投稿する

方法

基本はこちらを参考にしました。
https://hoshino-wp.com/line-notify-google-apps-script/
大変お世話になりました。ありがとうございます。
(LINE Notifyを目的のグループに招待するの忘れてて一日うまくいかなかった。)

こちらで1日一回apps scriptを走らせる設定にしました。

spreadsheetにはこんな感じの内容があるとしましょう。

date content
2020/08/01 Ἰησοῦς: イエス(固有名詞)
2020/08/02 Ἰούδας: ユダ(固有名詞)

以下、具体的なコードの編集内容です。

今日の新約聖書ギリシャ語単語
const url = "https://docs.google.com/spreadsheets/hogehoge";
const lineToken = 'qawsedrftgyhujiko';
const timeZone = 'Asia/Tokyo';
const timeFormat = 'yyyy/MM/dd';

//送信内容を作って送信
function postContent() {
  var content = createContent();
  sendPostContent(content);
}

//LINE Notifyへpost
function sendPostContent(content) {
  var token = [lineToken];
  var options = {
    "method": "post",
    "payload" : {"message": content },
    "headers": {"Authorization": "Bearer " + token}    
  };
  UrlFetchApp.fetch("https://notify-api.line.me/api/notify", options);
}

//送信内容を作ります
function createContent() {
  const today = getFormattedDate();

  const ss = SpreadsheetApp.openByUrl(url);
  const ws = ss.getSheetByName("単語");
  const captionRow = ws.getRange(1, 1, 1, ws.getLastColumn()).getValues();
  //日付の書いてある列番号を取得
  const dateColNum = captionRow[0].indexOf("date");
 //内容のまとめてある列番号を取得
  const contentColNum = captionRow[0].indexOf("content");

  //カラム名の下にある全データを取得
  const vocabData = ws.getRange(
     2, 
     1,
     ws.getLastRow(),
     ws.getLastColumn()
  ).getValues();

  const content = "\n\n" + 
    vocabData.filter(function(row){
      //日付が書いてないやつは無視します。
      if (row[dateColNum] == null){return false;};
      const plannedDate = getFormattedDate(row[dateColNum]);
   //日付が今日のものだけ選びます。
      return (plannedDate == today);
    }).map(function(row){
      //内容だけ抽出します。
      return row[contentColNum];
    }).reduce(function(previousValue, currentValue){
   //改行でつなげます。
      return previousValue + "\n\n" + currentValue;
    });

  console.log(content);
  return content;
}

function getFormattedDate(some){
  if (some == null){ 
    return Utilities.formatDate(new Date(), timeZone,timeFormat);
  }
  const target = Utilities.formatDate(new Date(some), timeZone,timeFormat);
  //console.log(target);
  return  target;
}

日付の比較だけなかなかスマートにいかないのですが、こちらを参考にさせていただきました。
https://qiita.com/rf_p/items/f423ed2b23b789b2ebe9

感想

毎日ちょっとずつやるのって難しいので自動化したいですよね。
Twitterのbotとかもみんなこんなん書いてるんですかね。
書いたコードをgoogleにおいておくだけっていうのはすごくいいなと思いました。


#ERROR!が送信された(2020/08/04追記)

LINE Notifyから"#ERROR!"の文字列が送信された。明らかにspreadsheet内のエラーである。
しかし当該のspreadsheetにerrorの文字列はない。
とはいえ心当たりはあって、apps scriptでカスタム関数を書いたせいだと思われる。
どういう処理の順番になってるのかわからないが、カスタム関数のスクリプトが走る前にLINE Notifyのスクリプトが動いたと考えるのが自然だ。

だいいち、spreadsheetって開くたんびにカスタム関数計算し直すから5000行も計算させるんじゃなかった。
というわけで、値を改めて貼り付けて生のデータにしました。

明日はうまく行くよね。

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