【发布时间】:2020-01-30 13:57:51
【问题描述】:
我需要帮助使用来自 Microsoft 的 BotFramework 的 webchat v4 通过 Javascript 将消息发送回机器人。
我的机器人代码是用 C# (.NET Framework) 编写的,但在我的场景中的某个时刻,我需要触发一些 javascript 来向用户询问位置。我该怎么办?
- 我发送事件类型的活动
- 在机器人的 store 中,我捕捉到了上述活动(这是我的代码):
const store = window.WebChat.createStore({},
({ dispatch }) => next => action => {
//console.log('action of type ' + action.type);
if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
var activityOut = action.payload.activity;
if (activityOut.type === 'message') {
// Store message
return next(action);
}
if (activityOut.type === 'event') {
// Handle based on event type
if (activityOut.name === 'getLocation') {
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else
{
console.log("Geolocation is not supported by this browser.");
}
} else {
return next(action);
}
}
}
else if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
var activityIn = action.payload.activity;
if (activityIn.type === 'message') {
// Store message
}
return next(action);
}
else {
return next(action);
}
});
- 我将信息发送给机器人(这里是位置):
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude + " Longitude: " + position.coords.longitude);
var v = position.coords.latitude +';' + position.coords.longitude;
store.dispatch({
type: 'WEB_CHAT/SEND_MESSAGE_BACK',
payload:
{
text: v,
value : v
}
});
}
我也尝试过使用“WEB_CHAT/SEND_MESSAGE”,但没有任何改变。
有关更多信息,这是我连接到机器人的一段代码:
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine({
secret: 'my secret (will change to a token of course)'
}),
store,
userID: chatUser.id,
username: chatUser.name,
locale: 'fr-FR',
styleOptions
},
document.getElementById('BotChatGoesHere')
);
已经感谢任何可以帮助我的人!
【问题讨论】:
-
接受/投票支持更大的 Stack Overflow 社区和任何有类似问题的人。如果您觉得我的回答足够,请“接受”并点赞。如果没有,请告诉我我还能提供哪些帮助!
标签: javascript botframework web-chat