@osagari

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

フォロー時に表示されるアンケートを、LINEリッチメニューでボタンを押した際も同様に表示する

解決したいこと

フォロー時に表示されるアンケートを、
LINEリッチメニューでボタンを押した際も同様に表示したい

エラー該当箇所

// LINE友達追加時の処理
    if (event.type == "follow") {                                     
      survey_gender(userId);  // 性別の質問を送信
    }

    // テキストメッセージ処理
    if(event.type == "message" && event.message.type == "text") {
      if(event.message.text === "ユーザー登録とプライバシーポリシーに進む") {
        survey_gender(userId);  // 性別の質問を送信
      }
    }

該当するソースコード

GAS
// 参考サイト:https://webmarketerokb.com/2021/05/04/line-botapi%e3%81%a7%e3%82%a2%e3%83%b3%e3%82%b1%e3%83%bc%e3%83%88%e3%81%ae%e3%82%88%e3%81%86%e3%81%aa%e9%81%b8%e6%8a%9e%e8%82%a2%e3%81%a4%e3%81%8d%e3%81%ae%e8%b3%aa%e5%95%8f%e3%82%92%e4%bd%9c/

var channel_token = PropertiesService.getScriptProperties().getProperty('LINEb_TOKEN');
var push_url = "https://api.line.me/v2/bot/message/push";
var reply_url = "https://api.line.me/v2/bot/message/reply";
var spreadsheet = SpreadsheetApp.openById("1QH0iozU5snvQGwA-vEXOB4XoigX6ZRFiprHii_lF9C4");
var sheet_userlist = spreadsheet.getSheetByName('userlist');

// LINEからのイベントがdoPostにとんでくる
function doPost(e) {
  var json = e.postData.contents;
  var events = JSON.parse(json).events;
  var dat = sheet_userlist.getDataRange().getValues();

  events.forEach(function(event) {
    var userId = event.source.userId;
    var json  = UrlFetchApp.fetch("https://api.line.me/v2/bot/profile/" + userId, {"headers" : {"Authorization" : "Bearer " + channel_token}});
    var displayName = JSON.parse(json).displayName;

    for(var i=1;i<dat.length;i++){
      if(dat[i][0] == userId){
        break;
      }
    }
    if(i==dat.length) {
      sheet_userlist.appendRow([userId, displayName]); 
    }

    // LINE友達追加時の処理
    if (event.type == "follow") {                                     
      survey_gender(userId);  // 性別の質問を送信
    }

    // テキストメッセージ処理
    if(event.type == "message" && event.message.type == "text") {
      if(event.message.text === "ユーザー登録とプライバシーポリシーに進む") {
        survey_gender(userId);  // 性別の質問を送信
      }
    }

    
    if(event.type == "postback") {
      var w_data = event.postback.data.split("&")[0].replace("data=","");
      var w_item = event.postback.data.split("&")[1].replace("item=","");

      if(w_data == "survey1") {
        sheet_userlist.getRange(i+1, 3).setValue(w_item);
        survey_age(event);
      }
      else if(w_data == "survey2") {
        sheet_userlist.getRange(i+1, 4).setValue(w_item);
        survey_attribute(event);
      }
      else if(w_data == "survey3") {
        sheet_userlist.getRange(i+1, 5).setValue(w_item);
        survey_agreement(event); // 同意確認の質問を追加
      }
      else if(w_data == "survey4") {
        sheet_userlist.getRange(i+1, 6).setValue(w_item); // 'はい' の選択を記録
        survey_end(event);
      }
    }
  });
}

function survey_demogra() {
  var sheet = SpreadsheetApp.openById("1QH0iozU5snvQGwA-vEXOB4XoigX6ZRFiprHii_lF9C4");
  var ss = sheet.getSheetByName('userlist');
  var dat = ss.getDataRange().getValues();
  for(var i=1;i<dat.length;i++){
    push_survey(dat[i][0]);
  }
}

function push_survey(userId){
  var headers = {
    "Content-Type" : "application/json; charset=UTF-8",
    'Authorization': 'Bearer ' + channel_token,
  };
  var postData = {
    "to" : userId,
    'messages' : [
      {
        "type": "text",
        "text" : "簡単な登録を進めるにあたり、4問ほど回答ください。\nまずはあなたの性別を選択してください。",
        "quickReply": {
          "items": [
            {
              "type": "action",
              "action": {
                  "type": "postback",
                  "label": "男性",
                  "data":"data=survey1&item=男性",
                  "displayText": "男性"
              }
            },
            {
              "type": "action",
              "action": {
                  "type": "postback",
                  "label": "女性",
                  "data":"data=survey1&item=女性",
                  "displayText": "女性"
              }
            }
          ]
        }
      }
    ]
  };
  var options = {
    "method" : "post",
    "headers" : headers,
    "payload" : JSON.stringify(postData)
  };
  
  UrlFetchApp.fetch(push_url, options);
}

