人間風のリマインダーLINEボットが作りたい。
前回の記事で、IFTTTを使うことによってGoogleカレンダーとTwitterを連携させて、自動ツイートをしました。
実はGoogleカレンダー×LINEも可能で、簡単に連携させられるんです。
ただ、その場合、「LINE Nofty」というアカウントから通知が行くことになります。



簡単なんだけど、ちょっと違うんだよな!
そう、私はできることならアニメのキャラクターや実在する人間風のアカウントがカレンダー通知してくれたらいいのにと思ったのです。
カレンダー通知リマインダー作り方 LINE BOTアカウント作成
LINEの仕様が変わっています。 https://manager.line.biz/account にアクセスします。

私は普段使っているLINEアカウントでログインしました。ビジネスアカウントを作ってもいいですね。

「コンソールホーム」→「作成する」をクリックします。

プロバイダ名は適当に付けます。

「メッセージAPI」をクリックして作成します。

著作権的にだめかなぁ~?とは思いつつ、アイコンにいのすけにしてみました。許可されないときにはイノシシにでもしちゃいます。

このように入力して完成。

チェックを付け、「作成する」をクリック。これでLINEボットの下準備が完成です。
こちらを参考にさせていただきました。
カレンダー通知リマインダー作り方 アクセストークン等のメモ。


「自動返信メッセージ」は、「編集」をクリックして、無効にしておきます。
次に「メッセージングAPI」を押しましょう。下のほうまで行くと、「チャンネルアクセストークン」という箇所がありますので、そこにある「問題」というボタンを押しましょう。

するとアクセストークンというパスワードのようなものが表示されます。このアクセストークンをコピーしておきましょう。

Linebotのアクセストークンが取得できないとき
ここで少しつまずきました。ラインの設定画面が変わっていて、アクセストークンが取得できません。

画面下部にある、 LINE Developersをクリックします。 https://developers.line.biz/ja/
ログインが完了すると、右上に自分のラインアイコンがあるので、クリック。

プロバイダリストに、先ほど作ったbotがありました。クリックして、下に進んでいくと、【アクセストークン】の発行の箇所がありました。

アクセストークンの『再発行』をクリック。

LINE BOTとGoogleスプレッドシート・GASを連携する
こちらからGoogleスプレッドシートに飛びましょう。


「+」を押しましょう。ファイルを新規作成します。
ファイルの名前はおまかせします。

シート名は「alart」にします。シートにはこんな感じで打ち込みましょう。

コピペする人はこちらから。
年 | 月 | 日 | 第n週 | 曜日 | 時 | 分 | 本文 |
5 | 20 | 0 | 0 | 今日はミッツの誕生日だよ! |
さらに、アドレスバーのd/~~~~~~~~~~~/edit この間の部分がシートIDになります。これもメモしておきましょう。


