Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
AnandChitale opened this issue Jan 24, 2017 · 23 comments
Closed

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

AnandChitale opened this issue Jan 24, 2017 · 23 comments
Assignees

Comments

@AnandChitale
Copy link

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.

@abhishan
Copy link

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

@AnandChitale
Copy link
Author

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

@AnandChitale
Copy link
Author

AnandChitale commented Jan 24, 2017

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;

@eanders-ms
Copy link
Contributor

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?

@AnandChitale
Copy link
Author

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
Copy link
Author

AnandChitale commented Feb 2, 2017

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
Copy link
Author

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

@Andrea-Orimoto
Copy link
Contributor

@AnandChitale, were you able to figure this out?

@AnandChitale
Copy link
Author

AnandChitale commented Feb 15, 2017

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..

@eanders-ms
Copy link
Contributor

@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.

@AnandChitale
Copy link
Author

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

@AnithaJothi
Copy link

AnithaJothi commented Mar 1, 2017

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!

@AnandChitale
Copy link
Author

@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.

@rohantare
Copy link

rohantare commented Mar 21, 2017

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
Copy link

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?

@nwhitmont
Copy link
Contributor

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

@AnandChitale
Copy link
Author

AnandChitale commented May 25, 2017 via email

@nwhitmont nwhitmont moved this from To Do to Done in Open Issues - .NET SDK May 26, 2017
@chetanmekha
Copy link

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.

@EricDahlvang
Copy link
Member

@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."));
            }
        });
    }
});

@sahar922
Copy link

@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.

@EricDahlvang
Copy link
Member

@sahar922 When the page is refreshed, a new conversation is initiated. If you want to sign users in before starting a conversation, you would need to write a client that handles that logic.

@sahar922
Copy link

@EricDahlvang Do you have any example? documentation to read?

Thanks!

@EricDahlvang
Copy link
Member

@sahar922 I do not have an example demonstrating the fact that a new conversation is initiated when the page is refreshed. You can hit F12 in your browser, on a page using the WebChat control, and look at network activity. The WebChat control https://github.com/Microsoft/BotFramework-WebChat uses DirectLineJs https://github.com/Microsoft/BotFramework-WebChat . If you follow what occurs when calling BotChat.App, you'll see that a new conversation is initiated for the supplied userid.

If you wish to discuss this further, please open a new issue or ask a question on Stack Overflow. Issue 2098 is currently closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests