@osagari

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

LINEリッチメニューでボタンを押したらアンケートが実施→URLを表示

解決したいこと

LINEのリッチメニューで、ボタンを押したらプッシュメッセージにてアンケートを実施したい

発生している問題・エラー

右下ボタンを押しても何も動作しない

該当するソースコード

GAS
function createRichMenuA() {  
  let richmenu = bot.richmenu({
    "name": "移住希望者A",
    "barText": "←キーボード / メニュー",
    "size": { "width": 2500, "height": 1686 },
    "selected": true, 
    "areas": [
      bot.area({ "x": 0,    "y": 0, "width": 1250, "height": 220,
      "action": bot.aSwitch({"aliasId": "switch-to-a", "data": "change to A"}) }),
      bot.area({ "x": 1250, "y": 0, "width": 1250, "height": 220,
      "action": bot.aSwitch({"aliasId": "switch-to-c", "data": "change to C"}) }),

      bot.area({ "x": 0,    "y": 220, "width": 2500, "height": 733,  //真ん中横長・アンケート
      "action": bot.aMessage({"text": "真ん中"}) }),
      bot.area({ "x": 0,    "y": 954, "width": 833, "height": 733,  //左下・webページ
      "action": bot.aUri({"uri": "https://ieno-osagari.studio.site/"}) }),
      bot.area({ "x": 834,  "y": 954, "width": 833, "height": 733,  //真ん中下・webページ
      "action": bot.aUri({"uri": "https://ieno-osagari.studio.site/"}) }),
      bot.area({ "x": 1667, "y": 954, "width": 833, "height": 733,  //右下・アンケート
      "action": bot.aPostback({
        "data": "sendSurvey"
      }) }),  
    ]
  });
  
  let res = bot.createRichMenu(richmenu);
  console.log("Rich menu created: " + res.toString()); 
}

// Postbackイベントを処理する関数
function handlePostback(event) {
  console.log("Postback received: " + event.postback.data);
  
  if (event.postback.data === "sendSurvey") {
    sendSurvey(event);
  } else if (event.postback.data.startsWith("answer-")) {
    handleNextStep(event);
  }
}

// アンケート用のプッシュメッセージを送信する関数
function sendSurvey(event) {
  console.log("Sending survey to: " + event.source.userId);
  
  bot.pushMessage(event.source.userId, {
    "type": "text",
    "text": "あなたの移住希望地を選んでください。",
    "quickReply": {
      "items": [
        {
          "type": "action",
          "action": {
            "type": "postback",
            "label": "北海道",
            "data": "answer-hokkaido"
          }
        },
        {
          "type": "action",
          "action": {
            "type": "postback",
            "label": "沖縄",
            "data": "answer-okinawa"
          }
        },
        {
          "type": "action",
          "action": {
            "type": "postback",
            "label": "東京",
            "data": "answer-tokyo"
          }
        },
        {
          "type": "action",
          "action": {
            "type": "postback",
            "label": "京都",
            "data": "answer-kyoto"
          }
        }
      ]
    }
  });
}

// 次のステップに進む処理
function handleNextStep(event) {
  let uri = '';
  switch (event.postback.data) {
    case 'answer-hokkaido':
      uri = 'https://example.com/hokkaido';  // 北海道のURL
      break;
    case 'answer-okinawa':
      uri = 'https://example.com/okinawa';  // 沖縄のURL
      break;
    case 'answer-tokyo':
      uri = 'https://example.com/tokyo';  // 東京のURL
      break;
    case 'answer-kyoto':
      uri = 'https://example.com/kyoto';  // 京都のURL
      break;
  }

  if (uri) {
    console.log("Redirecting to: " + uri);
    bot.pushMessage(event.source.userId, {
      type: 'text',
      text: 'こちらのリンクをご覧ください。',
      quickReply: {
        items: [
          {
            type: "action",
            action: {
              type: "uri",
              label: "リンク先はこちら",
              uri: uri
            }
          }
        ]
      }
    });
  }
}

// メインイベントハンドラ
function handleEvent(event) {
  if (event.type === 'postback') {
    handlePostback(event);
  } else if (event.type === 'message' && event.message.type === 'text') {
    handleResponse(event);  // 他のメッセージイベントも処理する場合
  }
}

// デバッグ用にログを追加
console.log("Bot started and waiting for events...");

自分で試したこと

GASでの実行、Webhook URL での認証は成功します

0 likes

1Answer

function handleEvent(event) { の次の行に console.log("handleEvent: " + event) と書いてみて右下のボタンを押すとログは出力されますか?

0Like

Comments

  1. @osagari

    Questioner

    スクリーンショット (882).png

    実行数からログを確認する方法を、調べて初めて知りました。
    結果、ボタンを押した先はエラーになっていることがわかりましたが、その原因については不明なままです。

    スクリーンショット (883).png

Your answer might help someone💌