BotFramework-WebChat icon indicating copy to clipboard operation
BotFramework-WebChat copied to clipboard

Change user avatar dynamically with out reload of webchat using bot.js and not react component

Open Kumar3010 opened this issue 7 years ago • 4 comments

As a user i need to change the user avatar dynamically with out reloading the web chat component. the web chat has to be loaded not using react component. currently we can set the botavatar and useravatar in styleoptions and provide these are params to the web chat component. once the chat component gets loaded there is no way to change the avtars. we would like to change avatar with out reload of chat component.

Kumar3010 avatar Nov 26 '19 04:11 Kumar3010

Hi @Kumar3010, Please see our customize the Web Chat UI docs. Recomposing the UI is unfortunately not supported in non-React Web Chat.

Please take future implementation questions to Stack Overflow. Thank you!

corinagum avatar Nov 26 '19 17:11 corinagum

@Kumar3010, this is possible as a hack/work-around, but it does come with a couple styling caveats. First, there is a visible, though short, delay in the user avatar updating. The amount of delay is approximate to the amount of time it takes for the user's message to post. When the message posts, then the image is displayed. Second, because of the delay, the space the avatar is set to occupy is empty potentially appearing as an indentation styling issue until the avatar fully renders.

To implement, first set userAvatarInitials to any value. You won't use the initials except for forcing Web Chat to render the "avatar" element. Also, remove the userAvatarImage property completely from styleOptions. If this property is not removed, then we have to force our pure CSS to override this.

You'll need to add some pure CSS. The url() option should contain an empty string. This adds in the attribute to the element which we'll access later when updating. Additionally, you'll want to set the color and background-color to "transparent". If you don't set these to "transparent", then those properties will be visible to the user until the avatar updates. By setting to "transparent", the user will only see the web chat background.

.from-user.avatar {
  background-image: url('');
  background-position: center;
  background-color: transparent;
  color: transparent;
}

Next, you'll want to dispatch a window event and catch it with an eventListener. When the event is detected, you'll filter the elements looking only for the user's avatar. When it is found, then the background-image attribute will be updated to the user's photo. I have the photo hard-coded in this example. You will need to pass that value in once the user is recognized.

<script>
  (async function () {
    [...]
      const eventUpdateAvatar = new Event( 'updateAvatar' );
      window.dispatchEvent(eventUpdateAvatar)

      window.addEventListener( 'updateAvatar', ( { data } ) => {
        const userPhoto = 'https://botberg81bd.blob.core.windows.net/images/image_32.png?st=2019-11-26T17%3A27%3A31Z&se=2019-11-27T17%3A27%3A31Z&sp=rl&sv=2018-03-28&sr=b&sig=278hyYIkQvJEhjxh8Wa0fMwC0KMmV%2Fv0MNH8u%2F8XZ5s%3D';
        const children = document.body.getElementsByClassName( 'from-user avatar' )
        const avatars = Object.values( children );
        for ( let avatar of avatars ) {
          avatar.setAttribute('style', `background-image: url(${userPhoto})`)
        }
      } );
  } );
</script>

Demo:

update-user-avatar

Hope of help!

stevkan avatar Nov 26 '19 23:11 stevkan

@stevkan this helps. thanks a ton

Kumar3010 avatar Jan 06 '20 03:01 Kumar3010

Our Front-End is in Vue, and we ended up making the avatar dynamic by using CSS variables.

We have an updateAvatar method which runs when the prop getAvatarFullUrl prop is changed.

updateAvatar() {
      document.documentElement.style.setProperty(
        "--userAvatar",
        `url(${this.getAvatarFullUrl})`
      );

which programatically updates CSS of

:root {
  --userAvatar: "";
}

.webchat__defaultAvatar--fromUser {
  background-position: center;
  background-color: transparent;
  color: transparent;
  background-size: cover;
  background-image: var(--userAvatar);
}

Casey6624 avatar Nov 30 '20 15:11 Casey6624