microsoft / BotBuilder-Samples

Welcome to the Bot Framework samples repository. Here you will find task-focused samples in C#, JavaScript/TypeScript, and Python to help you get started with the Bot Framework SDK!
https://github.com/Microsoft/botframework
MIT License
4.37k stars 4.88k forks source link

CoreBot doesn't render welcome card in webchat #1589

Closed gabog closed 5 years ago

gabog commented 5 years ago

When you start CoreBot in the emulator, the bot renders the welcome card when is open.

if you host the bot in webchat, the welcome card won't render and you need to type hi or something else for the card to show.

tdurnford commented 5 years ago

Per @cwhitten

The Bot Framework protocol sends two ConversationUpdates, one for the Bot when it enters the conversation and one for the User after they have sent their first message. This is not governed by WebChat, but built-in to the protocol events themselves itself. As a group we are discussing how to change this behavior for a better user experience, but this is what we have in place today.

https://github.com/microsoft/BotFramework-WebChat/issues/2049

If we want the welcome message to send before the user messages Core Bot in Web Chat, we would have to configure Web Chat to send a back channel event to the bot when the DirectLine Connection is fulfilled to trigger the message instead like the Web Chat Welcome Message sample.

gabog commented 5 years ago

That is pretty obscure, do we need to add that to the CoreBot sample to help developers discover this nuance? Or document it in the readme for CoreBot? Or document this in https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-send-welcome-message?view=azure-bot-service-4.0&tabs=csharp?

As it is, I don't think devs will be able to find the issue above in GitHub and learn about this.

mutanttech commented 5 years ago

@gabog , were you able to fix the welcome message issue in the Core Bot (v4). I am pulling my hairs out for the same and nothing works including referring to this article - https://blog.botframework.com/2018/07/12/how-to-properly-send-a-greeting-message-and-common-issues-from-customers/

The Web Chat (UI) version that I use is BotFramework-WebChat-0.11.4, I have done certain customizations in it to implement facebook Like/Unlike feature with comment. Please revert if you have found a solution for this.

tdurnford commented 5 years ago

@mutanttech It looks like you asked this question on StackOverflow. I will be sure to help you with your issue there. In the meantime, I would recommend taking a look at https://github.com/microsoft/BotFramework-DirectLineJS/issues/184#issuecomment-505162246 to see how to send a Welcome Message using DirectLineJs.

tdurnford commented 5 years ago

@gabog

The Direct Line Connector Service has been updated to send one conversation update event with both the bot id and the user id in the activity's membersAdded property if the token used to start the conversation was generated with a user id. Otherwise, the second conversation update for the user is still sent after the user messages the bot the first time.

In the example below, the user id is added to the token request and the welcome message is sent from the onMembersAdded handler.

Web Chat

(async function () {
  // Note, for the simplicity of this example, we are generating the Direct Line token on client side;
  // however, this is not a recommended practice and you should create and manage your tokens from the server. 
  // You should never put the Direct Line secret in the browser or client app.
  // https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-authentication
  const secret = '<DIRECT_LINE_SECRET>';
  const res = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', { 
    method: 'POST',
    headers: {
      Authorization: `Bearer ${secret}`,
      'Content-type': 'application/json'
    },
    body: JSON.stringify({ user: { id: 'dl_user_id', name: 'username' }})
  });
  const { token } = await res.json();

  window.WebChat.renderWebChat({
    directLine: window.WebChat.createDirectLine({ token })
  }, document.getElementById('webchat'));

  document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));

Bot - Node SDK v4

this.onMembersAdded(async (context, next) => {
    const { membersAdded } = context.activity;

    for (let member of membersAdded) {
        if (member.id !== context.activity.recipient.id)
            await context.sendActivity("Welcome Message from `onMembersAdded` handler!");
    }
    await next();
});

Screen Capture

screengif

mutanttech commented 5 years ago

@tdurnford, I read your comment today. Is this applicable for v3 as well. And does this mean the changes we have done for sending a backchannel event and handling the event activity can be simplified?

tdurnford commented 5 years ago

@mutanttech Yes, this should also apply to Web Chat v3. We're working on putting together better documentation for Web Chat Welcome Messages.

gabog commented 5 years ago

Hi @tdurnford, testing this right now, will let you know how it goes soon but preliminary tests look promising.

gabog commented 5 years ago

Hi again @tdurnford, I tested the webchat script in my code and conversation update works as expected now. thanks for the help.

Gabo