Title1

Title2

Title3

3-2 推播

官方文件:https://developers.line.biz/en/docs/messaging-api/sending-messages/#send-messages-at-any-time
to:userId、groupId

curl -v -X POST https://api.line.me/v2/bot/message/push \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {channel access token}' \
-d '{
    "to": "U4af4980629...",
    "messages":[
        {
            "type":"text",
            "text":"Hello, world1"
        },
        {
            "type":"text",
            "text":"Hello, world2"
        }
    ]
}'

 

GAS
pushMessage()


/*============================================
  Line 推播內容
============================================*/
function pushMessage() {
  //---------------------------------- 文字訊息(最多5組)
  // let messages = [
  //   {
  //     'type': 'text',
  //     'text': '文字訊息'
  //   }
  // ];

  //---------------------------------- 圖片訊息(最多5組)  
  // let messages = [
  //   {
  //     "type": "image",
  //     "originalContentUrl": "https://i.imgur.com/Fn9gu0q.png",
  //     "previewImageUrl": "https://i.imgur.com/Fn9gu0q.png"
  //   }
  // ];

  //---------------------------------- 文字+圖片訊息(最多5組)
  let messages = [
    {
      'type': 'text',
      'text': '文字訊息'
    },
    {
      "type": "image",
      "originalContentUrl": "https://i.imgur.com/hwvXBjq.png",
      "previewImageUrl": "https://i.imgur.com/hwvXBjq.png"
    }
  ];

  //---------------------------------- flex(訂單完成)
  // let messages = [{
  //   "type": "flex",
  //   "altText": "訂單完成",//訊息副標題
  //   "contents": flexTemplate()
  // }];

  //---------------------------------- carousel(商品展示)
  // let messages = [{
  //   "type": "flex",
  //   "altText": "商品展示",//訊息副標題
  //   "contents": {
  //     "type": "carousel",
  //     "contents": carouselTemplate()
  //   }
  // }];

  let to = '';//請自行替換 userId、groupId
  linePushMessage(to, messages);
}

 

linePushMessage(to, pushMessage)


/*============================================
  Line Bot 推播
============================================*/
function linePushMessage(to, pushMessage) {
  //-----------------------------------推播訊息給line使用者  
  let url = 'https://api.line.me/v2/bot/message/push';
  try {
    UrlFetchApp.fetch(url, {
      'headers': {
        'Content-Type': 'application/json; charset=UTF-8',
        'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN,
      },
      'method': 'post',
      'payload': JSON.stringify({
        'to': to,
        'messages': pushMessage
      })
    });
  } catch (e) {
    //---------------------------------- 記錄Line Bot 推播 錯誤訊息
    let formData = {
      sn: '',
      timestamp: '',
      type: 'Line Bot 推播 錯誤訊息',
      code: e
    }
    insert_sheet_record(formData);
    //---------------------------------- 記錄Line Bot 推播 錯誤訊息 end
  }
  //---------------------------------------推播訊息給line使用者 end
}