in OneDriveAlexaSkill/OneDriveAlexaSkill/Skill.cs [23:86]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
{
var json = await req.ReadAsStringAsync();
var skillRequest = JsonConvert.DeserializeObject<SkillRequest>(json);
// Verifies that the request is indeed coming from Alexa.
var isValid = await skillRequest.ValidateRequest(req, log);
if (!isValid)
{
return new BadRequestResult();
}
var request = skillRequest.Request;
var token = skillRequest.Session.User.AccessToken;
var client = GetAuthenticatedClientForUser(token);
SkillResponse response = null;
try
{
if (request is LaunchRequest launchRequest)
{
log.LogInformation("Session started");
var me = await client.Me.Request().GetAsync();
response = ResponseBuilder.Tell($"Hello {me.DisplayName}");
response.Response.ShouldEndSession = false;
}
else if (request is IntentRequest intentRequest)
{
// Checks whether to handle system messages defined by Amazon.
var systemIntentResponse = HandleSystemIntentRequest(intentRequest);
if (systemIntentResponse.IsHandled)
{
response = systemIntentResponse.Response;
}
else
{
if (intentRequest.Intent.Name == "OneDriveQuota")
{
log.LogInformation($"Request url: {client.Me.Drive.Request().RequestUrl}");
var drive = await client.Me.Drive.Request().GetAsync();
int used = ConvertBytesToTereabytes(drive.Quota.Used);
int total = ConvertBytesToTereabytes(drive.Quota.Total);
response = ResponseBuilder.Tell($"You are using {used} GB out of a total of {total} GB");
}
}
}
else if (request is SessionEndedRequest sessionEndedRequest)
{
log.LogInformation("Session ended");
response = ResponseBuilder.Empty();
response.Response.ShouldEndSession = true;
}
}
catch (Exception exc)
{
log.LogError(exc, "Error getting the response");
response = ResponseBuilder.Tell("I'm sorry, there was an unexpected error. Please, try again later.");
}
return new OkObjectResult(response);
}