LoginSignup
2
0

More than 1 year has passed since last update.

LINE×GAS|クイックリプライを使って予定を決める

Last updated at Posted at 2022-10-02

ゴール

僕は毎週、友達と電話をしています。
でも、その予定を毎週きめるのがめんどくさかったり、忘れたり、なんとなく聞きづらかったりするんですよね。ということで、代わりに聞いてくれるBotをLINE MessageAPIで作りました。

と言いたいところですが、実はLINEグループでの動作にエラーが発生していて、完成はしてません。でも、とりあえずここまでの成果を整理します。(GASとLINEの連携に詳しい方、ぜひお助けください。。。。)

▼LINE Notifyを使ったバージョンはこちら

未完成だけど、途中までの成果物

LINE

LINE Botが個人あてに「電話いつがいい?」という質問と共に、クイックリプライ(月〜日のボタン)が表示
IMG_6102.jpeg

ボタンをタップしたら、そのメッセージが送信される。
IMG_6103.jpeg

GAS

コード|グループIDを取得する

getGroupID.gs
function doPost(e){
 var json = JSON.parse(e.postData.contents);
 var UID = json.events[0].source.userId;
 var GID = json.events[0].source.groupId;
  
 var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 sheet.getRange(1,1).setValue(GID);

}

コード|クイックリプライボタンを設定したメッセージを送る

ask_day_of_week.gs
/*
クイックリプライボタンを設定したメッセージを送る
———————————–*/
function pushmessage_quick_reply() {
	/* スクリプトプロパティのオブジェクトを取得 */
	const prop = PropertiesService.getScriptProperties().getProperties();
 
	/* クイックリプライボタンを設定したメッセージを送る */
	UrlFetchApp.fetch('https://api.line.me/v2/bot/message/push', {
		'headers': {
			'Content-Type': 'application/json',
			'Authorization': 'Bearer' + ' '+ prop.TOKEN, // スクリプトプロパティにトークンは事前に追加しておく
		},
		'method': 'POST',
		'payload': JSON.stringify({
			"to": prop.DEBUGID, // スクリプトプロパティに送信先IDは事前に追加しておく →https://tonari-it.com/gas-line-bot-friend-token/#toc2
			"messages": [
				{
        "type": "text", // ①
        "text": "電話いつがいい?",
        "quickReply": { // ②
    "items": [
      {
        "type": "action", // ③
        "action": {
          "type": "message",
          "label": "",
          "text": ""
        }
      },
      {
        "type": "action",
        "action": {
          "type": "message",
          "label": "",
          "text": ""
        }
      },
      {
        "type": "action",
        "action": {
          "type": "message",
          "label": "",
          "text": ""
        }
      },
            {
        "type": "action",
        "action": {
          "type": "message",
          "label": "",
          "text": ""
        }
      },
            {
        "type": "action",
        "action": {
          "type": "message",
          "label": "",
          "text": ""
        }
      },
            {
        "type": "action",
        "action": {
          "type": "message",
          "label": "",
          "text": ""
        }
      },
            {
        "type": "action",
        "action": {
          "type": "message",
          "label": "",
          "text": ""
        }
      }
    ]
  }
}	],
			"notificationDisabled": false // trueだとユーザーに通知されない
		}),
	});
}

スクリプト プロパティ

クイックリプライくん_-プロジェクトの設定-_Apps_Script.png

未完成となった理由

グループIDを取得して、プロパティDEBUGIDに値を登録しても、下記エラーが発生してメッセージが送られない。
(個人LINE宛にはメッセージを送信できる。)

Exception: Request failed for https://api.line.me returned code 400. Truncated server response: {"message":"Failed to send messages"} (use muteHttpExceptions option to examine full response)
pushmessage_quick_reply	@ テスト.gs:9

LINEグループにLINEbotを招待する際の注意点

  • コンソールの[Channel基本設定]ページで、[Botのグループトーク参加]オプションが有効であることを確認。
  • また、グループチャットに同時に複数のボットは参加できないことに注意。既にボットが参加しているグループまたはトークルームにボットを招待することはできません。

▶︎参考

参考サイト

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