Title1

Title2

Title3

5-3 完成作品

測試.gs


function my() {
  let service = get_rows_service();  
  let botMessageText = "school";
  // 尋找符合條件的物件
  let matchedObjects = service.filter(item => item.enable === '是');
  // 呼叫函式並輸出結果
  let result = checkKeyword(matchedObjects, botMessageText);
  console.log(result);
}

// 定義函式,將關鍵字轉換為陣列並比對是否包含部分匹配 botMessageText
function checkKeyword(objects, text) {
  let result = [];
  objects.forEach(object => {
    let keywords = object.keyword.split('|');
    let found = keywords.some(keyword => text.includes(keyword));
    if (found) {
      result.push(object);
    }
  });
  return result;
}

 

else

else {
      let service = get_rows_service();

      // 存放符合條件的物件
      let matchedObjects = [];

      // 遍歷資料
      service.forEach(item => {
        // 檢查 enable 屬性是否為 '是'
        if (item.enable === '是') {
          // 將 keyword 轉成陣列
          let keywords = item.keyword.split('|');
          // 檢查陣列中是否包含 botMessageText
          if (keywords.some(keyword => botMessageText.includes(keyword))) {
            matchedObjects.push(item);
          }
        }
      });

      //-------------------------
      if(matchedObjects.length === 0)return;
      
      //---------------------------------- carousel
      let message_contents = serviceCarouselTemplate(matchedObjects);

      let messages = [{
        "type": "flex",
        "altText": "客服回覆",//訊息副標題
        "contents": {
          "type": "carousel",
          "contents": message_contents
        }
      }];

      lineReplyMessage(replyToken, messages);
      return;

    }

 

樣版


/**========================================
  客服回覆樣版
=========================================*/
function serviceCarouselTemplate(services) {
  let rows = [];
  services.forEach(row => {
    let message = {};
    message.type = "bubble";
    message.size = "micro";
    if (row.pic) {
      message.hero = {
        "type": "image",
        "url": row.pic,
        "size": "full",
        "aspectMode": "cover",
        "aspectRatio": "320:213"
      }
    }
    message.body = {
      "type": "box",
      "layout": "vertical",
      "spacing": "sm",
      "paddingAll": "13px"
    }

    let contents = [];
    if (row.title) {
      contents.push({
        "type": "text",
        "text": row.title,
        "weight": "bold",
        "size": "sm",
        "wrap": true
      });
    }
    if (row.content) {
      contents.push({
        "type": "text",
        "text": row.content,
        "wrap": true,
        "color": "#8c8c8c",
        "size": "xs",
        "flex": 5
      });
    }
    if (row.text_label && row.text_content) {
      contents.push({
        "type": "button",
        "style": row.text_style,
        "color": "#905c44",
        "margin": "xxl",
        "action": {
          "type": "message",
          "label": row.text_label,
          "text": row.text_content
        }
      });
    }
    if (row.url_label && row.url_content) {
      contents.push({
        "type": "button",
        "style": row.url_style,
        "color": "#8fce00",
        "margin": "xxl",
        "action": {
          "type": "uri",
          "label": row.url_label,
          "uri": row.url_content
        }
      });
    }
    message.body.contents = contents;
    rows.push(message);
  });
  return rows;
}