从Facebook的webview发送消息到机器人
问题描述:
已经为Facebook Messenger的ms bot框架写了bot,它使用custom channel data attachment和web_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://*.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>
读过吨documentation和blogs包括计算器:https://*.com/a/44536739/630169。
在页面上嵌入JavaScript脚本有简单的例子吗?谢谢!
答
如果我理解了正确的问题,可以设置一个触发消息发送的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);
})
可能有可能,但是如何将这个'psid'转换为机器人构建器'message.address'对象('IAddress'): ' “渠道ID”: “模拟器”, “用户”:{ “ID”: “默认用户”, “名”: “用户” }, “对话”:{ “ID”:“dehg2kfnh9703g94c “ }, ”BOT“:{ ”ID“: ”默认机器人“, ”名“: ”博特“ }, ”的serviceUrl“: ”http://127.0.0.1:46839“ } }'''发送消息返回使用R 111 –
所以总结:问题出在你的'sendMessage(psid);' - 如何实现这个?用于发送消息的Bot Framework代码是:var'reply = new builder.Message() .address(address) .text(“Hi!”); bot.send(reply);'在这种情况下如何从'PSID'制作'IAddress'对象? –
好吧,我明白了。在这种情况下,您不会将PSID传递回您的后端。 一个选项可能是当您打开web视图时将对话数据中的id作为查询参数传递。因此,例如,如果您使用url按钮打开webview: “buttons”:[ { “type”:“web_url”, “url”:“https://mywebviewurl.com/?conversationID =”, ‘称号’:‘查看项目’, } ] 然后,你可以解析id列,并通过回托特他后端时,web视图关闭: $。员额(的“https://myapi。com/sendOnWebviewClose',{“id”:idFromURLQuery}) –
amuramoto