BotFramework Webchat v4 通过 javascript 发回消息

Sha*_*ock 4 javascript web-chat botframework

我需要帮助使用来自 Microsoft 的 BotFramework 的 webchat v4 通过 Javascript 将消息发送回机器人。

我的机器人代码是用 C# (.NET Framework) 编写的,但在我的场景中的某个时刻,我需要触发一些 JavaScript 来向用户询问位置。我该怎么办?

  1. 我发送事件类型的活动
  2. 在机器人的商店中,我捕获了上述活动(这是我的代码):
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)
  1. 我将信息发送给机器人(此处为位置):
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)

这是我在执行此操作时遇到的异常: 控制台错误 活动发送

已经感谢任何愿意帮助我的人了!

Ste*_*erg 6

我认为你对代码的设计有点过度。您需要做的就是过滤传入事件,然后调用您的函数。请参阅下面的示例。

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_MESSAGESEND_MESSAGE_BACKSEND_POST_BACKSEND_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

在此输入图像描述

希望得到帮助!