2017-08-28 50 views
1

已經爲Facebook Messenger的ms bot框架寫了bot,它使用custom channel data attachmentweb_url創建了carousel,它使得信使擴展爲:"messenger_extensions": true。我們在網頁瀏覽頁面上有Added Messenger Extensions,但是不清楚如何將消息與附件從此webview頁面發送回信使,因此發送到bot框架。從Facebook的webview發送消息到機器人

<!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: "[email protected]", 
         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包括計算器:https://stackoverflow.com/a/44536739/630169

在頁面上嵌入JavaScript腳本有簡單的例子嗎?謝謝!

回答

2

如果我理解了正確的問題,可以設置一個觸發消息發送的API端點,並在MessengerExtensions.requestCloseBrowser()的成功回調中擊中該端點。

實施例使用jQuery和節點的明確模塊:

網頁視圖:

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); 
}) 
+0

可能有可能,但是如何將這個'psid'轉換爲機器人構建器'message.address'對象('IAddress'): ' 「渠道ID」: 「模擬器」, 「用戶」:{ 「ID」: 「默認用戶」, 「名」: 「用戶」 }, 「對話」:{ 「ID」:「dehg2kfnh9703g94c 「 }, 」BOT「:{ 」ID「: 」默認機器人「, 」名「: 」博特「 }, 」的serviceUrl「: 」http://127.0.0.1:46839「 } }'''發送消息返回使用R 111 –

+0

所以總結:問題出在你的'sendMessage(psid);' - 如何實現這個?用於發送消息的Bot Framework代碼是:var'reply = new builder.Message() .address(address) .text(「Hi!」); bot.send(reply);'在這種情況下如何從'PSID'製作'IAddress'對象? –

+1

好吧,我明白了。在這種情況下,您不會將PSID傳遞迴您的後端。 一個選項可能是當您打開web視圖時將對話數據中的id作爲查詢參數傳遞。因此,例如,如果您使用url按鈕打開webview: 「buttons」:[ { 「type」:「web_url」, 「url」:「https://mywebviewurl.com/?conversationID = 」, ‘稱號’:‘查看項目’, } ] 然後,你可以解析id列,並通過回托特他後端時,web視圖關閉: $。員額(的「https://myapi。com/sendOnWebviewClose',{「id」:idFromURLQuery}) – amuramoto

1

,能夠與所述get請求發送參數,(https://someurl?userChannelID=),然後在您的js代碼中使用它們,以激發您服務器發送的消息(我們使用directline)