1
1

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.

LINE Botを開発している過程で、Google Formsからの送信があった際にLINEに通知が送信される機能が必要になり、GASでコードを作成しました。
タイトルなし.png

Google Formsを作成したらスクリプトを開き、下のコードを貼りつけてください。

main.gs
function notifyLineBot(event) {
  var itemResponse = event.response.getItemResponses();
  var name = itemResponse[0].getResponse();
  var roomNumber = itemResponse[1].getResponse();
  var position = itemResponse[2].getResponse();

  // LINE BotのアクセストークンとユーザーIDを設定
  var accessToken = 'ここに入力'; // アクセストークン
  var userId = 'ここに入力'; // ユーザーID

  // LINEに送信するメッセージを作成
  var message = `${name}様からフォームの提出がありました。`;

  // LINEに通知を送信
  var url = 'https://api.line.me/v2/bot/message/push';
  var headers = {
    'Content-Type': 'application/json; charset=UTF-8',
    'Authorization': 'Bearer ' + accessToken
  };
  var pushData = {
    'to': userId,
    'messages': [
      {
        'type': 'text',
        'text': message
      }
    ]
  };
  var options = {
    'method': 'post',
    'headers': headers,
    'payload': JSON.stringify(pushData)
  };
  UrlFetchApp.fetch(url, options);

}

LINEのアクセストークンとユーザーIDの入力が必要になります。
タイトルなし.png

Google Formsの形式は自由ですが、このコードはフォームの冒頭に氏名を入力する形が前提になっています。
タイトルなし.png

トリガーを下図のように設定します。
タイトルなし.png

初回起動前に、下図のようにデプロイを行い、アカウントの認証を行ってください。
タイトルなし.png

ただし、デプロイはしてもLINE DevelopersのWebhookにリンクさせる必要はありません。

関数はトリガーで実行される為、LINE Developersへの連携は不要です。

LINEとの連携例としては、下図のようにまずGoogle Formsへのリンクに誘導し、フォームの送信が行われると、LINE側に通知が来るようになります。
タイトルなし.png

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?