「ツール」→「スクリプトエディタ」を選択。コードを入力していきます。以下コピペで、最初のアクセストークンとシートIDを入力しましょう。
// LINEアクセストークン。先ほどコピーしたアクセストークンを貼り付け。
var ACCESS_TOKEN = 'j0c4xVTMqgE38GU7zJ9deRiUUUG5SN+ivFGmjF5L/wnsG2hEtZJddnMb9OThw4pZGNgeCJpD8YF5WPv8vQX1Wb23cY6s1/48/zHZw0T0pAN050i5wMcoNK+EaBnHZPtGfW/SUAaT7JPaAQbudFG2+wdB04t89/1O/w1cDnyilFU=';
// Google スプレッドシートのシートIDを入力してください。
var SHEET_KEY = '1wkyneWH-0SR1FU1PQmL4uSgmVVFXq4yN45_lZAY3TR4';
function doPost(e) {
var event = JSON.parse(e.postData.contents).events[0];
var userMessage = event.message.text;
var message = "";
if ( userMessage === "ID" ) {
message = tellID(event);
}
else if ( userMessage === "id" ) {
message = tellID(event);
}
else if ( userMessage === "電車だ" ) {
//好きな受け答えをすることができます。
message = "猪突猛進!!(ドーン)";
}
else {
// 疎通確認が終わったらコメントアウトすると良いです。
message = "ゴメンネ ワカラナクッテ。「ID」ナラワカルヨ。";
}
replyMessage(event.replyToken, message);
return ContentService.createTextOutput(JSON.stringify({'content': 'post ok'})).setMimeType(ContentService.MimeType.JSON);
}
function replyMessage(token, message) {
UrlFetchApp.fetch('https://api.line.me/v2/bot/message/reply', {
'headers': {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + ACCESS_TOKEN,
},
'method': 'post',
'muteHttpExceptions': true,
'payload': JSON.stringify({
'replyToken': token,
'messages': [{
'type': 'text',
'text': message,
}],
}),
});
}
function pushMessage(to, message) {
UrlFetchApp.fetch('https://api.line.me/v2/bot/message/push', {
'headers': {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + ACCESS_TOKEN,
},
'method': 'post',
'muteHttpExceptions': true,
'payload': JSON.stringify({
'to': to,
'messages': [{
'type': 'text',
'text': message,
}],
}),
});
}
function tellID(event) {
// ID
var userID = event.source.userId;
var talkID = "";
if (event.source.type === "group") {
talkID = event.source.groupId;
} else if (event.source.type === "room") {
talkID = event.source.roomId;
}
var message = "おまえのIDは、 " + userID;
if (talkID != "") {
message += "\nおまえらのグループのIDは " + talkID;
}
return message;
}
function notice() {
var sheet = SpreadsheetApp.openById(SHEET_KEY).getSheetByName('alart');
var data = sheet.getDataRange().getValues();
var dayStr = ["日", "月", "火", "水", "木", "金", "土"];
var now = new Date();
for (var i=1; i<data.length; i++) {
var [year, month, dayOfMonth, weekNum, dayOfWeek, hour, minute, message, to] = data[i];
// 本文と発言する場所が空の場合はスキップ
if (message === "" || to === "") { continue; }
if ( (year == now.getFullYear() || year === "")
&& (month == now.getMonth() + 1 || month === "")
&& (dayOfMonth == now.getDate() || dayOfMonth === "")
&& (weekNum == parseInt(now.getDate() / 7) + 1 || weekNum === "")
&& (dayOfWeek === dayStr[now.getDay()] || dayOfWeek === "")
&& (hour == now.getHours() || hour === "")
&& (minute == now.getMinutes() || minute === "")
) {
pushMessage(to, message);
}
}
}

すべて終了したら、「公開」→「ウェブアプリケーションとして導入」を選択。
Projectvuversionは「NEW」一番下の場所は、「anyone,even anonymous」を選択。


こんな感じで出た時は、「詳細」をクリック。

「安全ではないページに移動」 をクリック。

で、許可を押します。
appURLをゲットする

こんな感じになれば成功。 appURLは後で必要になるのでメモしておきましょう。

さらに、「トリガーを追加」


このように変更して、保存を押しましょう。これでコードの設定は終了です。
LINE Developersに戻る
さきほどの「メッセージAPI」の場所から、「Webhook設定」のところへ。

「編集」をクリックします。先ほどGASの方でゲットしappURLをここに打ち込みます。更新。


さらに、アカウント機能、自動返信メッセージから、「編集」をクリック。このように設定しましょう。


同じページにQRコードがあるので、スマホで読み込み、友達登録をしましょう。

すべてうまくいくと、こんな感じで会話ができるようになります。
IDを聞くと、答えてくれます。
このIDをスプレッドシートのI列(発言する場所)に入力します。
また、スプレッドシート内の空白部分は、毎時その通知を行います。
例えば、下の入力だと「年」部分が空欄なので、毎年5月20日22:33に通知をするようになります。


うまくいきました。

これを使って、母親ボットを作るもよし、好きなキャラに通知されてもよし、好きに使えますね。
誕生日・命日・記念日など決まっていることのリマインダーとして役に立ちます。