Title1

Title2

Title3

3-1 回覆

回覆

官方文件https://developers.line.biz/en/docs/messaging-api/sending-messages/#reply-messages
messages:最多5則訊息

curl -v -X POST https://api.line.me/v2/bot/message/reply \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {channel access token}' \
-d '{
    "replyToken":"nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
    "messages":[
        {
            "type":"text",
            "text":"Hello, user"
        },
        {
            "type":"text",
            "text":"May I help you?"
        }
    ]
}'

 

GAS
lineMessage(botData)

/**========================================
  用戶傳送訊息給聊天機器人 
=========================================*/
function lineMessage(botData) {
  const botMessageType = botData.events[0].message.type;

  // 判斷訊息是否為文字
  if (botMessageType === 'text') {
    // 用戶聊天訊息內容
    const botMessageText = botData.events[0].message.text;
    // 用戶訊息的來源
    const source = botData.events[0].source;
    // 用戶 token
    const replyToken = botData.events[0].replyToken;

    // --------------------------------- 文字訊息(最多5組)
    let messages = [{
      'type': 'text',
      'text': botMessageText
    }];
    
    //---------------------------------- 圖片訊息(最多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': botMessageText
    //   },
    //   {
    //     "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()
    //   }
    // }];

    // 調用函數
    lineReplyMessage(replyToken, messages);
  }
}

lineReplyMessage(replyToken, replyMessage)


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

 

/*============================================
  flexTemplate
  https://developers.line.biz/flex-simulator/  
============================================*/
function flexTemplate() {
  // let order = {
  //   sn: '',
  //   date: '',
  //   userId: 'userId',
  //   group: 'group',
  //   name: 'xxx',
  //   prod_sn: 'prod_sn',
  //   title: 'Brown Cafe',
  //   unit: '個',
  //   price: 200,
  //   amount: 3,
  //   total: 600,
  //   pic: 'https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_1_cafe.png'
  // }

  let content = 'xxx 您好,您的訂購資料:' + '\n';
  content += '商品單位: ' + '個' + '\n';
  content += '商品單價: ' + '200' + ' 元\n';
  content += '訂購數量: ' + '3' + '\n';
  content += '合計金額: ' + '600' + ' 元\n';
  let messages = {
    "type": "bubble",
    "hero": {
      "type": "image",
      "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_1_cafe.png",//圖片
      "size": "full",
      "aspectRatio": "20:13",
      "aspectMode": "cover"
    },
    "body": {
      "type": "box",
      "layout": "vertical",
      "contents": [
        {
          "type": "text",
          "text": "Brown Cafe",//標題
          "weight": "bold",
          "size": "xl"
        },
        {
          "type": "text",
          "text": content,//內容
          "wrap": true,
          "color": "#666666",
          "size": "sm",
        }
      ]
    }
  }
  return messages;
}

 

carouselTemplate()

/**======================================
  carousel 樣版
=======================================*/
function carouselTemplate(){
  let messages = [];
  let title = '商品1'
  let content = '商品單位: ' + '個' + ' \n';
  content += '商品單價: ' + '100' + '元\n';
  content += '商品內容商品內容商品內容商品內容\n';
  content += '商品內容商品內容商品內容商品內容\n';
  content += '商品內容商品內容商品內容商品內容\n';
  content += '商品內容商品內容商品內容商品內容\n';
  content += '----------------------\n';
  messages.push({
    "type": "bubble",
    "size": "micro",
    "hero": {
      "type": "image",
      "url": "https://scdn.line-apps.com/n/channel_devcenter/img/flexsnapshot/clip/clip10.jpg",//圖片
      "size": "full",
      "aspectMode": "cover",
      "aspectRatio": "320:213"
    },
    "body": {
      "type": "box",
      "layout": "vertical",
      "spacing": "sm",
      "paddingAll": "13px",
      "contents": [
        {
          "type": "text",
          "text": title,//標題
          "weight": "bold",
          "size": "sm",
          "wrap": true
        },
        {
          "type": "text",
          "text": content,//內容
          "color": "#8C8C8C",
          "size": "sm",
          "wrap": true
        }, 
        {
          "type": "box",
          "layout": "vertical",
          "contents": [
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "message",
                "label": '+1',                     //訊息標題
                "text":  '+1' + ' ' + title //  訊息內容
              }
            },
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "message",
                "label": '+2',                     //訊息標題
                "text":  '+2' + ' ' + title //  訊息內容
              }
            },
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "message",
                "label": '+5',                     //訊息標題
                "text":  '+5' + ' ' + title //  訊息內容
              }
            }
          ]
        }
      ]
    }
  });
  messages.push({
    "type": "bubble",
    "size": "micro",
    "hero": {
      "type": "image",
      "url": "https://scdn.line-apps.com/n/channel_devcenter/img/flexsnapshot/clip/clip10.jpg",//圖片
      "size": "full",
      "aspectMode": "cover",
      "aspectRatio": "320:213"
    },
    "body": {
      "type": "box",
      "layout": "vertical",
      "spacing": "sm",
      "paddingAll": "13px",
      "contents": [
        {
          "type": "text",
          "text": title,//標題
          "weight": "bold",
          "size": "sm",
          "wrap": true
        },
        {
          "type": "text",
          "text": content,//內容
          "color": "#8C8C8C",
          "size": "sm",
          "wrap": true
        }, 
        {
          "type": "box",
          "layout": "vertical",
          "contents": [
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "message",
                "label": '+1',                     //訊息標題
                "text":  '+1' + ' ' + title //  訊息內容
              }
            },
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "message",
                "label": '+2',                     //訊息標題
                "text":  '+2' + ' ' + title //  訊息內容
              }
            },
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "message",
                "label": '+5',                     //訊息標題
                "text":  '+5' + ' ' + title //  訊息內容
              }
            }
          ]
        }
      ]
    }
  });
  messages.push({
    "type": "bubble",
    "size": "micro",
    "hero": {
      "type": "image",
      "url": "https://scdn.line-apps.com/n/channel_devcenter/img/flexsnapshot/clip/clip10.jpg",//圖片
      "size": "full",
      "aspectMode": "cover",
      "aspectRatio": "320:213"
    },
    "body": {
      "type": "box",
      "layout": "vertical",
      "spacing": "sm",
      "paddingAll": "13px",
      "contents": [
        {
          "type": "text",
          "text": title,//標題
          "weight": "bold",
          "size": "sm",
          "wrap": true
        },
        {
          "type": "text",
          "text": content,//內容
          "color": "#8C8C8C",
          "size": "sm",
          "wrap": true
        }, 
        {
          "type": "box",
          "layout": "vertical",
          "contents": [
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "message",
                "label": '+1',                     //訊息標題
                "text":  '+1' + ' ' + title //  訊息內容
              }
            },
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "message",
                "label": '+2',                     //訊息標題
                "text":  '+2' + ' ' + title //  訊息內容
              }
            },
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "message",
                "label": '+5',                     //訊息標題
                "text":  '+5' + ' ' + title //  訊息內容
              }
            }
          ]
        }
      ]
    }
  });

  return messages;
}