我读到你可以与反向通道共享位置。
我想使用此代码直接与机器人对话:
function postButtonMessage() {
botConnection.postActivity({
entities:{type: "ClientCapabilities", requiresBotState: true, supportsTts: true, supportsListening: true},
from: { id: 'userid', name: 'username' },
name: 'botname',
type: 'message',
value: 'Hi',
textFormat: 'plain'
})
.subscribe(function (id) {
console.log('"buttonClicked" sent');
});
};
但是我收到一个错误,说"错误的网关 502",但是当我通过 Web 通道窗谈时,它工作正常,所以我知道直接线路键配置正确。当我使用类型时:event
而不是message
它工作正常并且我没有问题,所以我对此感到困惑。
(问题最初在 https://github.com/Microsoft/BotFramework-WebChat/issues/778#中提出(
这里有两种方法。
一种是使用反向通道将event
活动发送到机器人,其中包含您喜欢的任何数据。您可以在单击按钮时、定期或位置更改时执行此操作。
var dl = new BotChat.DirectLine({secret});
BotChat.App({
botConnection: dl,
// other Chat props go here
});
function postButtonMessage() {
dl.postActivity({
from: { id: 'userid', name: 'username' },
type: 'event',
name: 'location',
value: { /* location goes here */ }
})
.subscribe(id => {
console.log('"buttonClicked" sent');
});
};
另一种方法是使用客户端中间件在每条消息发出时拦截和修改该数据,从而随每条消息一起发送数据。这种方法的优点是每条消息都"标记"了其位置。缺点是只有在用户发送消息时才能获得位置更新。
var dl = new BotChat.DirectLine({secret});
BotChat.App({
botConnection: {
... dl,
postActivity: activity => dl.postActivity({
... activity,
channelData: { location: /* location goes here */ }
})
},
// other Chat props go here
});
当然,你可以两者兼而有之!