URLをコピーしました!
スキルアップを始める!

【GAS(GoogleAppsScript)】毎日名言をつぶやいてくれるLINEBotを作成してみる#03

GAS(GoogleAppsScript)
\ワードプレスのスキルアップはこちら!/ WordPress入門読本

ひとまずBot完成

前回までで、ひとまずBotらしきものが完成しました。あとは、トリガーをセットして、1日1回朝に配信・・・などとすればOKです。

名言

ということで、Googleシートに名言リストを作成して…と思いましたが、名言APIみたいなものは無いかな…?と思ったところ、ありました。

古今名言集~座右の銘にすべき言葉~
http://www.kokin.rr-livelife.net/rss/rss.html

RSSをXML(Extensible Markup Language)形式で取得するので、前回と取り方を変えてみます。

XML Service
https://developers.google.com/apps-script/reference/xml-service

function QuotationsBot() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet_name = sheet.getSheetByName(‘名言’);
// RSSのURL設定
var feedURL = “http://www.kokin.rr-livelife.net/rss/rss20.php”;
// RSSを取得
var response = UrlFetchApp.fetch(feedURL);
// XMLをパース
var xml = XmlService.parse(response.getContentText());
// 各データの要素を取得する
var entries = xml.getRootElement().getChildren(“channel”)[0].getChildren(“item”);
// 要素数を取得
var length = entries.length;
// 記事タイトル
var title;
// 取得したデータをループさせる
for(var i = 0; i < length; i++) {
title = entries[i].getChildText(“title”);
// シートに出力
sheet_name.getRange(i+1,1).setValue(title);
}
}

取得できました!!

Bot配信

UrlFetchApp.fetch(“https://api.line.me/v2/bot/message/push”, {
“method”: “post”,
“headers”: {
“Content-Type”: “application/json”,
“Authorization”: “Bearer ” + Channel_Access_Token,
},
“payload”: JSON.stringify({
“to”: userId,
“messages”: [{
“type”: “text”,
“text”: message,
}]
})
});

あとは、トリガーを設定して、「1日1回、朝配信する」など設定すればBot完成です!

配信Bot完成!

注意点としては、メッセージ配信できる上限が無料アカウントの場合は1000通となっています。「お友だち数が1000人」いたら「毎月1通」しか配信できなくなってしまうので注意が必要です。

LINE公式アカウント 料金プラン
https://www.linebiz.com/lp/line-official-account/plan/

ご参考下さい^o^)/

Kindle Unlimited 会員は無料で購読できます
購読はこちら
Kindle Unlimited 会員は無料で購読できます
購読はこちら