I built a bot using the Microsoft.Bot.Builder.Azure
4.12.2 connected to MS Teams via the Azure Bot Service. I have a message with a Hero Card attachment containing a set of buttons. When a user clicks on a button, the value of the card is sent back to the bot as a message, but there doesn't seem to be any other information attached to identify that the message was a button click as opposed to a message. I'd like to understand how to properly handle the button click.
I'll show my code to demonstrate...
public class MyBot : ActivityHandler
{
protected override async Task OnMessageActivityAsync(
ITurnContext<IMessageActivity> turnContext,
CancellationToken cancellationToken)
{
var activity = turnContext.Activity;
if (activity.Text is "test")
{
var heroCard = new HeroCard
{
Buttons = new List<CardAction>
{
new(ActionTypes.ImBack)
{
Title = "Cup 1",
DisplayText = "You chose Cup1",
Value = "cup1",
ChannelData = new { id = 1, status = "wise" }
},
new(ActionTypes.ImBack)
{
Title = "Cup 2",
DisplayText = "You chose Cup2",
Value = "cup2",
ChannelData = new { id = 1, status = "unwise" }
}
}
};
var response = MessageFactory.Text("Choose wisely");
response.Attachments.Add(heroCard.ToAttachment());
await turnContext.SendActivityAsync(response, cancellationToken);
return;
}
// How can I tell that they actually clicked the Cup 1 button and didn't just type "cup1"?
if (activity.Text is "cup1")
{
await turnContext.SendActivityAsync(MessageFactory.Text("you chose wisely."), cancellationToken);
}
else
{
await turnContext.SendActivityAsync(MessageFactory.Text("you chose unwisely."), cancellationToken);
}
}
}
And here's an example of it in action.
Here's the sequence of activities.
- I send a message "test" to the bot.
- Bot responds with a message containing two buttons.
- I click the first button.
- Bot responds that I chose wisely.
- I type "cup1"
- Bot responds that I chose wisely.
What I want to be able to do is have the bot ignore if I type "cup1" because that's not a button click. When I examine the IMessageActivity
the bot receives on the button click, there doesn't seem to be anything that indicates it's a button click. Any help is appreciated!