[Question] Trigger c# Function on click of Hero Card Button
Hello I want to trigger a C# function on click of button on Hero Card (or any other rich card). While I have read many blogs about the same not many explain how to do it. Can someone please explain and also provide the code snippet for C#. Thanks in advance.
@neetgupta7 for more information, see: https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-add-rich-card-attachments#process-events-within-rich-cards
Here's an example Hero Card with "Card Actions":
Activity replyToConversation = message.CreateReply("Should go to conversation, in carousel format");
replyToConversation.AttachmentLayout = AttachmentLayouts.Carousel;
replyToConversation.Attachments = new List<Attachment>();
Dictionary<string, string> cardContentList = new Dictionary<string, string>();
cardContentList.Add("PigLatin", "https://<ImageUrl1>");
cardContentList.Add("Pork Shoulder", "https://<ImageUrl2>");
cardContentList.Add("Bacon", "https://<ImageUrl3>");
foreach(KeyValuePair<string, string> cardContent in cardContentList)
{
List<CardImage> cardImages = new List<CardImage>();
cardImages.Add(new CardImage(url:cardContent.Value ));
List<CardAction> cardButtons = new List<CardAction>();
CardAction plButton = new CardAction()
{
Value = $"https://en.wikipedia.org/wiki/{cardContent.Key}",
Type = "openUrl",
Title = "WikiPedia Page"
};
cardButtons.Add(plButton);
HeroCard plCard = new HeroCard()
{
Title = $"I'm a hero card about {cardContent.Key}",
Subtitle = $"{cardContent.Key} Wikipedia Page",
Images = cardImages,
Buttons = cardButtons
};
Attachment plAttachment = plCard.ToAttachment();
replyToConversation.Attachments.Add(plAttachment);
}
var reply = await connector.Conversations.SendToConversationAsync(replyToConversation);
Thanks for the above. I have seen this standard piece which allows me to launch any url from the Hero card. What I am looking at is a functionality similar to PromptDialog.Choice where when a user chooses an option the control is passed on to a specific C# function. And I want a similar functionality with a Hero Card as it has a richer UI.
So for e.g. I show user a carousel of Hero Cards to choose a particular Cuisine. And when user clicks on a button on Hero Card it should capture user's choice and pass the control back to a particular C# method.
Above method will simply launch the url and my code would not know what was clicked.
For that just change the actiontype to "imBack"...then put switch case in after function
@PrasadGautham Can you please provide the the example code snippet.
List <CardAction> Go = new List <CardAction> { new CardAction(ActionTypes.ImBack, title: "button1", value: "button1"), new CardAction(ActionTypes.ImBack, "buttton2", value: "buttton2"), new CardAction(ActionTypes.ImBack, "buttton3", value: "buttton3") };
HeroCard card = new HeroCard { Title = "Your Options", Buttons = Go };
var message = context.MakeMessage();
message.Attachments.Add(card.ToAttachment());
await context.PostAsync(message);
context.Wait(Yourafterfunction);
I think this will work...I didnt run it though
@PrasadGautham Thanks alot. It really helped 👍
@PrasadGautham I tried doing the above but it gave me this error
Exception: IDialog method execution finished with multiple resume handlers specified through IDialogStack.
Yourfunctionafter should have context.done(new object());
Michael Hutchful
Sent from my Windows 10 phone
From: nupurkamble Sent: 26 June 2017 05:38 To: Microsoft/BotBuilder Cc: Subscribed Subject: Re: [Microsoft/BotBuilder] [Question] Trigger c# Function on click ofHero Card Button (#2976)
@PrasadGautham I tried doing the above but it gave me this error Exception: IDialog method execution finished with multiple resume handlers specified through IDialogStack. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.
@michaelhutchful I tried doing that. But it returns the same error. This is my code
private async Task ConnectWithPractice(IDialogContext context)
{
var result = GetConnectCard(phone);
var message = context.MakeMessage();
message.Attachments.Add(result);
await context.PostAsync(message);
context.Wait(ConnectMail);
}
//opens outlook
private async Task ConnectMail(IDialogContext context, IAwaitable<object> result)
{
var temp = await result;
if (temp.ToString().Equals("Connect with Email"))
{
Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.To = "[email protected]";
oMailItem.Subject = "Test";
oMailItem.Display(true);
}
context.Done(new object());
}
//card for showing options to connect
private Attachment GetConnectCard(string phone)
{
List<CardAction> buttons = new List<CardAction>();
CardAction button1 = new CardAction()
{
Value = "tel:"+phone,
Type = "call",
Title = "Connect with Call"
};
CardAction button2 = new CardAction()
{
Value = "Connect with Email",
Type = "ImBack",
Title = "Connect with Email"
};
buttons.Add(button1);
buttons.Add(button2);
var card = new HeroCard()
{
Title = "How would you like to connect ?",
Buttons = buttons
};
return card.ToAttachment();
}
Step through your code and make sure it reaches the context. Done
Michael
Sent from my Windows 10 phone
From: nupurkamble Sent: 26 June 2017 06:59 To: Microsoft/BotBuilder Cc: michaelhutchful; Mention Subject: Re: [Microsoft/BotBuilder] [Question] Trigger c# Function on click ofHero Card Button (#2976)
@michaelhutchful I tried doing that. But it returns the same error. This is my code private async Task ConnectWithPractice(IDialogContext context) { var result = GetConnectCard(phone); var message = context.MakeMessage(); message.Attachments.Add(result); await context.PostAsync(message); context.Wait(ConnectMail); }
//opens outlook
private async Task ConnectMail(IDialogContext context, IAwaitable<object> result)
{
var temp = await result;
if (temp.ToString().Equals("Connect with Email"))
{
Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.To = "[email protected]";
oMailItem.Subject = "Test";
oMailItem.Display(true);
}
context.Done(new object());
}
//card for showing options to connect
private Attachment GetConnectCard(string phone)
{
List<CardAction> buttons = new List<CardAction>();
CardAction button1 = new CardAction()
{
Value = "tel:"+phone,
Type = "call",
Title = "Connect with Call"
};
CardAction button2 = new CardAction()
{
Value = "Connect with Email",
Type = "ImBack",
Title = "Connect with Email"
};
buttons.Add(button1);
buttons.Add(button2);
var card = new HeroCard()
{
Title = "How would you like to connect ?",
Buttons = buttons
};
return card.ToAttachment();
}
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
@michaelhutchful okay! Thank you so much :)
did it work?? context.Done
Yes it did. I don't know if it's the same thing. But context.Done(new object()) worked for me.
please can one make the value of the button referenced to a method or class
I tried doing what @nupurkamble did but the value in temp variable in ConnectMail is "Microsoft.Bot.Connector.Activity". I was hoping the value in the temp var to be the card action of type "ImBack"
@michaelhutchful I tried doing that. But it returns the same error. This is my code
private async Task ConnectWithPractice(IDialogContext context) { var result = GetConnectCard(phone); var message = context.MakeMessage(); message.Attachments.Add(result); await context.PostAsync(message); context.Wait(ConnectMail); } //opens outlook private async Task ConnectMail(IDialogContext context, IAwaitable<object> result) { var temp = await result; if (temp.ToString().Equals("Connect with Email")) { Outlook.Application oApp = new Outlook.Application(); Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); oMailItem.To = "[email protected]"; oMailItem.Subject = "Test"; oMailItem.Display(true); } context.Done(new object()); } //card for showing options to connect private Attachment GetConnectCard(string phone) { List<CardAction> buttons = new List<CardAction>(); CardAction button1 = new CardAction() { Value = "tel:"+phone, Type = "call", Title = "Connect with Call" }; CardAction button2 = new CardAction() { Value = "Connect with Email", Type = "ImBack", Title = "Connect with Email" }; buttons.Add(button1); buttons.Add(button2); var card = new HeroCard() { Title = "How would you like to connect ?", Buttons = buttons }; return card.ToAttachment(); }
Hi, could you tell how to use the cardAction of chatbot. I need to show the list of question like prompt and after click one of them it should give the respone base on qna. please help me
hi @asif1380 I haven't worked on the bot framework in a while so I would not be able to help you. Maybe someone else can assist. In the mean time I would look into it in my spare time