繁体   English   中英

Botframework v4 Directline集成:有没有办法将通过Directline传输到Chatbot生成的对话ID(nodejs代码)

[英]Botframework v4 Directline integration: Is there a way to get the conversation id generated from directline transfer to the Chatbot (nodejs code)

这可能很简单,但是我找不到关于此的任何参考。 我使用直接API将聊天机器人集成到了网络应用中; 我正在使用此API生成对话ID:

POST: https : //directline.botframework.com/v3/directline/conversations

我正在尝试从上面的API(从Web应用程序代码)到聊天机器人代码(NodeJS)中获取生成的对话ID。 有没有办法做到这一点?

根据问题评论的一种方法是在开始对话之前使用以下命令将数据发送到bot:

var params = BotChat.queryParams(location.search);
var my_token = params['my_token'];

var botConnection = new BotChat.DirectLine({
    secret: 'DIRECTLINE_SECRET'
});

BotChat.App({
    botConnection: botConnection
    ,user: { id: 'USER_ID', name: 'User' }  // user.id auto updates after first user message
}, document.getElementById("bot"));

botConnection.connectionStatus$.subscribe(function (status) {
    if (status == 2) {  // wait for connection is 'OnLine' to send data to bot
        var convID = botConnection.conversationId;
        botConnection.postActivity({
            from: { id: convID }  // because first time user ID == conversation ID
            ,type: 'event'
            ,name: 'registerUserData'    // event name as we need
            ,value: my_token   // data attached to event
        }).subscribe(function (activityId) {
            // This subscription is a MUST
            // If I remove this handler the postActivity not reaches the bot
        });
    }
});

在这里,您订阅botConnection.connectionStatus $,并且当状态等于2时,您将从botConnection对象获取对话ID。

然后,您可以在机器人代码中添加此中间件代码以获取数据:

bot.use({ receive: function(event, next) {
    if (!!event && !!event.address && event.name == 'registerUserData') {
        var message = new builder.Message().address(event.address).text('my_token:' + event.value);
        bot.send(message, function (err) {});  // simulate proactive message to user
    }
    next();
} });

希望这可以帮助。

我使用Botframework Web聊天返回频道解决了此问题,这是参考链接: https : //github.com/Microsoft/BotFramework-WebChat

在我使用Directline API生成对话ID之后:POST: https ://directline.botframework.com/v3/directline/conversations

我通过反向通道将数据从Web应用程序发送到聊天机器人。

<div id="webchat"></div>
<script>
  (async function () {
    // We are using a customized store to add hooks to connect event
    const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
      if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
        // When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
        dispatch({
          type: 'WEB_CHAT/SEND_EVENT',
          payload: {
            name: 'webchat/join',
            value: { conversation_id: conversationID }
          }
        });
      }

      return next(action);
    });

    window.WebChat.renderWebChat({
      directLine: window.WebChat.createDirectLine({ token }),
      store
    }, document.getElementById('webchat'));

    document.querySelector('#webchat > *').focus();
  })().catch(err => console.error(err));

  //Note: conversationID and token is generated in the backend code of the web app.
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM