LINE BOT 傳回表單問題

Xin Ru Wang
各位大大好 ,這裡是程式菜鳥
我在開發LINE BOT的API時
想要有多個存取GOOGLE FORM的功能
當我把存取CODE放到全域的時候功能正常 但只能單一功能
我這邊想改成多個,所以將CODE放進doPost裡做點選按鈕的判斷
實作時卻卡在回覆表單的部分,請問如何解決呢?

var CHANNEL_ACCESS_TOKEN = "####";
var formId = "";    //表單 ID
var spreadSheetId = "";    //試算表 ID
var sheetName = "";    //工作表名稱
var destinationFolderID = ""; //存放檔案的資料夾ID
var form ;
var spreadSheet;
var sheet  ;
var lastRow ;
var lastColumn;
var sheetData;
var confirmationMessageDefault;
var confirmationMessage;
var formItems;


function doPost(e) {

var userData = JSON.parse(e.postData.contents);
console.log(userData);

// 取出 replayToken 和發送的訊息文字
var replyMessage = [];
var replyToken = userData.events[0].replyToken;

var clientID = userData.events[0].source.userId;
var clientMessage;
var nowTime = new Date();
var prefixFileName = nowTime.getFullYear() + append1Zero(nowTime.getMonth() + 1) + append1Zero(nowTime.getDate()) + append1Zero(nowTime.getHours()) + append1Zero(nowTime.getMinutes()) + append1Zero(nowTime.getSeconds()) + append2Zero(nowTime.getMilliseconds()) + "-";
var fileLocation = "";

switch(userData.events[0].type) {
  case "message":
    switch(userData.events[0].message.type) {
      case "text":          
        clientMessage = userData.events[0].message.text;
        var url = 'https://api.line.me/v2/bot/message/reply';
         switch(clientMessage){
          case "FUNCTION":      
            reply = "FUNCTION";                  
            UrlFetchApp.fetch(url, {
              'headers': {
                'Content-Type': 'application/json; charset=UTF-8',
                'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN,
              },
              'method': 'post',
              'payload': JSON.stringify({
                'replyToken': replyToken,
                'messages': [
                  {
                    "type": "template",
                    "altText": "this is a confirm template",
                    "template": {
                      "type": "confirm",
                      "actions": [
                        {
                          "type": "message",
                          "label": "查詢",
                          "text": "查詢:"+reply
                        },
                        {
                          "type": "message",
                          "label": "更新",
                          "text": "更新:"+reply
                        }
                      ],
                      "text": "FUNCTION:要執行甚麼動作呢?"
                    }
                  }
                ],
              }),
            });      
            break;    
            
          case "更新:FUNCTION":       
            formId = "####";    //表單 ID
            spreadSheetId = "####";    //試算表 ID
            sheetName = "回應";    //工作表名稱
            destinationFolderID = ""; //存放檔案的資料夾ID
            form = FormApp.openById(formId);
            spreadSheet = SpreadsheetApp.openById(spreadSheetId);
            sheet = spreadSheet.getSheetByName(sheetName);
            lastRow = sheet.getLastRow();
            lastColumn = sheet.getLastColumn();
            sheetData = sheet.getSheetValues(1, 1, lastRow, lastColumn);
            confirmationMessageDefault = "登記完畢!!";
            confirmationMessage = form.getConfirmationMessage();
            if (confirmationMessage == "") {confirmationMessage = confirmationMessageDefault;}
            formItems = form.getItems();
            break;    
  
        }                   
        break;             
      }
      break;
 
  case "postback":
    switch(userData.events[0].postback.data){
      case "DateMessage":
        clientMessage = userData.events[0].postback.params.date;
        replyMessage.push({type:"text", text:clientMessage});
        break;

      case "TimeMessage":
        clientMessage = userData.events[0].postback.params.time;
        replyMessage.push({type:"text", text:clientMessage});
        break;

      case "ignoreQuestion":
        clientMessage = "NULL";
        replyMessage.push({type:"text", text:"此題已略過"});
        break;

      case "otherOption":
        replyMessage = replyMessage.concat(otherOptionMessage());
        sendReplyMessage(CHANNEL_ACCESS_TOKEN, replyToken, replyMessage);
        return;
        break;
    }
    break;

  case "follow":
    clientMessage = "follow";
    break;

  default:
    return;
}
var replyData = getUserAnswer(clientID, clientMessage);
switch (replyData[1]) {
  case -1:
    sheet.getRange(replyData[0], 1).setValue(Date());
    replyMessage = replyMessage.concat(finishTheQuestionnare(replyData[2]));
    sendReplyMessage(CHANNEL_ACCESS_TOKEN, replyToken, replyMessage);
    return;
    break;
  case 1:
    replyMessage = replyMessage.concat(getFormTitle());
    break;
}
replyMessage = replyMessage.concat(getQuestion(replyData[1]));
sendReplyMessage(CHANNEL_ACCESS_TOKEN, replyToken, replyMessage);
}


程式參考:https://www.youtube.com/watch?v=AdLnMhDPY_c&list=PLLrJ9DEA0QKOxdbYiEZ4L-g81M0LbiPgR&index=4
回到頂部