Search code examples
node.jsazurebotframeworkchatbot

Unable to Intercept Bot Response in Azure Bot Framework


I want to intercept all the messages exchanged between user and bot. PFB code that I have used to intercept. I am using Bot Diaolog framework for webchat. Here the issue is I am not able to extract values from activities object which contains messages sent from bot to user.

adapter.use(async (turnContext, next) => {
// pre-processing of the current incoming activity
console.log("adapture use::turnContext::" + turnContext.activity.text);


// hook up a handler to process any outgoing activities sent during this turn
turnContext.onSendActivities(async (sendContext, activities, nextSend) => {
    // pre-processing of outgoing activities
    await nextSend();
    console.log("adapture use::activities::POST##::" + flat.stringify(activities));
    // post-processing outgoing activities
});

await next();

// post-processing of the current incoming activity 
console.log(`Processing activity ${turnContext.activity.id} finishing. `);

});


Solution

  • To access the bot's responses, you can update the bot.js file (i.e. the file with the activity handlers defined) to something like the below.

    In short, the onSendActivities() method keeps an array of the activities that have been passed in. You can cycle thru the activities pushing them into an array and then acting on a particular one. Or, react as each arrives. Further below are examples of each output.

    const { ActivityHandler, MessageFactory } = require('botbuilder');
    
    class EchoBot extends ActivityHandler {
        constructor() {
            super();
    
            [...]
    
            const transcript = [];
            this.onTurn(async (context, next1) => {
                let responses = {};
    
                logActivity(transcript, cloneActivity(context.activity));
    
                context.onSendActivities(async (ctx, activities, next2) => {
                    responses = await next2();
                    activities.forEach((a) => logActivity(transcript, cloneActivity(a)));
                    console.log('TRANSCRIPT ', activities);
                    return responses;
                });
    
                await next1();
            });
        }
    }
    
    const logActivity = (transcript, activity) => {
        if (!activity.timestamp) {
            activity.timestamp = new Date();
        }
        transcript.push(activity);
    };
    
    const cloneActivity = (activity) => {
        return Object.assign({}, activity);
    };
    
    module.exports.EchoBot = EchoBot;
    
    

    Logging the transcript array: Shows the list of activities.

    [
      {
        type: 'conversationUpdate',
        id: '1hEUP37Da8S',
        timestamp: 2021-09-07T23:01:04.910Z,
        serviceUrl: 'https://directline.botframework.com/',
        channelId: 'directline',
        from: { id: 'dl_16310556645490.nc93iu9jr1' },
        conversation: { id: '5JgOxxxxxxxxxxxxv6sw-g' },
        recipient: { id: 'somebot@QaeuoeEamLg', name: 'Some Bot' },
        membersAdded: [ [Object], [Object] ],
        rawTimestamp: '2021-09-07T23:01:04.9109865Z',
        callerId: 'urn:botframework:azure'
      },
      {
        type: 'message',
        text: 'Hello and welcome!',
        inputHint: 'acceptingInput',
        speak: 'Hello and welcome!',
        channelId: 'directline',
        locale: undefined,
        serviceUrl: 'https://directline.botframework.com/',
        conversation: { id: '5JgOxxxxxxxxxxxxv6sw-g' },
        from: { id: 'somebot@QaeuoeEamLg', name: 'Some Bot' },
        recipient: { id: 'dl_16310556645490.nc93iu9jr1' },
        timestamp: 2021-09-07T23:01:06.547Z
      },
      {
        type: 'message',
        id: '5JgOxxxxxxxxxxxxv6sw-g|0000001',
        timestamp: 2021-09-07T23:01:08.704Z,
        localTimestamp: 2021-09-07T23:01:08.527Z,
        localTimezone: 'America/Los_Angeles',
        serviceUrl: 'https://directline.botframework.com/',
        channelId: 'directline',
        from: { id: 'dl_16310556645490.nc93iu9jr1', name: '' },
        conversation: { id: '5JgOxxxxxxxxxxxxv6sw-g' },
        recipient: { id: 'somebot@QaeuoeEamLg', name: 'Some Bot' },
        textFormat: 'plain',
        locale: 'en-US',
        text: 'Hi',
        entities: [ [Object] ],
        channelData: {
          clientActivityID: '1631055668527tzwhm47a4qd',
          clientTimestamp: '2021-09-07T23:01:08.527Z'
        },
        rawTimestamp: '2021-09-07T23:01:08.704318Z',
        rawLocalTimestamp: '2021-09-07T16:01:08.527-07:00',
        callerId: 'urn:botframework:azure'
      },
      {
        type: 'message',
        text: 'Echo: Hi',
        inputHint: 'acceptingInput',
        speak: 'Echo: Hi',
        channelId: 'directline',
        locale: 'en-US',
        serviceUrl: 'https://directline.botframework.com/',
        conversation: { id: '5JgOxxxxxxxxxxxxv6sw-g' },
        from: { id: 'somebot@QaeuoeEamLg', name: 'Some Bot' },
        recipient: { id: 'dl_16310556645490.nc93iu9jr1', name: '' },
        replyToId: '5JgOxxxxxxxxxxxxv6sw-g|0000001',
        timestamp: 2021-09-07T23:01:09.147Z
      }
    ]
    

    Logging activities only: Shows the last item to have passed thru from either the bot or user.

    [
      {
        type: 'message',
        text: 'Echo: Hi',
        inputHint: 'acceptingInput',
        speak: 'Echo: Hi',
        channelId: 'directline',
        locale: 'en-US',
        serviceUrl: 'https://directline.botframework.com/',
        conversation: { id: 'AURKxxxxxxxxxJHpO-f' },
        from: { id: 'somebot@QaeuoeEamLg', name: 'Some Bot' },
        recipient: { id: 'dl_16310605730140.3nrm4evq6um', name: '' },
        replyToId: 'AURKxxxxxxxxxJHpO-f|0000001'
      }
    ]