LoginSignup
3
0

More than 1 year has passed since last update.

【GAS】LINE Notifyでめざまし占いを毎日通知する

Last updated at Posted at 2023-03-22

ゴール

毎日8:00~9:00の間に以下のようなメッセージが届く。

image.png

目次

  1. LINE Notifyのトークン生成
  2. GASからLINE通知を送る
  3. めざまし占いのデータを取得
  4. めざまし占いのデータをLINEに送る
  5. GASの定期実行

1. LINE Notifyのトークン生成

Line Notifyのページにアクセス

ログインする

image.png

マイページへ

image.png

トークンを発行
このトークンは後で使うのでメモしておく

image.png

2. GASからLINE通知を送る

コード.gsを以下のように修正してmainを実行

コード.gs
function main() {
  sendMessage("こんにちは")
}

/**
 * LINEに通知を送る
 */
function sendMessage(message){
  const token = "先ほど発行したトークン";
  const url = "https://notify-api.line.me/api/notify";

  const options ={
    "method"  : "post",
    "payload" : {"message": message},
    "headers" : {"Authorization":"Bearer " + token}
  };

  UrlFetchApp.fetch(url, options);
}

以下のようにLINEが届いたら成功。

image.png

3. めざまし占いのデータを取得

以下のURLで今日のめざまし占いのデータが取得できます。
公式にAPIとして公開されているものではないため、利用する際は自己責任でお願いします。

めざまし占いのAPI
https://www.fujitv.co.jp/meza/uranai/data/uranai.json

以下のように書くとめざまし占いのデータが取得できる。

/**
 * めざまし占いのデータを取得
 */
function getForecastData() {
  const url = "https://www.fujitv.co.jp/meza/uranai/data/uranai.json";
  const response = UrlFetchApp.fetch(url).getContentText();
  const json = JSON.parse(response);
  return json;
}

jsonの中身はこんな感じ

占い結果
{
	"date": "2023/03/22",
	"ranking": [
		{
			"advice": "友達からの意見を参考にする",
			"person": "語学力が高い人",
			"name": "おひつじ座",
			"rank": 1,
			"text": "持ち前の行動力を発揮するチャンス!<br>4月に予定している計画は前倒しして。",
			"point": "地図アプリ"
		},
        ...
		{
			"advice": "携帯電話の電源を一度切る",
			"person": "",
			"name": "やぎ座",
			"rank": 12,
			"text": "何げない一言が相手を傷つけるかも。<br>会話する時は言葉のチョイスに注意。",
			"point": "靴べら"
		}
	]
}

4. めざまし占いのデータをLINEに送る

以下のように修正してmainを実行。

コード.js
function main() {
  const { ranking } = getForecastData();
  const sign = "おうし座";
  let { name, rank, text, point } = ranking.find(item => item.name == sign);
  text = text.replaceAll('<br>',''); // brタグを消す
  const message = `\n\n${name} 【第${rank}位】\n${text}\nラッキーアイテム:${point}`;
  sendMessage(message)
}

/**
 * LINEに通知を送る
 */
function sendMessage(message){
  const token = "発行したトークン";
  const url = "https://notify-api.line.me/api/notify";

  const options ={
    "method"  : "post",
    "payload" : {"message": message},
    "headers" : {"Authorization":"Bearer " + token}
  };

  UrlFetchApp.fetch(url, options);
}

/**
 * めざまし占いのデータを取得
 */
function getForecastData() {
  const url = "https://www.fujitv.co.jp/meza/uranai/data/uranai.json";
  const response = UrlFetchApp.fetch(url).getContentText();
  const json = JSON.parse(response);
  return json;
}

以下のような通知が届いたら成功
image.png

5. GASの定期実行

「トリガー」を開く。

image.png

以下のように設定して完了。

image.png

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