facebook messaging current_thread不返回psid

时间:2018-05-23 12:27:35

标签: javascript html webhooks facebook-messenger

我正在尝试使用facebook MessengerExtensions.beginShareFlow()发送请求。我尝试发送此API,以便重定向到名为schedules的新页面。

var messageToShare = {
  "attachment":{
    "type":"template",
    "payload":{
      "template_type":"generic",
      "elements": [{
        "title":"Garfield is going to the cinema",
        "image_url": "https://webhookboi.herokuapp.com/schedule",
        "subtitle": "You have been invited to join them, let them know when you are free",
        "default_action": {
              "type": "web_url",
              "url": "https://webhookboi.herokuapp.com/schedule",
              "messenger_extensions": "true",
              "webview_height_ratio": "full",
              "fallback_url": "https://webhookboi.herokuapp.com/schedule"
            },
        "buttons":[{
          "type":"web_url",
          "url":"https://webhookboi.herokuapp.com/schedule",
          "title":"Select Times",
          "messenger_extensions": "true"
        }]
      }]
    }
  }
};

这是封装在提交按钮监听器

中的beginShareFlow()
document.getElementById('submitButton').addEventListener('click', () => {

            MessengerExtensions.beginShareFlow(function success(response) {
              if(response.is_sent === true){
                // User shared. We're done here!
                MessengerExtensions.requestCloseBrowser();
              }
              else{
                // User canceled their share!
                MessengerExtensions.requestCloseBrowser();
              }
            },
            function error(errorCode, errorMessage) {
            // An error occurred trying to share!
            },
            messageToShare,
            "current_thread");


        });

请求将在主javascript

中截获
app.get('/schedule', (req, res, next) => {
    let referer = req.get('Referer');
    if (referer) {
        if (referer.indexOf('www.messenger.com') >= 0) {
            res.setHeader('X-Frame-Options', 'ALLOW-FROM https://www.messenger.com/');
        } else if (referer.indexOf('www.facebook.com') >= 0) {
            res.setHeader('X-Frame-Options', 'ALLOW-FROM https://www.facebook.com/');
        }
        res.sendFile('public/schedule.html', {root: __dirname});
    }
});

app.get('/schedulepostback', (req, res) => {
    let body = req.query;
    let response = {
        "text": `Great, you have agreed to watch on ${body.dates} in the ${body.times} at ${body.locations} `
    };


    res.status(200).send('Please close this window to return to the conversation thread.');
    callSendAPI(body.psid, response);
});

这是主要问题,在下面的代码片段中,我试图再次调用线程psid,但它从未显示过。

<html>
<head>
    <title>Set your Schedule</title>
</head>
<body>
<script>
    (function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) {
            return;
        }
        js = d.createElement(s);
        js.id = id;
        js.src = "//connect.facebook.com/en_US/messenger.Extensions.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'Messenger'));
    window.extAsyncInit = () => {
        // TODO: How to parse env file from here?
        MessengerExtensions.getSupportedFeatures(function success(result) {
            let features = result.supported_features;
            if (features.includes("context")) {
                MessengerExtensions.getContext(INPUTED_APP_ID_HERE,
                    function success(thread_context) {
                        // success
                        document.getElementById("psid").value = thread_context.psid;
                        document.getElementById("tid").value = thread_context.tid;
                        
                    },
                    function error(err) {
                        // error
                        console.log(err);
                    }
                );
            }
        },
         function error(err) {
            // error retrieving supported features
            console.log(err);
        });



        var messageToShare = {
            "text": `Your friend requested to watch on`.document.querySelector('input[name="dates"]:checked').value.'in the'.document.querySelector('input[name="times"]:checked').value.'at'.document.querySelector('input[name="locations"]:checked').value
};



        document.getElementById('submitButton').addEventListener('click', () => {

            MessengerExtensions.beginShareFlow(function success(response) {
              if(response.is_sent === true){
              	// User shared. We're done here!
              	MessengerExtensions.requestCloseBrowser();
              }
              else{
              	// User canceled their share!
                MessengerExtensions.requestCloseBrowser();
              }
            },
            function error(errorCode, errorMessage) {
          	// An error occurred trying to share!
            },
            messageToShare,
            "current_thread");


        });
    };
</script>
<form action="/schedulepostback" method="get">
    <input type="text" name="psid" id="psid">
    <input type="text" name="tid" id="tid">
    <h3>Date</h3>
    <input type="radio" name="dates" value="3 April" checked>3 April<br>
    <input type="radio" name="dates" value="4 April">4 April<br>
    <input type="radio" name="dates" value="5 April">5 April<br>
    <input type="radio" name="dates" value="6 April">6 April<br>
    <input type="radio" name="dates" value="7 April">7 April<br>
    <h3>Time</h3>
    <input type="radio" name="times" value="Daytime" checked>Daytime<br>
    <input type="radio" name="times" value="Evening">Evening<br>
    <h3>Location</h3>
    <input type="radio" name="locations" value="Munich" checked>Munich<br>
    <input type="radio" name="locations" value="Berlin">Berlin<br>
    <input type="radio" name="locations" value="Dusseldorf">Dusseldorf<br>
    <input type="submit" value="Submit" id="submitButton">
</form>
</body>
</html>

这是因为我的模板类型吗?或者是其他东西?任何帮助都会得到满足。

修改

如果我点击聊天扩展程序,一切正常,并且信使扩展程序可以生成此聊天回发。

result of the chat extension postback

然而,当我点击回发上的“选择时间”按钮时,psid doest显示。前三个栏应该显示psid,tid和object。这也没有在X-Frame中弹出,而是在新标签中弹出。这里有什么帮助吗?

the result of the select times button

0 个答案:

没有答案