Srichakradhar
Srichakradhar

Reputation: 1625

How to create and send a backchannel event with every message in bot framework?

I'm trying to access a session variable from the botbuilder middleware in send hook:

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

But since session is not available in send, how can I access the userData?

createEvent simply creates a backchannel event:

//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;
}

I found this answer on stackoverflow:

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

    next();
}

But, when I try to create an event and send it, I'm not able to access the address

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'] = {};
            }
        });

both session.message and event.address are undefined inside the callback function. How can I possibly do a workaround?

event has following content:

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' }

whereas outside the loadSessionWithoutDispatching function it has:

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\'.' }

I've used bot.loadSession instead of loadSessionWithoutDispatching and it works fine.

Upvotes: 3

Views: 442

Answers (1)

Srichakradhar
Srichakradhar

Reputation: 1625

I've used bot.loadSession instead of loadSessionWithoutDispatching and it works fine.

Upvotes: 4

Related Questions