1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GAS - Googleフォームからアップロードした画像を自動でLINE NOTIFYに投稿

Last updated at Posted at 2022-04-01

Google フォームからアップロードした画像をLINE NOTIFYに投稿する手順のメモ

LINE NOTIFY API公式ドキュメント

・Googleドライブにアップロードされたファイルのパーミッションを公開に設定しないと、LINEのAPIが画像を読み込めない。
パーミッションの設定を変えるには setSharing 関数を使う。

file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);

注意:LINE NOTIFYにはJPEG画像かPNG画像しかアップロードできない。フォームでは画像や文書など種類は制限できるが、画像フォーマットまでは制限できないため、画像フォーマットを判別して変換する等の対応が必要になるか。(下記では未対応)

準備

  • フォームのコンテナバインドスクリプトとして記述(スプレッドシートではない)
  • トリガーでフォーム送信時に下記 postImage 関数が実行されるように設定しておく。
  • フォームに画像アップロードの設問を1つ作り、タイトルを「画像アップロード」にしておく。

参考コード

const LINE_NOTIFY_TOKEN = '*******';

function postImage(e) {
  let blob = null;
  const itemResponses = e.response.getItemResponses();
  itemResponses.forEach(item => {
    const title = item.getItem().getTitle();
    const response = item.getResponse();

    if (title === "画像アップロード") {
      const fileImage = DriveApp.getFileById(response);
      fileImage.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
      blob = fileImage.getBlob();
    } else {
      // その他の設問の場合の処理
    }
  });

  const body = "メッセージ";
  sendLine(body, blob);
}

function sendLine(body, blob) {
  const options = {
    "method": "post",
    "payload": {
      "message": body,
      "imageFile" : blob,
    },
    "headers": { "Authorization": "Bearer " + LINE_NOTIFY_TOKEN }
  };

  UrlFetchApp.fetch("https://notify-api.line.me/api/notify", options);
}
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?