LINEを使った注文システムソースコード

##ソースコード
```
// LINE developersのメッセージ送受信設定に記載のアクセストークン
  var ACCESS_TOKEN = '/*LINEのACCESS TOKEN*/';

function doPost(e) {
  // WebHookで受信した応答用Token
  var replyToken = JSON.parse(e.postData.contents).events[0].replyToken;
  var id = JSON.parse(e.postData.contents).events[0].source.userId;
  // ユーザーのメッセージを取得
  var userMessage = JSON.parse(e.postData.contents).events[0].message.text;
  var replyMessage = "https://peraichi.com/landing_pages/view/orderline";
  var comma;
  var menu;
  var menu_num;
  var amount;
  var table_ID
  var A = "A".charCodeAt(0);
  var T = userMessage.slice(0,1);
  var n;
  var n2;
  var n3;
  var m =2;
  var s = "";
  var s2 = "";
  var s3 = "";

  //spread sheet書き込み
  function write(ID,content)
  {
    var sheet_id = "/*Spread SheetのID*/";
    var sheet_name = "注文集計";
    
    // シートの取得
    var spreadsheet = SpreadsheetApp.openById(sheet_id);
    var sheet = spreadsheet.getSheetByName(sheet_name);
 
    // 書き込み
    sheet.getRange(ID).setValue(content);
  }
  
  //厨房シート書き込み
  function write_kitchen(menu,amount,table)
  {
    var sheet_id = "/*Spread SheetのID*/";
    var sheet_name = "厨房";
    var ID =1;
    // シートの取得
    var spreadsheet = SpreadsheetApp.openById(sheet_id);
    var sheet = spreadsheet.getSheetByName(sheet_name);
    var cell = "A"+ ID;
    // 読み込み
    var x =sheet.getRange(cell).getValue();
    while(x != null && x!= "")
    {
      ID += 1;
      cell ="A"+ ID;
      x =sheet.getRange(cell).getValue();
    }
    // 書き込みmenu
    sheet.getRange(cell).setValue(menu);
    //書き込みamount
    cell = "B"+ ID;
    sheet.getRange(cell).setValue(amount);
    //書き込みtable
    cell = "C"+ ID;
    sheet.getRange(cell).setValue(table);
  }
  
  //spread sheet読み込み
  function read(ID)
  {
    var sheet_id = "/*Spread SheetのID*/";
    var sheet_name = "注文集計";
    
    // シートの取得
    var spreadsheet = SpreadsheetApp.openById(sheet_id);
    var sheet = spreadsheet.getSheetByName(sheet_name);
 
    // 読み込み
    return sheet.getRange(ID).getValue();
  }
   

  //テーブル検索(LINE IDから)
  function find_table(ID)
  {
    //シートの選択
    var sheet_id = "/*Spread SheetのID*/"
    var sheet_name = "注文集計"
    
    // シートの取得
    var spreadsheet = SpreadsheetApp.openById(sheet_id);
    var sheet = spreadsheet.getSheetByName(sheet_name);
    var data = sheet.getRange(2,2,11,97).getValues();//検索範囲（セルB2:CS11）
    //検索
    var i;
    var j;
    for (j = 0; j <= 96; j++){
      for(i = 0; i <= 9; i++){
        if(data[i][j] == ID){
          j = Math.floor((j+3)/3);
          return j;
        }
      }
    }
    return -1;
  }

  
  //テーブル設定
  if(T == "T" || T =="t")
  {
    if(find_table(id) == -1)
    {
      n = userMessage.replace(/[^-^0-9\.]/g,"");
      n = n*3-1;
      while (n >= 1) {
        n--;
        s = String.fromCharCode(A + (n % 26)) + s;
        n = Math.floor(n / 26);
      }
      
      table_ID = s+m;
      while (read(table_ID) != null && read(table_ID)!= ""){
        m += 1;
        table_ID = s+m;
      }
      if (m <= 11){
        write(table_ID,id);
        n = userMessage.replace(/[^-^0-9\.]/g,"");
        replyMessage = n +"番テーブルを選択しました。";
      } 
    }
    else
    {
      replyMessage = find_table(id)+"番テーブルをすでに選択しています。メニュー番号と注文数をコンマ（,）で区切って入力してください\n\n（例）1番のオムライスを２人前注文する場合：\n「１，２」と入力して下さい。";
    }
  }

 //メッセージ変換
  if(userMessage.indexOf(",") != -1)
  {
    comma = userMessage.indexOf(",");
    menu_num = userMessage.slice(0,comma);
    amount = userMessage.slice(comma+1);
    amount = amount.replace(/[^-^0-9\.]/g,"");
    menu_num = Math.floor(menu_num);
    if(find_table(id) == -1)
    {
      replyMessage = "Tのうしろに座席番号を入力して座席を選択してください\n\n（例）5番テーブルを選択する場合：\n「T5」と入力してください";
    }
    else
    {
      n = find_table(id);
      n = n*3-1;
      n2 = n+1;
      n3 = n+2;
      m = 12;
      while (n >= 1) {
        n--;
        s = String.fromCharCode(A + (n % 26)) + s;
        n = Math.floor(n / 26);
      }
      while (n2 >= 1) {
        n2--;
        s2 = String.fromCharCode(A + (n2 % 26)) + s2;
        n2 = Math.floor(n2 / 26);
      }
      while (n3 >= 1) {
        n3--;
        s3 = String.fromCharCode(A + (n3 % 26)) + s3;
        n3 = Math.floor(n3 / 26);
      }
      table_ID = s+m;
      while (read(table_ID) != null && read(table_ID)!= ""){
        m += 1;
        table_ID = s+m;
      }
      n = find_table(id);
      switch(menu_num)
      {
        case 1:
          menu = "オムライス";
          replyMessage = menu + "を" + amount + "つ承りました";
          write(table_ID,menu);
          table_ID = s2+m;
          write(table_ID,"980");
          table_ID = s3+m;
          write(table_ID,amount);
          write_kitchen(menu,amount,n);
          break;
        
        case 2:
          menu = "カレーライス";
          replyMessage = menu + "を" + amount + "つ承りました";
          write(table_ID,menu);
          table_ID = s2+m;
          write(table_ID,"980");
          table_ID = s3+m;
          write(table_ID,amount);
          write_kitchen(menu,amount,n);
          break;
        
        case 3:
          menu = "サラダ";
          replyMessage = menu + "を" + amount + "つ承りました";
          write(table_ID,menu);
          table_ID = s2+m;
          write(table_ID,"320");
          table_ID = s3+m;
          write(table_ID,amount);
          write_kitchen(menu,amount,n);
          break;
          
        case 4:
          menu = "ピクルス";
          replyMessage = menu + "を" + amount + "つ承りました";
          write(table_ID,menu);
          table_ID = s2+m;
          write(table_ID,"540");
          table_ID = s3+m;
          write(table_ID,amount);
          write_kitchen(menu,amount,n);
          break;
          
        case 5:
          menu = "スープ";
          replyMessage = menu + "を" + amount + "つ承りました";
          write(table_ID,menu);
          table_ID = s2+m;
          write(table_ID,"320");
          table_ID = s3+m;
          write(table_ID,amount);
          write_kitchen(menu,amount,n);
          break;
          
        case 6:
          menu = "紅茶";
          replyMessage = menu + "を" + amount + "つ承りました";
          write(table_ID,menu);
          table_ID = s2+m;
          write(table_ID,"280");
          table_ID = s3+m;
          write(table_ID,amount);
          write_kitchen(menu,amount,n);
          break;
          
        default:
          menu = "メニューは6番までです。";
          replyMessage =menu + "1~6"+ "までの数字の後に「,個数」を入力して下さい" ;
          break;
      }
    }
  }
 
  // 応答メッセージ用のAPI URL
  var url = 'https://api.line.me/v2/bot/message/reply';
  
  if(userMessage == "menu"){
    UrlFetchApp.fetch(url, {
      'headers': {
        'Content-Type': 'application/json; charset=UTF-8',
        'Authorization': 'Bearer ' + ACCESS_TOKEN,
      },
      'method': 'post',
      'payload': JSON.stringify({
        'replyToken': replyToken,
        'messages': [
          {
            "type": "template",
            "altText": "this is a buttons template",
            "template": {
              "type": "carousel",
              "actions": [],
              "columns": [
                {
                  "thumbnailImageUrl": "https://3.bp.blogspot.com/-fRi465qB0PU/WWNAjw1nHBI/AAAAAAABFUc/qRqCiP3QlM87_Qvi8souFa3x81ZaHJOJACLcBGAs/s400/food_dish_blank.png",
                  "title": "Food",
                  "text": "を選択",
                  "actions": [
                    {
                      "type": "message",
                      "label": "Food",
                      "text": "Food"
                    }
                  ]
                },

                {
                  "thumbnailImageUrl": "https://www.illust-box.jp/db_img/sozai/00004/44339/watermark.jpg",
                  "title": "Drink",
                  "text": "を選択",
                  "actions": [
                    {
                      "type": "message",
                      "label": "Drink",
                      "text": "Drink"
                    }
                  ]
                }
              ]
            }
          }
        ]
      }),
    }); 
  }
  else{
  
    if(userMessage == "Food"){
          UrlFetchApp.fetch(url, {
      'headers': {
        'Content-Type': 'application/json; charset=UTF-8',
        'Authorization': 'Bearer ' + ACCESS_TOKEN,
      },
      'method': 'post',
      'payload': JSON.stringify({
        'replyToken': replyToken,
        'messages': [
          {
            "type": "template",
            "altText": "this is a buttons template",
            "template": {
              "type": "carousel",
              "actions": [],
              "columns": [
                {
                  "thumbnailImageUrl": "https://www.illust-box.jp/db_img/sozai/00004/47321/watermark.jpg",
                  "title": "オムライス",
                  "text": "を選択",
                  "actions": [
                    {
                      "type": "message",
                      "label": "注文",
                      "text": "1,1"
                    }
                  ]
                },
                
                //カレー、サラダを選択
                {
                  "thumbnailImageUrl": "https://illustkun.com/wp-content/uploads/illustkun-03313-curry.png",
                  "title": "カレーライス",
                  "text": "を選択",
                  "actions": [
                    {
                      "type": "message",
                      "label": "注文",
                      "text": "2,1"
                    }
                  ]
                },
                
                {
                  "thumbnailImageUrl": "https://i.pinimg.com/originals/03/1b/5b/031b5b955b4df4fb68e8ed0a7a929a64.jpg",
                  "title": "サラダ",
                  "text": "を選択",
                  "actions": [
                    {
                      "type": "message",
                      "label": "注文",
                      "text": "3,1"
                    }
                  ]
                },
                {
                  "thumbnailImageUrl": "https://www.otafuku.co.jp/image.jsp?id=1607",
                  "title": "ピクルス",
                  "text": "を選択",
                  "actions": [
                    {
                      "type": "message",
                      "label": "注文",
                      "text": "4,1"
                    }
                  ]
                },
                //スープを選択
                {
                  "thumbnailImageUrl": "https://www.lettuceclub.net/img/dish/1/S20121210161005A_000.png",
                  "title": "スープ",
                  "text": "を選択",
                  "actions": [
                    {
                      "type": "message",
                      "label": "注文",
                      "text": "5,1"
                    }
                  ]
                }
              ]
            }
          }
        ]
      }),
    });
    }
      
    else{
      if(find_table(id) == -1){
        UrlFetchApp.fetch(url, {
          'headers': {
            'Content-Type': 'application/json; charset=UTF-8',
            'Authorization': 'Bearer ' + ACCESS_TOKEN,
          },
          'method': 'post',
          'payload': JSON.stringify({
            'replyToken': replyToken,
            'messages': [{
              'type': 'text',
              'text': replyMessage,
            }],
          }),
        }); 
      }
      
      else{
        UrlFetchApp.fetch(url, {
          'headers': {
            'Content-Type': 'application/json; charset=UTF-8',
            'Authorization': 'Bearer ' + ACCESS_TOKEN,
          },
          'method': 'post',
          'payload': JSON.stringify({
            'replyToken': replyToken,
            'messages': [
              {
                "type": "template",
                "altText": "this is a buttons template",
                "template": {
                  "type": "buttons",
                  "actions": [
                    {
                      "type": "message",
                      "label": "menu",
                      "text": "menu"
                    },
                  ],
                    "text": replyMessage
                    }
                    }
                    
                  ],
                }),
              }); 
              }
              }
 //             }
  return ContentService.createTextOutput(JSON.stringify({'content': 'post ok'})).setMimeType(ContentService.MimeType.JSON);
}
              }
```