如何在Bot框架中创建和发送包含每条消息的反向通道事件?

时间:2018-06-22 05:59:52

标签: node.js hook botframework

我正在尝试通过send挂钩中的botbuilder中间件访问会话变量:

bot.use({
    botbuilder: function (session, next) {
        session.send(); // it doesn't work without this..
        session.sendTyping();
        console.log('session.userData', session.userData['UI_Changes']);
        var reply = createEvent("UI_Changes", session.userData['UI_Changes'], session.message.address);
        session.send(reply);
        // session.userData['UI_Changes'] = {};
        next();
    },
    send: function (event, next) {
        // console.log('session.userData', session.userData['UI_Changes']);
        // var reply = createEvent("UI_Changes", session.userData['UI_Changes'], session.message.address);
        // session.send(reply);
        // session.userData['UI_Changes'] = {};
        next();
    }
});

但是由于session中没有send,如何访问userData

createEvent只需创建一个backchannel事件:

//Creates a backchannel event
const createEvent = (eventName, value, address) => {
    var msg = new builder.Message().address(address);
    msg.data.type = "event";
    msg.data.name = eventName;
    msg.data.value = value;
    return msg;
}

我在stackoverflow上找到了这个答案:

send: function (message, next) {
    bot.loadSessionWithoutDispatching(message.address,function (error,session){
        console.log(session.userData);
    });

    next();
}

但是,当我尝试创建一个事件并将其发送时,我无法访问该地址

bot.loadSessionWithoutDispatching(event.address, function (error,session){
            console.log('session not null?', session !== null ? "yes" : "no");
            if(session !== null){
                console.log('session.userData', session.userData['UI_Changes'], 'address:', session);
                var reply = createEvent("UI_Changes", session.userData['UI_Changes'], event.address); //undefined
                session.send(reply);
                session.userData['UI_Changes'] = {};
            }
        });

session.messageevent.address都位于回调函数中的undefined中。我该如何解决?

event具有以下内容:

event: { type: 'message',
  text: 'You reached the Greeting intent. You said \'hi\'.',
  locale: 'en-US',
  localTimestamp: '2018-06-21T14:37:12.684Z',
  from: { id: 'Steves@4MRN9VFFpAk', name: 'Steves' },
  recipient: { id: 'pruthvi', name: 'pruthvi' },
  inputHint: 'acceptingInput' }

loadSessionWithoutDispatching函数之外,它具有:

event outside { type: 'message',
  agent: 'botbuilder',
  source: 'directline',
  textLocale: 'en-US',
  address: 
   { id: 'A7nrBS4yINt2607QtKpxOP|0000048',
     channelId: 'directline',
     user: { id: 'pruthvi', name: 'pruthvi' },
     conversation: { id: 'A7nrBS4yINt2607QtKpxOP' },
     bot: { id: 'Steves@4MRN9VFFpAk', name: 'Steves' },
     serviceUrl: 'https://directline.botframework.com/' },
  text: 'You reached the Greeting intent. You said \'hi\'.' }

我用bot.loadSession代替了loadSessionWithoutDispatching,它工作正常。

1 个答案:

答案 0 :(得分:4)

我用bot.loadSession代替了loadSessionWithoutDispatching,它工作正常。