botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

How to greet on Web Chat i.e Greet message from Bot to User

Open AnandChitale opened this issue 9 years ago • 23 comments

Deployed a bot to Azure. Enabled Web Chat Channel. Embed it to a iFrame on a website. Question: Ho greet w to get greet message from bot to user as in attached file.

AnandChitale avatar Jan 24 '17 08:01 AnandChitale

We are using conversationUpdated activity to send a message to the user. Check out our demo bot here http://alboteinstein.azurewebsites.net/index.html

CC: @kothakapu

abhishan avatar Jan 24 '17 09:01 abhishan

ConversationUpdate does not seem to work for me in web chat. It used to work few days back , suddenly stoped working.

AnandChitale avatar Jan 24 '17 09:01 AnandChitale

Below is the code:

case ActivityTypes.ConversationUpdate:
                            IConversationUpdateActivity update = activity;
                            using (var scope = Microsoft.Bot.Builder.Dialogs.Internals.DialogModule.BeginLifetimeScope(Conversation.Container, activity))
                            {
                                var client = scope.Resolve<IConnectorClient>();
                                if (update.MembersAdded.Any())
                                {
                                    var reply = activity.CreateReply();
                                    foreach (var newMember in update.MembersAdded)
                                    {
                                        if (newMember.Id != activity.Recipient.Id)
                                        {
                                            string message = "";
                                            message += $"Hi,\n";                                            
                                            reply.Text = message;                                           
                                            await client.Conversations.ReplyToActivityAsync(reply);
                                        } 
                                    }
                                }
                            }
                            break;

AnandChitale avatar Jan 24 '17 09:01 AnandChitale

Hi @AnandChitale, with a few small changes I was able to get your code working in my testbot (Microsoft.Bot.Builder v3.5.0):

case ActivityTypes.ConversationUpdate:
    IConversationUpdateActivity update = activity;
    using (var scope = Microsoft.Bot.Builder.Dialogs.Internals.DialogModule.BeginLifetimeScope(Conversation.Container, activity))
    {
        var client = scope.Resolve<IConnectorClient>();
        if (update.MembersAdded.Any())
        {
            foreach (var newMember in update.MembersAdded)
            {
                if (newMember.Id != activity.Recipient.Id)
                {
                    var reply = activity.CreateReply();
                    reply.Text = $"Welcome {newMember.Name}!";
                    await client.Conversations.ReplyToActivityAsync(reply);
                }
            }
        }
    }
    break;

For some reason I was having a problem compiling var client = scope.Resolve().

Does the above code work for you? Is your case ActivityTypes.ConversationUpdate: handler getting called at all?

eanders-ms avatar Jan 25 '17 02:01 eanders-ms

Yes it used to work before. Suddenly stopped working i.e Bot was not able to greet user on conversation update. But when user types Hi ,then bot replied with greet message.

AnandChitale avatar Jan 30 '17 14:01 AnandChitale

Upgraded to V3.5.1 and using new emulator , getting the greet message in Emulator only . Web Chat control does not get greet message.

Through emulator if I connect bot hosted on azure , greet message comes through bot to emulator as conversation update. issue20198

But after enabling the Web Chat Channel and using the URL in browser , bot does not start the conversation with user through greet message in Web control.

AnandChitale avatar Feb 02 '17 08:02 AnandChitale

@abhishan Could you please share if how to have greet message using Web Chat.

AnandChitale avatar Feb 02 '17 13:02 AnandChitale

@AnandChitale, were you able to figure this out?

Andrea-Orimoto avatar Feb 14 '17 22:02 Andrea-Orimoto

No still facing the same issue.. Bot does not greet when web chat link in opened in any browser. only when user say hi , Bot greets.. @Andrea-Orimoto do you have any solution..

AnandChitale avatar Feb 15 '17 08:02 AnandChitale

@AnandChitale Your screenshot above shows the old WebChat control. Are you using, or have you tried using, the new Web Chat control? You must opt in to it on the WebChat channel's config page.

It might be that ConversationUpdate messages are not working in the old control. I will look into this, but in the meantime you might try using the new WebChat. It has better support for more media types and other improvements.

eanders-ms avatar Feb 15 '17 18:02 eanders-ms

Yes the ConversationUpdate messages are not working in my case for web chat neither in directline.

AnandChitale avatar Feb 16 '17 07:02 AnandChitale

Hello, Any luck on the above yet? I tried using the new web chat control as well as a customized version built using the open source web chat. I am trying to display a welcome card (Hero card). This works fine in the emulator however doesnt work when published - neither in the classic web chat control nor in my customized version configured using direct line. The conversation update doesnt get called for me as soon as the bot gets launched. Instead I see the welcome card getting displayed after the user types in some text. Not sure if i am missing something. My code snippet below

