Send a message from Facebook webview to bot

A bot was written on the ms bot framework for Facebook Messenger, which creates a carousel using a user channel attachment with web_url , which allows you to expand messages: "messenger_extensions": true . We have added Messenger extensions on the web view page, but it is unclear how to send a message with an attachment from this web page back to the messenger and, therefore, to the bot.

 <!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 psid " + psid); }, function error(err) { alert("Messenger Extension Error: " + err); }); }; </script> </body> </html> 

Read through tons of documentation and blogs including stackoverflow: https://stackoverflow.com/a/412477/

Is there a simple JavaScript script example embedded in the page? Thanks!

+1
source share
2 answers

If I understand the question correctly, you can configure the API endpoint that triggers the sending of the message and hit that endpoint in the success callback "MessengerExtensions.requestCloseBrowser ().

An example of using the jQuery module and node express:

Webview:

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

Server:

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

You can send parameters with a get request ( https: // someurl? UserChannelID = ), and then use them in your js code, in a triger message from your rank (we use a straight line)

+1
source

Source: https://habr.com/ru/post/1015610/


All Articles