BotFramework Webchat v4 通过 javascript 发回消息
BotFramework Webchat v4 Sending message back through the javascript
我需要帮助使用来自 Microsoft BotFramework 的 webchat v4 通过 Javascript.
将消息发送回机器人
我的机器人代码是用 C# (.NET Framework) 编写的,但在我的场景中的某个时刻,我需要触发一些 javascript 来向用户询问位置。我该怎么办?
- 我发送一个 activity 类型的 事件
- 在机器人的 store 中,我捕获了所说的 activity(这是我的代码):
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')
);
这是我每次执行此操作时遇到的异常:
已经感谢任何愿意帮助我的人!
我认为您对代码的设计有点过度。您需要做的就是过滤传入事件,然后调用您的函数。请参阅下面的示例。
dialog.js(节点机器人示例): 发送一个简单的事件作为瀑布步骤。我将过滤的 'name' 属性 分配给 channelData
.
async eventStep ( stepContext ) {
let reply = { type: ActivityTypes.Event };
reply.channelData = { name: 'getLocation' };
await stepContext.context.sendActivities( reply );
return await stepContext.next();
}
createStore(): 我过滤传入活动的 event
类型。如果 channelData
属性 包含具有正确值的 name
,则调用 showPosition
函数。
const store = window.WebChat.createStore( {}, ( { dispatch } ) => next => async action => {
if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
const { activity } = action.payload;
if ( activity.type === 'event' ) {
// Handle based on event type
if ( activity.channelData.name === 'getLocation' ) {
if ( navigator.geolocation ) {
await navigator.geolocation.getCurrentPosition( showPosition );
}
else {
console.log( "Geolocation is not supported by this browser." );
}
}
}
}
return next(action);
});
showPosition(): 我将函数定位在网络聊天呈现器之后。我还演示了发送 SEND_MESSAGE
、SEND_MESSAGE_BACK
、SEND_POST_BACK
和 SEND_EVENT
。 activity 中数据的排列方式在每种情况下都不同。见下文。
[...]
document.getElementById('BotChatGoesHere')
);
function showPosition( position ) {
const message = "Latitude: " + position.coords.latitude + " Longitude: " + position.coords.longitude;
store.dispatch( {
type: 'WEB_CHAT/SEND_MESSAGE',
payload:
{
text: message,
channelData: { latitude: position.coords.latitude, longitude: position.coords.longitude }
}
} );
store.dispatch( {
type: 'WEB_CHAT/SEND_MESSAGE_BACK',
payload:
{
text: message,
value: { latitude: position.coords.latitude, longitude: position.coords.longitude }
}
} );
store.dispatch( {
type: 'WEB_CHAT/SEND_POST_BACK',
payload: { value: { latitude: position.coords.latitude, longitude: position.coords.longitude } }
} );
store.dispatch( {
type: 'WEB_CHAT/SEND_EVENT',
payload:
{
name: 'EVENT_GET_POSITION',
value: { latitude: position.coords.latitude, longitude: position.coords.longitude }
}
} );
}
SEND_MESSAGE
:作为消息发送;有文本字段;显示给用户; activity.channelData
中的数据
SEND_MESSAGE_BACK
:作为消息发送;有文本字段;不显示给用户; activity.value
中的数据
SEND_POST_BACK
:作为消息发送;没有文本字段;不显示给用户; activity.value
中的数据
SEND_EVENT
:作为事件发送;没有文本字段;不显示给用户; activity.name
中的事件名称和 activity.value
中的数据
希望得到帮助!
我需要帮助使用来自 Microsoft BotFramework 的 webchat v4 通过 Javascript.
将消息发送回机器人我的机器人代码是用 C# (.NET Framework) 编写的,但在我的场景中的某个时刻,我需要触发一些 javascript 来向用户询问位置。我该怎么办?
- 我发送一个 activity 类型的 事件
- 在机器人的 store 中,我捕获了所说的 activity(这是我的代码):
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')
);
这是我每次执行此操作时遇到的异常:
已经感谢任何愿意帮助我的人!
我认为您对代码的设计有点过度。您需要做的就是过滤传入事件,然后调用您的函数。请参阅下面的示例。
dialog.js(节点机器人示例): 发送一个简单的事件作为瀑布步骤。我将过滤的 'name' 属性 分配给 channelData
.
async eventStep ( stepContext ) {
let reply = { type: ActivityTypes.Event };
reply.channelData = { name: 'getLocation' };
await stepContext.context.sendActivities( reply );
return await stepContext.next();
}
createStore(): 我过滤传入活动的 event
类型。如果 channelData
属性 包含具有正确值的 name
,则调用 showPosition
函数。
const store = window.WebChat.createStore( {}, ( { dispatch } ) => next => async action => {
if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
const { activity } = action.payload;
if ( activity.type === 'event' ) {
// Handle based on event type
if ( activity.channelData.name === 'getLocation' ) {
if ( navigator.geolocation ) {
await navigator.geolocation.getCurrentPosition( showPosition );
}
else {
console.log( "Geolocation is not supported by this browser." );
}
}
}
}
return next(action);
});
showPosition(): 我将函数定位在网络聊天呈现器之后。我还演示了发送 SEND_MESSAGE
、SEND_MESSAGE_BACK
、SEND_POST_BACK
和 SEND_EVENT
。 activity 中数据的排列方式在每种情况下都不同。见下文。
[...]
document.getElementById('BotChatGoesHere')
);
function showPosition( position ) {
const message = "Latitude: " + position.coords.latitude + " Longitude: " + position.coords.longitude;
store.dispatch( {
type: 'WEB_CHAT/SEND_MESSAGE',
payload:
{
text: message,
channelData: { latitude: position.coords.latitude, longitude: position.coords.longitude }
}
} );
store.dispatch( {
type: 'WEB_CHAT/SEND_MESSAGE_BACK',
payload:
{
text: message,
value: { latitude: position.coords.latitude, longitude: position.coords.longitude }
}
} );
store.dispatch( {
type: 'WEB_CHAT/SEND_POST_BACK',
payload: { value: { latitude: position.coords.latitude, longitude: position.coords.longitude } }
} );
store.dispatch( {
type: 'WEB_CHAT/SEND_EVENT',
payload:
{
name: 'EVENT_GET_POSITION',
value: { latitude: position.coords.latitude, longitude: position.coords.longitude }
}
} );
}
SEND_MESSAGE
:作为消息发送;有文本字段;显示给用户; activity.channelData
SEND_MESSAGE_BACK
:作为消息发送;有文本字段;不显示给用户; activity.value
SEND_POST_BACK
:作为消息发送;没有文本字段;不显示给用户; activity.value
SEND_EVENT
:作为事件发送;没有文本字段;不显示给用户; activity.name
中的事件名称和 activity.value
希望得到帮助!