function survey_age(event){
  var message = {
    "replyToken" : event.replyToken,
    'messages' : [
      {"type": "text","text" : "年齢層を選択してください。",
        "quickReply": {
          "items": [
            {
              "type": "action",
              "action": {
                "type": "postback",
                "label": "20歳未満",
                "data":"data=survey2&item=20歳未満",
                "displayText": "20歳未満"
              }
            },
            {
              "type": "action",
              "action": {
                "type": "postback",
                "label": "20代",
                "data":"data=survey2&item=20代",
                "displayText": "20代"
              }
            },
            {
              "type": "action",
              "action": {
                "type": "postback",
                "label": "30代",
                "data":"data=survey2&item=30代",
                "displayText": "30代"
              }
            },
            {
              "type": "action",
              "action": {
                "type": "postback",
                "label": "40代",
                "data":"data=survey2&item=40代",
                "displayText": "40代"
              }
            },
            {
              "type": "action",
              "action": {
                "type": "postback",
                "label": "50代",
                "data":"data=survey2&item=50代",
                "displayText": "50代"
              }
            },
            {
              "type": "action",
              "action": {
                "type": "postback",
                "label": "60歳以上",
                "data":"data=survey2&item=60歳以上",
                "displayText": "60歳以上"
              }
            }
          ]
        }}
    ]
  };
  var options = {
    "method" : "post",
    "headers" : {
      "Content-Type" : "application/json",
      "Authorization" : "Bearer " + channel_token
    },
    "payload" : JSON.stringify(message)
  };
  
  UrlFetchApp.fetch(reply_url, options);
}

function survey_attribute(event) {
  var message = {
    "replyToken" : event.replyToken,
    'messages' : [
      {"type": "text","text" : "属性を選択してください。",
        "quickReply": {
          "items": [
            {
              "type": "action",
              "action": {
                  "type": "postback",
                  "label": "〇",
                  "data":"data=survey3&item=〇",
                  "displayText": "〇"
              }
            },
            {
              "type": "action",
              "action": {
                  "type": "postback",
                  "label": "×",
                  "data":"data=survey3&item=×",
                  "displayText": "×"
              }
            }
          ]
        }}
    ]
  };

  var options = {
    "method" : "post",
    "headers" : {
      "Content-Type" : "application/json",
      "Authorization" : "Bearer " + channel_token
    },
    "payload" : JSON.stringify(message)
  };

  UrlFetchApp.fetch(reply_url, options);
}

function survey_agreement(event) {
  var message = {
    "replyToken" : event.replyToken,
    'messages' : [
      {"type": "text","text" : "プライバシーポリシー同意しますか?\n https://docs.google.com/document/d/1UT1Oj1iffLVXYw17bHRTZRDEntl8874mPy4DImcK0Bo/edit?usp=sharing",
        "quickReply": {
          "items": [
            {
              "type": "action",
              "action": {
                  "type": "postback",
                  "label": "はい",
                  "data":"data=survey4&item=はい",
                  "displayText": "はい"
              }
            }
          ]
        }}
    ]
  };

  var options = {
    "method" : "post",
    "headers" : {
      "Content-Type" : "application/json",
      "Authorization" : "Bearer " + channel_token
    },
    "payload" : JSON.stringify(message)
  };

  UrlFetchApp.fetch(reply_url, options);
}

function survey_end(event){ 
  var message = {
    "replyToken" : event.replyToken,
    'messages' : [
      {"type": "text","text" : "アンケートのご協力ありがとうございました!"}
    ]
  };
  
  var options = {
    "method" : "post",
    "headers" : {
      "Content-Type" : "application/json",
      "Authorization" : "Bearer " + channel_token
    },
    "payload" : JSON.stringify(message)
  };
  
  UrlFetchApp.fetch(reply_url, options);
}

自分で試したこと

ログを確認したところ、エラーが表示されていることがわかりました

0 likes

1Answer

Comments

  1. @osagari

    Questioner

    関数がdoPostだからでしょうか。押下しても何も起きず、特段エラー内容を確認できません。

Your answer might help someone💌