protected override async void OnRun(IBackgroundTaskInstance taskInstance)
{
this.serviceDeferral = taskInstance.GetDeferral();
taskInstance.Canceled += OnTaskCanceled;
var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
VoiceCommandUserMessage userMessage;
VoiceCommandResponse response;
try
{
voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted;
VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();
switch (voiceCommand.CommandName)
{
case "where":
var city = voiceCommand.Properties["city"][0];
var imageFile = await GenerateWideIconWithCity(city);
var localFolder = ApplicationData.Current.LocalFolder;
StorageFile cityIcon = await localFolder.GetFileAsync(imageFile);
var contentTiles = new List<VoiceCommandContentTile>();
var tile1 = new VoiceCommandContentTile();
tile1.ContentTileType = VoiceCommandContentTileType.TitleWith280x140IconAndText;
tile1.AppLaunchArgument = city;
tile1.Image = cityIcon;
contentTiles.Add(tile1);
userMessage = new VoiceCommandUserMessage()
{
DisplayMessage = "Here you go Best Friend, it's " + city,
SpokenMessage = "Here you go Best Friend, it's " + city
};
response = VoiceCommandResponse.CreateResponse(userMessage, contentTiles);
await voiceServiceConnection.ReportSuccessAsync(response);
break;
case "sendMessageInCanvas":
var message = voiceCommand.Properties["message"][0];
var bot = new Bot();
string firstResponse = await bot.SendMessageAndGetResponseFromBot(message);
var responseMessage = new VoiceCommandUserMessage();
responseMessage.DisplayMessage = responseMessage.SpokenMessage = "Your Best Friend says \"" + firstResponse + "\"";
response = VoiceCommandResponse.CreateResponse(responseMessage);
await voiceServiceConnection.ReportSuccessAsync(response);
break;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
finally
{
if (this.serviceDeferral != null)
{
//Complete the service deferral
this.serviceDeferral.Complete();
}
}
}