Sha*_*ock 4 javascript web-chat botframework
我需要帮助使用来自 Microsoft 的 BotFramework 的 webchat v4 通过 Javascript 将消息发送回机器人。
我的机器人代码是用 C# (.NET Framework) 编写的,但在我的场景中的某个时刻,我需要触发一些 JavaScript 来向用户询问位置。我该怎么办?
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);
}
});
Run Code Online (Sandbox Code Playgroud)
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
}
});
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过“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')
);
Run Code Online (Sandbox Code Playgroud)
已经感谢任何愿意帮助我的人了!
我认为你对代码的设计有点过度。您需要做的就是过滤传入事件,然后调用您的函数。请参阅下面的示例。
dialog.js(Node bot 示例):以瀑布步骤发送简单事件。我将要过滤的“名称”属性被分配给channelData
。
async eventStep ( stepContext ) {
let reply = { type: ActivityTypes.Event };
reply.channelData = { name: 'getLocation' };
await stepContext.context.sendActivities( reply );
return await stepContext.next();
}
Run Code Online (Sandbox Code Playgroud)
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);
});
Run Code Online (Sandbox Code Playgroud)
showPosition():我将该函数定位在网络聊天渲染器之后。我还演示了发送SEND_MESSAGE
、SEND_MESSAGE_BACK
、SEND_POST_BACK
和SEND_EVENT
。数据在活动中的排列方式在每种情况下都不同。见下文。
[...]
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 }
}
} );
}
Run Code Online (Sandbox Code Playgroud)
SEND_MESSAGE
:作为消息发送;有文本字段;显示给用户;数据输入activity.channelData
SEND_MESSAGE_BACK
:作为消息发送;有文本字段;不向用户显示;数据输入activity.value
SEND_POST_BACK
:作为消息发送;没有文本字段;不向用户显示;数据输入activity.value
SEND_EVENT
:作为事件发送;没有文本字段;不向用户显示;事件名称输入activity.name
和数据输入activity.value
希望得到帮助!