Search code examples
botframeworkdirect-line-botframeworkweb-chat

Method to selectively display IM messages in webchat with BotFramework v4?


I'm working on a voice-first bot using Microsoft botframework v4 and wanted to find a way where I can selectively display im messages in the transcript of my bot. I want to never display the user's messages and only display the bot's if there is something they need to visually confirm or a form they need to submit.

Is there a straightforward way to do this? Thank you.


Solution

  • You can use the activity middleware to return an empty component when the user messages the bot.

    Web Chat Code

    const activityMiddleware = () => next => card => {
      if (card.activity.from.role === 'user') {
        return () => {}
      }
      return next(card)
    };
    
    window.WebChat.renderWebChat({
        activityMiddleware,
        directLine: window.WebChat.createDirectLine({ token })
      },
      document.getElementById('webchat')
    );