Search code examples
node.jsbotframework

How to intercept bot messages in SDK4


I have an SDK 4 bot, logging user interactions to Blob storage. I would also like to log bot responses also. In SDK 3 I did this with something like ...

bot.use({
    // Code for handling message receives
    receive: function (session, event, next) {
        var userId = session.address.user.id;
        logger.logUserConversation(session.text, userId, session.address.conversation.id, userId);
        next();
    },
    // Code for handling message sends
    send: function (event, next) {
        var text = event.text;    
        if(!event.text) {
            text = "Attachments sent";
        }
        logger.logUserConversation(text, 'bot', event.address.conversation.id, event.address.user.id);
        next();
    }
});

In SDK 4, I am able to configure middleware which intercepts user activity, but I cannot seem to intercept bot activity. I can't seem to find anything in the documentation, but I am new to SDK 4 and might be missing something.

Anybody know how I can intercept both user and bot events, so that I can log?


Solution

  • There are already 2 samples in Node.js in the official samples repository:

    I gave a try to the 1st one and can confirm that it logs both user input and bot replies. It is logging every activity, even ConversationUpdate.

    See example of transcript generated below:

     Activity Received: { type: 'conversationUpdate',
      membersAdded: [ { id: '1', name: 'Bot' } ],
      channelId: 'emulator',
      conversation: { id: '36e25420-ec19-11e8-8040-2ba105e71021|livechat' },
      id: '370f7ea0-ec19-11e8-9ee4-fb60855d29c5',
      localTimestamp: 2018-11-19T16:36:07.000Z,
      recipient: { id: '1', name: 'Bot', role: 'bot' },
      timestamp: 2018-11-19T16:36:07.689Z,
      from:
       { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6',
         name: 'User',
         role: 'user' },
      locale: '',
      serviceUrl: 'http://localhost:58083' } 
    
     Activity Received: { type: 'conversationUpdate',
      membersAdded:
       [ { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6', name: 'User' } ],
      channelId: 'emulator',
      conversation: { id: '36e25420-ec19-11e8-8040-2ba105e71021|livechat' },
      id: '3711c890-ec19-11e8-9ee4-fb60855d29c5',
      localTimestamp: 2018-11-19T16:36:07.000Z,
      recipient: { id: '1', name: 'Bot', role: 'bot' },
      timestamp: 2018-11-19T16:36:07.705Z,
      from:
       { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6',
         name: 'User',
         role: 'user' },
      locale: '',
      serviceUrl: 'http://localhost:58083' } 
    
     Activity Received: { text:
       'I am a bot that demonstrates custom logging. We will have a short conversation where I ask a few questions  to collect your name and age, then store those values in UserState for later use. after this you will be able to find a log of the conversation in the folder set by the transcriptsPath environment variable Say anything to continue.',
      inputHint: 'acceptingInput',
      channelId: 'emulator',
      serviceUrl: 'http://localhost:58083',
      conversation: { id: '36e25420-ec19-11e8-8040-2ba105e71021|livechat' },
      from: { id: '1', name: 'Bot', role: 'bot' },
      recipient:
       { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6',
         name: 'User',
         role: 'user' },
      replyToId: '3711c890-ec19-11e8-9ee4-fb60855d29c5',
      type: 'message',
      timestamp: 2018-11-19T16:36:08.408Z } 
    
     Activity Received: { type: 'message',
      text: 'test',
      from:
       { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6',
         name: 'User',
         role: 'user' },
      locale: '',
      textFormat: 'plain',
      timestamp: 2018-11-19T16:36:23.421Z,
      channelData: { clientActivityId: '1542645367574.7109285295569892.0' },
      entities:
       [ { type: 'ClientCapabilities',
           requiresBotState: true,
           supportsTts: true,
           supportsListening: true } ],
      channelId: 'emulator',
      conversation: { id: '36e25420-ec19-11e8-8040-2ba105e71021|livechat' },
      id: '406fdad0-ec19-11e8-9ee4-fb60855d29c5',
      localTimestamp: 2018-11-19T16:36:23.000Z,
      recipient: { id: '1', name: 'Bot', role: 'bot' },
      serviceUrl: 'http://localhost:58083' } 
    
     Activity Received: { text: 'What is your name, human?',
      inputHint: 'expectingInput',
      channelId: 'emulator',
      serviceUrl: 'http://localhost:58083',
      conversation: { id: '36e25420-ec19-11e8-8040-2ba105e71021|livechat' },
      from: { id: '1', name: 'Bot', role: 'bot' },
      recipient:
       { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6',
         name: 'User',
         role: 'user' },
      replyToId: '406fdad0-ec19-11e8-9ee4-fb60855d29c5',
      type: 'message',
      timestamp: 2018-11-19T16:36:23.443Z } 
    

    More details about the code that generated that is available on the project, here. If you look at it, the main point is:

    if (activity.value === 'endOfInput') {
        console.log(this.conversations[id]);
        var transcriptfileName = util.format('%s/log_%s.transcript', process.env.transcriptsPath, id);
        fs.writeFile(transcriptfileName, JSON.stringify(this.conversations[id], null, 3), function(err) {
            if (err) throw err;
        });
        delete this.conversations[id];
    }