Azure SMS-Based Testing Solution

SendSmsQuizQuestion Code

Question

HOTSPOT -

You are developing a SMS-based testing solution. The solution sends users a question by using SMS. Early responders may qualify for prizes.

Users must respond with an answer choice within 90 seconds. You must be able to track how long it takes each user to respond. You create a durable Azure

Function named SendSmsQuizQuestion that uses Twilio to send messages.

You need to write the code for MessageQuiz.

How should you complete the code? To answer, select the appropriate options in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

Explanations

Explanation

To complete the code for MessageQuiz, you need to add code that handles the response from the user and tracks the time it takes for the user to respond.

The following code shows an example implementation for MessageQuiz:

csharp
public static async Task<IActionResult> MessageQuiz( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, [DurableClient] IDurableOrchestrationClient starter, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string phoneNumber = data?.phoneNumber; string question = data?.question; string[] answerChoices = data?.answerChoices; // Generate a unique ID for the quiz string quizId = Guid.NewGuid().ToString("N"); // Start the quiz orchestration var orchestrationInput = new QuizOrchestrationInput { PhoneNumber = phoneNumber, Question = question, AnswerChoices = answerChoices, QuizId = quizId }; string instanceId = await starter.StartNewAsync("QuizOrchestration", null, orchestrationInput); return new OkObjectResult(new { InstanceId = instanceId, QuizId = quizId }); } [FunctionName("QuizOrchestration")] public static async Task<List<string>> RunQuizOrchestration( [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log) { // Get the input parameters var input = context.GetInput<QuizOrchestrationInput>(); // Send the question via SMS using Twilio var twilioClient = new TwilioRestClient(Environment.GetEnvironmentVariable("TwilioAccountSid"), Environment.GetEnvironmentVariable("TwilioAuthToken")); var message = await twilioClient.SendMessageAsync( new Twilio.Types.PhoneNumber(input.PhoneNumber), input.Question ); // Wait for 90 seconds for a response from the user var timeout = TimeSpan.FromSeconds(90); var dueTime = context.CurrentUtcDateTime.Add(timeout); var responseTask = context.WaitForExternalEvent<string>("QuizResponse"); var timeoutTask = context.CreateTimer(dueTime, CancellationToken.None); var winner = await Task.WhenAny(responseTask, timeoutTask); if (winner == responseTask) { // User responded before timeout var response = responseTask.Result; var responseTime = context.CurrentUtcDateTime - message.DateCreated.ToUniversalTime(); log.LogInformation($"User responded with {response} after {responseTime.TotalSeconds} seconds"); return new List<string> { "Correct", responseTime.TotalSeconds.ToString() }; } else { // Timeout occurred log.LogInformation($"Timeout occurred"); return new List<string> { "Timeout" }; } } public class QuizOrchestrationInput { public string PhoneNumber { get; set; } public string Question { get; set; } public string[] AnswerChoices { get; set; } public string QuizId { get; set; } } [FunctionName("QuizResponse")] public static async Task<IActionResult> QuizResponse( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, [DurableClient] IDurableOrchestrationClient client, ILogger log) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string quizId = data?.quizId; string response = data?.response; // Raise an event for the quiz orchest