in blog-samples/CSharp/TriviaBotSpeechSample/TriviaApp/MainPage.xaml.cs [202:290]
private async void OnConversationUpdated(object sender, Activity e) => await RunOnUi(() =>
{
var appEntities = (AppEntities)null;
// Don't do anything for messages this client sent
if (e.From.Id == _botClient.ClientID)
{
return;
}
if (e.Entities != null)
{
appEntities =
(from entity in e.Entities
where entity.Type == "AppEntities"
select entity).FirstOrDefault()?.GetAs<AppEntities>();
if (appEntities != null)
{
// Parse message type
if (appEntities.MessageType != null)
{
var messageType = appEntities.MessageType;
_model["ShowGotItRight"] = false;
_model["ShowGotItWrong"] = false;
if (messageType != MessageType.Question)
{
_answerCards.Clear();
UpdateAnswerCards();
_countdownTimer.Reset();
_model["RemainingTime"] = "∞";
switch (messageType)
{
case MessageType.Statement:
break;
case MessageType.GotItRight:
++_rightAnswers;
_model["Score1"] = _rightAnswers;
_model["ShowGotItRight"] = true;
break;
case MessageType.GotItWrong:
++_wrongAnswers;
_model["Score0"] = _wrongAnswers;
_model["ShowGotItWrong"] = true;
break;
case MessageType.StartLightningMode:
_lightningMode = true;
break;
case MessageType.StopLightningMode:
_lightningMode = false;
break;
}
}
}
}
}
if (!string.IsNullOrEmpty(e.Text))
{
var choicesMessage = string.Empty;
// Parse choices
if (appEntities?.MessageType == MessageType.Question)
{
// We've received a question from the bot. Tell the bot client that the user is likely to say one of
// these options, so it can specifically listen for them (supported in the Microsoft.Bot.Client.Universal.WindowsMediaSpeechRecognizer)
_botClient.ListenFor(appEntities.TriviaAnswerOptions);
_answerCards.Clear();
var index = 1;
foreach (var option in appEntities.TriviaAnswerOptions ?? Enumerable.Empty<string>())
{
_answerCards.Add(new AnswerCard { Index = $"{index}.", AnswerText = $" {option}" });
choicesMessage += $"\n {index}. {option}";
++index;
}
UpdateAnswerCards();
_startCountdownTimer = true;
}
_model["LastBotMessage"] = e.Text;
AddChatMessage(MessageSource.Bot, e.Text + choicesMessage);
}
});