else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
IConversationUpdateActivity update = message;
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
var client = scope.Resolve();
ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
if (update.MembersAdded.Any())
{
var replyNow = message.CreateReply();
foreach (var newMember in update.MembersAdded)
{
if (newMember.Id != message.Recipient.Id)
{
List cardImages = new List();
string strCurrentURL = this.Url.Request.RequestUri.AbsoluteUri.Replace(@"/api/messages", "");
string imageURL = String.Format(@"{0}/{1}", strCurrentURL, "Images/icon_Hamilton_1.png");
cardImages.Add(new CardImage(url: imageURL));
string subtitle = "";
subtitle = @"Welcome!";
HeroCard plCard = new HeroCard()
{
Title = "HHHHH",
Subtitle = subtitle,
Images = cardImages,
Buttons = null
};
Attachment plAttachment = plCard.ToAttachment();
List alist = new List();
alist.Add(plAttachment);
replyNow.Attachments = alist;
connector.Conversations.ReplyToActivityAsync(replyNow);
}
}
}
}
}

I seem to be kind of stuck here and any help or debugging tips are much appreciated please!

AnithaJothi avatar Mar 01 '17 12:03 AnithaJothi

@AnithaJothi . I am still facing this issue both on web chat and directline.

For directline the workaround is to send a private message to bot and handle the response as greet. Do not show private message on the UI.

On Web Chat , Yes the bot responds only when user inputs something.

AnandChitale avatar Mar 02 '17 06:03 AnandChitale

I am using WebChat control, hosted in website. ConversationUpdate() gets fired twice. So I always greet user twice, which is so annoying. Can any one tell me , how to fix this?

Code snippet:

    private async Task<Activity> HandleSystemMessage(Activity message)
    {
        if (message.Type == ActivityTypes.DeleteUserData)
        {
            // Implement user deletion here
            // If we handle user deletion, return a real message
        }
        else if (message.Type == ActivityTypes.ConversationUpdate)
         {

            ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
            Activity reply = message.CreateReply($"Hey I am a chatbot");
            await connector.Conversations.ReplyToActivityAsync(reply);
         }
       }

rohantare avatar Mar 21 '17 10:03 rohantare

Plus I am trying to trigger a dialog right after my greeting message, But I have to force user to type something, then only Post would get called. Is there any workaround for this?

rohantare avatar Mar 21 '17 10:03 rohantare

@AnandChitale Are you still experiencing the issue after upgrading to BotBuilder 3.8.1?

nwhitmont avatar May 25 '17 00:05 nwhitmont

@Nils I am not facing this issue now. Thanks!

On Thu, May 25, 2017 at 5:33 AM, Nils [email protected] wrote:

@AnandChitale https://github.com/anandchitale Are you still experiencing the issue after upgrading to BotBuilder 3.8.1 https://www.nuget.org/packages/Microsoft.Bot.Builder/?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Microsoft/BotBuilder/issues/2098#issuecomment-303886010, or mute the thread https://github.com/notifications/unsubscribe-auth/AXK431yysKmA39gjgVcaFGYAA2Gdet-Mks5r9MU8gaJpZM4Lr_JH .

AnandChitale avatar May 25 '17 12:05 AnandChitale

Hello Guys, I am facing the same issue in node js version. I have latest version of webchat. My scenario is,

  1. When user add bot in browser then welcome msg come with choice option
  2. When user select or type anything then again conversationUpdate get's called and welcome message appears
  3. After selecting choice option second time bot start working normally. Please help me out over this. I am using node version, botbuilder version 3.8.3.

chetanmekha avatar May 29 '17 11:05 chetanmekha

@rohantare The ConversationUpdate activity is sent for each member added: once for the user, and once for the bot. Filtering on .Recipient.Id enables replying just once:

else if (message.GetActivityType() == ActivityTypes.ConversationUpdate)
{
	IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
	if (iConversationUpdated != null)
	{
		ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));

		foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
		{
			// if the bot is added, then 
			if (member.Id == iConversationUpdated.Recipient.Id)
			{
				var reply = ((Activity)iConversationUpdated).CreateReply($"Hey I am a chatbot");                            
				await connector.Conversations.ReplyToActivityAsync(reply);
			}
		}
	}
}

@chetanmekha Similarly, in node:


bot.on('conversationUpdate', function (message) {
    if (message.membersAdded) {
        message.membersAdded.forEach(function (identity) {
            if (identity.id === message.address.bot.id) {
                bot.send(new builder.Message()
                    .address(message.address)
                    .text("Hello!  I'm a bot."));
            }
        });
    }
});

EricDahlvang avatar May 29 '17 20:05 EricDahlvang

@EricDahlvang your solution works for me (NodeJS) but the welcome message shows every refresh of the page and not just the first time the user sends a message.

sahar922 avatar Sep 24 '17 14:09 sahar922