I had created a bot with v3 (C#) SDK and the welcome message used to work just fine without any sweat. And it still does for me in production. The code is handled in HandleSystemMessage like this -
.. v3 Code additional code removed for clarity...
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
//Code to show Welcome Message
if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))
{
var reply = message.CreateReply();
reply.Attachments = new List<Attachment>();
// Create the attachment.
Attachment attachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = AdaptiveCardHelper.GetOptionsCard()
};
reply.Attachments.Add(attachment);
ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
await connector.Conversations.ReplyToActivityAsync(reply);
}
}
The Web Chat 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.
Now I am migrating the bots to v4 SDK (C# + .Net Core Web App), and I am intending to use the same old version of webchat. But I am struggling for two days to get a welcome message displayed in the same web chat while it works well on emulator (given to that two ConversationUpdate) events.
I have tried sending a message as well as an event using the solution provided in this article and tried to catch that in Bot on different async methods OnEventAsync, OnEventActivityAsync, OnMessageActivityAsync.
https://blog.botframework.com/2018/07/12/how-to-properly-send-a-greeting-message-and-common-issues-from-customers/
V4 Code looks like below:
protected override async Task OnConversationUpdateActivityAsync(ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
if (turnContext.Activity.MembersAdded != null)
{
if (turnContext.Activity.MembersAdded.Any(m => m.Id != turnContext.Activity.Recipient?.Id))
{
//var welcomeCard = CreateAdaptiveCardAttachment();
//var response = CreateResponse(turnContext.Activity, welcomeCard);
//await turnContext.SendActivityAsync(response, cancellationToken);
await Utility.LogTraceAsync("Inside OnConversationUpdateActivityAsync");
var eventActivity = turnContext.Activity.AsConversationUpdateActivity();
ConnectorClient connector = new ConnectorClient(new Uri(eventActivity.ServiceUrl), Configuration.MicrosoftAppId, Configuration.MicrosoftAppPassword);
await Utility.LogTraceAsync("Service URL OnConversationUpdateActivityAsync" + eventActivity.ServiceUrl);
await Utility.LogTraceAsync("Recipient ID OnConversationUpdateActivityAsync" + turnContext.Activity.Recipient?.Id);
var welcomeCard = CreateAdaptiveCardAttachment();
var reply = ((Activity)eventActivity).CreateReply();
reply.Attachments.Add(welcomeCard);
//var response = CreateResponse(turnContext.Activity, welcomeCard);
await connector.Conversations.ReplyToActivityAsync(reply, cancellationToken);// turnContext.SendActivityAsync(response, cancellationToken);
await Utility.LogTraceAsync("OnConversationUpdateActivityAsync Response Returned.");
await Utility.LogTraceAsync("Exit OnConversationUpdateActivityAsync");
}
}
}
protected override async Task OnEventActivityAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
await Utility.LogTraceAsync("Inside OnEventActivityAsync");
if (turnContext.Activity.Type == ActivityTypes.Event)
{
var eventActivity = turnContext.Activity.AsEventActivity();
await Utility.LogTraceAsync("Event Activity from WebChat matched.");
ConnectorClient connector = new ConnectorClient(new Uri(eventActivity.ServiceUrl), Configuration.MicrosoftAppId, Configuration.MicrosoftAppPassword);
await Utility.LogTraceAsync("Service URL " + eventActivity.ServiceUrl);
var welcomeCard = CreateAdaptiveCardAttachment();
var reply = ((Activity)eventActivity).CreateReply();
reply.Attachments.Add(welcomeCard);
var members = await connector.Conversations.GetConversationMembersAsync(eventActivity.Conversation.Id.ToString());
var membernames = "";
foreach (var member in members) {
membernames += member.Name + ",";
}
await Utility.LogTraceAsync(membernames);
await connector.Conversations.SendToConversationAsync(reply, cancellationToken);
await connector.Conversations.ReplyToActivityAsync(reply, cancellationToken);// turnContext.SendActivityAsync(response, cancellationToken);
await Utility.LogTraceAsync("Event Response Returned.");
}
await Utility.LogTraceAsync("Exit OnEventActivityAsync");
}
But it does not seem to work at all. I am pulling my hairs out and there is no clue on how to do for .Net Core App. I will be glad to know if someone has solved this problem.
Update - I have used the JS code on client-side as provided by @tdurnford and at Bot Side had following two methods -
//Required to Show Welcome Message on Emulator
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded ?? Array.Empty<ChannelAccount>())
{
// Greet anyone that was not the target (recipient) of this message.
// To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
if (member.Id != turnContext.Activity.Recipient.Id)
{
Activity reply = ((Activity)turnContext.Activity).CreateReply();
AdaptiveCard card = AdaptiveCardHelper.GetWelcomeCard();
Attachment attachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
reply.Attachments.Add(attachment);
await turnContext.SendActivityAsync(reply, cancellationToken);
}
}
}
//Required to Show Welcome Message on Web Chat
protected override async Task OnEventActivityAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
if (turnContext.Activity.Name == "webchat/join")
{
Activity reply = ((Activity)turnContext.Activity).CreateReply();
AdaptiveCard card = AdaptiveCardHelper.GetWelcomeCard();
Attachment attachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
reply.Attachments.Add(attachment);
await turnContext.SendActivityAsync(reply, cancellationToken);
}
}
With both methods two welcome messages are shown in the chat window -
Bot Window with two Welcome Messages
Then I've commented the OnEventActivityAsync method in C# and deployed again. Now it shows only one welcome message returned from OnMembersAddedAsync as shown in the window.
Bot Window with only one Welcome Message
If I comment the following code lines in the webchat code i.e. don't send the post-activity -
botConnection.postActivity({
from: {
id: 'myUserId',
name: 'myUserName'
},
type: 'event',
name: 'webchat/join',
value: {
locale: 'en-US'
}
}).subscribe(
id => console.log("Posted welcome event, assigned ID ", id),
error => console.log("Error posting activity", error)
);
In this case, no welcome message is displayed. @tdurnford, please check if you are able to replicate this behavior.
Although there is another problem here in this way that when a user types a question in the bot, then a welcome message is displayed again.
Bot window with two welcome messages one on load and another after the first question
See Question&Answers more detail:
os