【问题标题】:BotFramework Webchat v4 Sending message back through the javascriptBotFramework Webchat v4 通过 javascript 发回消息
【发布时间】:2020-01-30 13:57:51
【问题描述】:

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

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

  1. 我发送事件类型的活动
  2. 在机器人的 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);
        }
    });
  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
        }
    });
}

我也尝试过使用“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


【解决方案1】:

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

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();
}

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。在每种情况下,数据在活动中的排列方式是不同的。见下文。

  [...]

  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 中的数据

希望有帮助!

【讨论】:

  • 感谢您的帮助和很长的回复。我的问题是商店中缺少 await 和 async 来显示位置,然后将 next(action) 放在末尾而不是在特定情况下.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多