Javascript 通过Bot框架中的后端通道发送位置详细信息
我正在使用此方法将位置详细信息传递给机器人。但当我试图传递地址时,它无法获取bot中的所有详细信息。我对fulladdress变量使用alert。它向我显示地址,但无法在bot中获取值。我正在尝试获取bot活动或事件的详细信息 你能帮我吗Javascript 通过Bot框架中的后端通道发送位置详细信息,javascript,c#,botframework,Javascript,C#,Botframework,我正在使用此方法将位置详细信息传递给机器人。但当我试图传递地址时,它无法获取bot中的所有详细信息。我对fulladdress变量使用alert。它向我显示地址,但无法在bot中获取值。我正在尝试获取bot活动或事件的详细信息 你能帮我吗 <script type="text/javascript"> // for location of user.... var geocoder; var fulladdress = {};
<script type="text/javascript">
// for location of user....
var geocoder;
var fulladdress = {};
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng)
}
function errorFunction() {
alert("Geocoder failed");
}
function initialize() {
geocoder = new google.maps.Geocoder();
}
function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//formatted address
alert(results[0].formatted_address)
// fulladdress = results[0].formatted_address;
fulladdress = { 'latitude': results[0].formatted_address};
//find country name
for (var i = 0; i < results[0].address_components.length; i++) {
for (var b = 0; b < results[0].address_components[i].types.length; b++) {
//there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
//this is the object you are looking for
city = results[0].address_components[i];
break;
}
}
}
//city data
alert(city.short_name + " " + city.long_name);
alert(fulladdress.latitude);
}
else {
alert("No results found");
}
}
else {
alert("Geocoder failed due to: " + status);
}
});
}
// for Bot .......
var model = {
"userId": '_' + Math.random().toString(36).substr(2, 9),
"userName": fulladdress.latitude,
"botId": "Chatbot Main Tree",
"botIconUrl": "//bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-8.png",
"botName": "Chatbot Main Tree",
"secret": "Secret",
"iconUrl": "//bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-8.png",
"directLineUrl": "https://webchat.botframework.com/v3/directline",
"webSocketEnabled": "True"
};
BotChat.App({
sendTyping: true,
user: { id: model.userId, name: model.userName },
bot: { id: model.botId, name: model.botName },
speechOptions: speechOptions,
resize: 'window',
directLine: {
secret: model.secret,
token: model.token,
domain: model.directLineUrl,
webSocket: true,
sendTyping: true
}
}, document.getElementById("BotChatElement"));
});</script>
//有关用户的位置。。。。
var地理编码器;
var fulladdress={};
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(successFunction,errorFunction);
}
//得到纬度和经度;
功能成功功能(位置){
var lat=位置坐标纬度;
var lng=位置坐标经度;
codeLatLng(lat,lng)
}
函数errorFunction(){
警报(“地理编码器失败”);
}
函数初始化(){
geocoder=新的google.maps.geocoder();
}
功能代码LATLNG(lat,lng){
var latlng=新的google.maps.latlng(lat,lng);
geocoder.geocode({'latLng':latLng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
console.log(结果)
如果(结果[1]){
//格式化地址
警报(结果[0]。格式化的\u地址)
//fulladdress=结果[0]。格式化的\u地址;
fulladdress={“纬度”:结果[0]。格式化的_地址};
//查找国家名称
对于(var i=0;i
模型和BotChat.App用于在网页上实例化bot,并不用于在托管网页和bot之间传输数据。为此,您需要使用.postActivity()和bot.on()。您可以阅读有关设置backchannel的更多信息
您可以看到一个演示发送地理位置数据的示例。在main.js文件中查找.postActivity()。该示例显示了如何从浏览器API以及从服务发送地理位置信息
希望有帮助
在网页上,事件发布在以下一些用户/页面操作中:
botConnection
.postActivity({type: "event", value: "", from: {id: "me" }, name: "buttonClicked"})
.subscribe(id => console.log("success"));
在bot中,捕获发布的事件并对其执行操作:
bot.on("event", function (event) {
var msg = new builder.Message().address(event.address);
msg.data.textLocale = "en-us";
if (event.name === "buttonClicked") {
msg.data.text = "I see that you clicked a button.";
}
bot.send(msg);
})
它向我显示地址,但无法在bot中获取值。我正在尝试获取bot活动或事件的详细信息
根据您的代码,我们可以发现您通过user:{id:model.userId,name:model.userName}
传递位置信息要启动您的BotChat,您可以使用以下代码片段检索传递的位置信息
在bot应用中:
此外,正如Steven Kanberg所提到的,您还可以使用反向通道机制在客户端和bot之间交换信息,而无需向用户展示
在网页上:
在MessagesController中:
测试结果:
注意:
,我建议您可以将赋值给model
的userName
属性并在回调函数中初始化BotChat的代码片段,如下所示
function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//your code logic
var model = {
"userId": 'user_' + Math.random().toString(36).substr(2, 9),
"userName": fulladdress.latitude,
"botId": "Chatbot Main Tree",
"botIconUrl": "//bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-8.png",
"botName": "Chatbot Main Tree",
"secret": "Secret",
"iconUrl": "//bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-8.png",
"directLineUrl": "https://webchat.botframework.com/v3/directline",
"webSocketEnabled": "True"
};
var botConnection = new BotChat.DirectLine({ secret: model.secret });
BotChat.App({
directLine: botConnection,
user: { id: model.userId, name: model.userName },
//...
//other options
//...
}, document.getElementById("BotChatElement"));
botConnection.postActivity({
type: 'event',
from: { id: model.userId },
name: 'GetUserLocation',
value: model.userName
}).subscribe(function (id) { console.log('UserLocation: "' + model.userName + '" sent'); });
}
else {
alert("No results found");
}
}
else {
alert("Geocoder failed due to: " + status);
}
});
}
您将此地址发送到您的bot的何处?看起来您没有发布此信息。在我的情况下,没有按钮单击。我只想将变量(html上的js变量)上的值传递给BOT。现在的情况是如何发送值以及如何在bot.中获取该值,我正在使用c#bot sdk。如何将bot.on代码转换为c#我正在使用c#抱歉。不知怎么的,我看不出你需要c#解决方案。请参考我在上面提到的费的回答
botConnection.postActivity({
type: 'event',
from: { id: model.userId },
name: 'GetUserLocation',
value: model.userName
}).subscribe(function (id) { console.log('UserLocation: "' + model.userName + '" sent'); });
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
//......
//code logic for other messages types
//......
else if (message.Type == ActivityTypes.Event && message.Name == "GetUserLocation")
{
var userlocation = message.Value;
//your code logic
}
return null;
}
function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//your code logic
var model = {
"userId": 'user_' + Math.random().toString(36).substr(2, 9),
"userName": fulladdress.latitude,
"botId": "Chatbot Main Tree",
"botIconUrl": "//bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-8.png",
"botName": "Chatbot Main Tree",
"secret": "Secret",
"iconUrl": "//bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-8.png",
"directLineUrl": "https://webchat.botframework.com/v3/directline",
"webSocketEnabled": "True"
};
var botConnection = new BotChat.DirectLine({ secret: model.secret });
BotChat.App({
directLine: botConnection,
user: { id: model.userId, name: model.userName },
//...
//other options
//...
}, document.getElementById("BotChatElement"));
botConnection.postActivity({
type: 'event',
from: { id: model.userId },
name: 'GetUserLocation',
value: model.userName
}).subscribe(function (id) { console.log('UserLocation: "' + model.userName + '" sent'); });
}
else {
alert("No results found");
}
}
else {
alert("Geocoder failed due to: " + status);
}
});
}