はじめに
GAS?なにそれ?なんかのガス?の状態からスタート...
google apps scriptというプログラミング言語でgoogleドライブから使用できるみたいです。
googleドライブ>新規>その他>その他>google apps script を選択していくと、使えます。
メリットは、詳しく調べていませんが、
使っていて感じたのは、「テストが楽」「デプロイが楽」、
そして今回、時間をトリガーにしたいという目的で採用しました。
このあたりにメリットが書いてあります。
全体像
時間になったら「おはよう」と
その日の調子を聞いてくれるLine Botです。
var access_token = "アクセストークン"
var to = "UserId"
//送信するメッセージ定義する関数を作成します。
function createMessage() {
//メッセージを定義する
message_push='';
return push(message_push);
}
//実際にメッセージを送信する関数を作成します。
function push(text) {
var url_push = "https://api.line.me/v2/bot/message/push";
var headers = {
"Content-Type" : "application/json; charset=UTF-8",
'Authorization': 'Bearer ' + access_token,
};
//toのところにメッセージを送信したいユーザーのIDを指定します。(toは最初の方で自分のIDを指定したので、linebotから自分に送信されることになります。)
var postData = { //postDataオブジェクトのmessageキーの値にテンプレートを設定
"to" : to,
"messages" : [
{
"type": "template",
"altText": "this is a buttons template",
"template": {
"type": "buttons",
"thumbnailImageUrl": "https://loosedrawing.com/wp/wp-content/uploads/2021/01/0828.png",
"imageAspectRatio": "rectangle",
"imageBackgroundColor": "#FFFFFF",
"title": "おはようございます!",
"text": "調子はいかがですか?",
"actions": [
{
"type": "message",
"label": "良い",
"text": "良い"
},
{
"type": "message",
"label": "普通",
"text": "普通"
},
{
"type": "message",
"label": "悪い",
"text": "悪い"
}
]
}
}
]
};
var options = {
"method" : "post",
"headers" : headers,
"payload" : JSON.stringify(postData)
};
return UrlFetchApp.fetch(url_push, options)
}
//replyする役割
function doPost(e) {
//トークンがきたか判定
var replyToken= JSON.parse(e.postData.contents).events[0].replyToken;
if (typeof replyToken === 'undefined') {
return;
}
var url = 'https://api.line.me/v2/bot/message/reply';
//受信オブジェクトを取得
var input = JSON.parse(e.postData.contents).events[0].message;
var receive = '';
var reply_message = '個別メッセージには対応しておりません。'; //挨拶じゃない場合
var good = [{
"type": "template",
"altText": "this is a confirm template",
"template": {
"type": "confirm",
"actions": [
{
"type": "message",
"label": "はい",
"text": "はい"
},
{
"type": "message",
"label": "いいえ",
"text": "いいえ"
}
],
"text": "今日も良い1日にしましょう。ところで血圧は測りましたか?"
}
}];
var yes =[{
"type": "template",
"altText": "this is a confirm template",
"template": {
"type": "confirm",
"actions": [
{
"type": "message",
"label": "変わりない",
"text": "変わりない"
},
{
"type": "message",
"label": "変わった",
"text": "変わった"
}
],
"text": "体重はお変わりないです? 急激な変化は要注意です。"
}
}];
var noChange = [{
"type": "template",
"altText": "this is a buttons template",
"template": {
"type": "buttons",
"thumbnailImageUrl": "https://loosedrawing.com/wp/wp-content/uploads/2020/06/y0366.png",
"title": "朝ごはん",
"text": "もう食べましたか?",
"actions": [
{
"type": "message",
"label": "食べた",
"text": "食べた"
},
{
"type": "message",
"label": "まだ、これから。",
"text": "まだ、これから。"
}
]
}
}];
//メッセージ取得
if(input.type == 'text') {
receive = input.text;
}
if(input.text.match('良い') || ((input.text.match('普通'))||(input.text.match('悪い')))) reply_message = good;
if(input.text.match('はい')) reply_message = yes;
if(input.text.match('変わりない')) reply_message = noChange;
UrlFetchApp.fetch(url, {
'headers': {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + access_token,
},
'method': 'post',
'payload': JSON.stringify({
'replyToken': replyToken,
'messages': reply_message,
}),
});
return ContentService.createTextOutput(JSON.stringify({'content': 'post ok'})).setMimeType(ContentService.MimeType.JSON);
}
createMessage
関数を実行すると、テンプレートで送られます。
テンプレートの作成には、Line Bot Designerを使用しました。
普通のメッセージではなく、テンプレート送信の実装が結構大変でした。
reply_message変数
に[]
で囲みオブジェクトとして格納するとうまく出力されました。
時間をトリガーにする方法は、下記の参考資料を参照ください。
参考資料
■push通知を参考
■replyを参考
■GASのデプロイを参考
■GASの承認を参考