将消息从Facebook webview发送回bot

时间:2017-08-28 16:55:37

标签: node.js webview botframework webhooks facebook-messenger

在Facebook Messenger的ms bot框架上编写了bot,使用carousel创建custom channel data attachmentweb_url启用了信使扩展:"messenger_extensions": true。我们在网页浏览页面上有Added Messenger Extensions,但不清楚如何从此网页浏览页面向邮件发送消息附件,从而发送到僵尸框架。

<!DOCTYPE html>
<html>

<body>
    <style type="text/css">
        .button {
            background-color: #4CAF50;
            /* Green */
            border: none;
            color: white;
            padding: 15px 32px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            width: 50%;
            margin: 25%;
        }
    </style>

    <button type="button" class="button" onclick="sendMessage();">Complete Booking</button>

    <script type="text/javascript">
        function sendMessage() {
            alert('Booking completed. What to do now? How to send the message back to bot?')
            /// how to return? the facebook docs don't say anything
            /// you HAVE to make a server round trip.. https://stackoverflow.com/questions/43956045/messengerextensions-how-to-send-a-message-by-messenger-to-users
            return {
                text: "HOTEL_SERVICE_PAYLOAD",
                attachments: [
                    {
                        email: "some@email.com",
                        hotelName: "Hotel marriott",
                        confirmNumber: "1234567"
                    }
                ]
            }
            MessengerExtensions.requestCloseBrowser(function success() {

            }, function error(err) {

            });
        }

        (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 = function () {
            // the Messenger Extensions JS SDK is done loading
            MessengerExtensions.getUserID(function success(uids) {
                var psid = uids.psid;//This is your page scoped sender_id
                alert("Getting PSID")
                alert("This is the user's psid " + psid);
            }, function error(err) {
                alert("Messenger Extension Error: " + err);
            });
        };
    </script>
</body>
</html>

已阅读大量documentationblogs,包括stackoverflow:https://stackoverflow.com/a/44536739/630169

是否有嵌入页面的JavaScript脚本的简单示例?谢谢!

2 个答案:

答案 0 :(得分:3)

如果我理解正确的问题,您可以设置一个触发消息发送的API端点,并在`MessengerExtensions.requestCloseBrowser()的成功回调中点击该端点。

使用jQuery和node的express模块​​的示例:

网页视图:

window.extAsyncInit = function () {
    // the Messenger Extensions JS SDK is done loading
    MessengerExtensions.getUserID(function success(uids) {
        var psid = uids.psid;//This is your page scoped sender_id
        $.post('https://myapi.com/sendOnWebviewClose', {"psid": psid})
    }, function error(err) {
        alert("Messenger Extension Error: " + err);
    });
};

服务器:

app.post('/sendOnWebviewClose', (req, res) => {
  let psid = req.body.psid;
  sendMessage(psid);
})

答案 1 :(得分:1)

可以使用get请求发送参数(https://someurl?userChannelID=),然后在js代码中使用它们,以便从服务器中删除消息(我们使用直接线)