LoginSignup
5
1

More than 1 year has passed since last update.

gasでLinebot 作ってみた

Last updated at Posted at 2021-08-26

2021 8月現在

gasは代入分割対応してるみたいだぞん

function doPost (e) {
  // LINE developersのメッセージ送受信設定に記載のアクセストークン
  const ACCESS_TOKEN =
    'hoge'

  // Webhookで受信したデータをjsで読めるように変換して格納
  const data = JSON.parse(e.postData.contents)

  // Webhookで受信した応答用Tokenとメッセージ取得
  const { replyToken, message } = data.events[0]

  // 応答メッセージ用のオブジェクト作成
  const messages = [
    {
      type: 'text',
      text: `${message.text}する勇気`,
    },
  ]

  // 応答メッセージ用のAPI URL
  const url = 'https://api.line.me/v2/bot/message/reply'

  // リクエストオプションのオブジェクトを作成
  const options = {
    headers: {
      'Content-Type': 'application/json; charset=UTF-8',
      Authorization: `Bearer ${ACCESS_TOKEN}`,
    },
    method: 'post',
    payload: JSON.stringify({ replyToken, messages }),
  }

  UrlFetchApp.fetch(url, options)

  return ContentService.createTextOutput(
    JSON.stringify({ content: 'post ok' }),
  ).setMimeType(ContentService.MimeType.JSON)
}

参考

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