Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit 3bc6ae8

Browse files
ChengxianModarrenj
authored andcommitted
Chenmo/refine user service (#439)
* add user model and refine user service * add user api UT * fix regression for flow tests * add copy bot to all skill project * change exception message and add todos
1 parent 4500f61 commit 3bc6ae8

File tree

17 files changed

+838
-199
lines changed

17 files changed

+838
-199
lines changed

solutions/Virtual-Assistant/src/csharp/skills/calendarskill/CalendarSkill.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@
100100
<None Update="*.bot">
101101
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
102102
</None>
103+
<None Update="LocaleConfigurations\*.bot">
104+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
105+
</None>
103106
</ItemGroup>
104107

105108
<ItemGroup>

solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Dialogs/CreateEvent/CreateEventDialog.cs

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Microsoft.Bot.Solutions.Extensions;
1818
using Microsoft.Bot.Solutions.Resources;
1919
using Microsoft.Bot.Solutions.Skills;
20-
using Microsoft.Graph;
2120
using Calendar = Luis.Calendar;
2221

2322
namespace CalendarSkill
@@ -414,7 +413,7 @@ public CreateEventDialog(
414413
}
415414
else
416415
{
417-
if (state.EventSource == EventSource.Microsoft)
416+
if (state.EventSource != EventSource.Other)
418417
{
419418
if (userInput != null)
420419
{
@@ -467,7 +466,7 @@ public CreateEventDialog(
467466
var originContactList = await GetContactsAsync(sc, currentRecipientName);
468467
originPersonList.AddRange(originContactList);
469468

470-
var originUserList = new List<Person>();
469+
var originUserList = new List<PersonModel>();
471470
try
472471
{
473472
originUserList = await GetUserAsync(sc, currentRecipientName);
@@ -498,7 +497,7 @@ public CreateEventDialog(
498497
var result =
499498
new FoundChoice()
500499
{
501-
Value = $"{user.DisplayName}: {user.ScoredEmailAddresses.FirstOrDefault()?.Address ?? user.UserPrincipalName}",
500+
Value = $"{user.DisplayName}: {user.Emails[0] ?? user.UserPrincipalName}",
502501
};
503502

504503
return await sc.NextAsync(result, cancellationToken);
@@ -895,44 +894,20 @@ public CreateEventDialog(
895894
}
896895
}
897896

898-
public async Task<List<Person>> GetUserAsync(WaterfallStepContext sc, string name)
899-
{
900-
var result = new List<Person>();
901-
var state = await Accessor.GetAsync(sc.Context);
902-
try
903-
{
904-
var token = state.APIToken;
905-
var service = ServiceManager.InitUserService(token, state.EventSource);
906-
907-
// Get users.
908-
var userList = await service.GetUserAsync(name);
909-
foreach (var user in userList)
910-
{
911-
result.Add(user.ToPerson());
912-
}
913-
}
914-
catch (ServiceException)
915-
{
916-
// won't clear conversation state hear, because sometime use api is not available, like user msa account.
917-
}
918-
919-
return result;
920-
}
921-
922-
protected static (List<Person> formattedPersonList, List<Person> formattedUserList) FormatRecipientList(List<Person> personList, List<Person> userList)
897+
protected static (List<PersonModel> formattedPersonList, List<PersonModel> formattedUserList) FormatRecipientList(List<PersonModel> personList, List<PersonModel> userList)
923898
{
924899
// Remove dup items
925-
List<Person> formattedPersonList = new List<Person>();
926-
List<Person> formattedUserList = new List<Person>();
900+
List<PersonModel> formattedPersonList = new List<PersonModel>();
901+
List<PersonModel> formattedUserList = new List<PersonModel>();
927902

928903
foreach (var person in personList)
929904
{
930-
var mailAddress = person.ScoredEmailAddresses.FirstOrDefault()?.Address ?? person.UserPrincipalName;
905+
var mailAddress = person.Emails[0] ?? person.UserPrincipalName;
931906

932907
bool isDup = false;
933908
foreach (var formattedPerson in formattedPersonList)
934909
{
935-
var formattedMailAddress = formattedPerson.ScoredEmailAddresses.FirstOrDefault()?.Address ?? formattedPerson.UserPrincipalName;
910+
var formattedMailAddress = formattedPerson.Emails[0] ?? formattedPerson.UserPrincipalName;
936911

937912
if (mailAddress.Equals(formattedMailAddress))
938913
{
@@ -949,12 +924,12 @@ protected static (List<Person> formattedPersonList, List<Person> formattedUserLi
949924

950925
foreach (var user in userList)
951926
{
952-
var mailAddress = user.ScoredEmailAddresses.FirstOrDefault()?.Address ?? user.UserPrincipalName;
927+
var mailAddress = user.Emails[0] ?? user.UserPrincipalName;
953928

954929
bool isDup = false;
955930
foreach (var formattedPerson in formattedPersonList)
956931
{
957-
var formattedMailAddress = formattedPerson.ScoredEmailAddresses.FirstOrDefault()?.Address ?? formattedPerson.UserPrincipalName;
932+
var formattedMailAddress = formattedPerson.Emails[0] ?? formattedPerson.UserPrincipalName;
958933

959934
if (mailAddress.Equals(formattedMailAddress))
960935
{
@@ -967,7 +942,7 @@ protected static (List<Person> formattedPersonList, List<Person> formattedUserLi
967942
{
968943
foreach (var formattedUser in formattedUserList)
969944
{
970-
var formattedMailAddress = formattedUser.ScoredEmailAddresses.FirstOrDefault()?.Address ?? formattedUser.UserPrincipalName;
945+
var formattedMailAddress = formattedUser.Emails[0] ?? formattedUser.UserPrincipalName;
971946

972947
if (mailAddress.Equals(formattedMailAddress))
973948
{
@@ -986,7 +961,7 @@ protected static (List<Person> formattedPersonList, List<Person> formattedUserLi
986961
return (formattedPersonList, formattedUserList);
987962
}
988963

989-
protected async Task<PromptOptions> GenerateOptions(List<Person> personList, List<Person> userList, DialogContext dc)
964+
protected async Task<PromptOptions> GenerateOptions(List<PersonModel> personList, List<PersonModel> userList, DialogContext dc)
990965
{
991966
var state = await Accessor.GetAsync(dc.Context);
992967
var pageIndex = state.ShowAttendeesIndex;
@@ -1000,7 +975,7 @@ protected async Task<PromptOptions> GenerateOptions(List<Person> personList, Lis
1000975
for (var i = 0; i < personList.Count; i++)
1001976
{
1002977
var user = personList[i];
1003-
var mailAddress = user.ScoredEmailAddresses.FirstOrDefault()?.Address ?? user.UserPrincipalName;
978+
var mailAddress = user.Emails[0] ?? user.UserPrincipalName;
1004979

1005980
var choice = new Choice()
1006981
{
@@ -1040,7 +1015,7 @@ protected async Task<PromptOptions> GenerateOptions(List<Person> personList, Lis
10401015
for (var i = 0; i < userList.Count; i++)
10411016
{
10421017
var user = userList[i];
1043-
var mailAddress = user.ScoredEmailAddresses.FirstOrDefault()?.Address ?? user.UserPrincipalName;
1018+
var mailAddress = user.Emails[0] ?? user.UserPrincipalName;
10441019
var choice = new Choice()
10451020
{
10461021
Value = $"{user.DisplayName}: {mailAddress}",
@@ -1076,21 +1051,17 @@ protected async Task<PromptOptions> GenerateOptions(List<Person> personList, Lis
10761051
return options;
10771052
}
10781053

1079-
protected async Task<List<Person>> GetContactsAsync(WaterfallStepContext sc, string name)
1054+
protected async Task<List<PersonModel>> GetContactsAsync(WaterfallStepContext sc, string name)
10801055
{
1081-
var result = new List<Person>();
1056+
var result = new List<PersonModel>();
10821057
try
10831058
{
10841059
var state = await Accessor.GetAsync(sc.Context);
10851060
var token = state.APIToken;
10861061
var service = ServiceManager.InitUserService(token, state.EventSource);
10871062

10881063
// Get users.
1089-
var contactList = await service.GetContactsAsync(name);
1090-
foreach (var contact in contactList)
1091-
{
1092-
result.Add(contact.ToPerson());
1093-
}
1064+
result = await service.GetContactsAsync(name);
10941065
}
10951066
catch (Exception ex)
10961067
{
@@ -1101,9 +1072,9 @@ protected async Task<List<Person>> GetContactsAsync(WaterfallStepContext sc, str
11011072
return result;
11021073
}
11031074

1104-
protected async Task<List<Person>> GetPeopleWorkWithAsync(WaterfallStepContext sc, string name)
1075+
protected async Task<List<PersonModel>> GetPeopleWorkWithAsync(WaterfallStepContext sc, string name)
11051076
{
1106-
var result = new List<Person>();
1077+
var result = new List<PersonModel>();
11071078
try
11081079
{
11091080
var state = await Accessor.GetAsync(sc.Context);
@@ -1122,6 +1093,28 @@ protected async Task<List<Person>> GetPeopleWorkWithAsync(WaterfallStepContext s
11221093
return result;
11231094
}
11241095

1096+
protected async Task<List<PersonModel>> GetUserAsync(WaterfallStepContext sc, string name)
1097+
{
1098+
var result = new List<PersonModel>();
1099+
var state = await Accessor.GetAsync(sc.Context);
1100+
try
1101+
{
1102+
var token = state.APIToken;
1103+
var service = ServiceManager.InitUserService(token, state.EventSource);
1104+
1105+
// Get users.
1106+
result = await service.GetUserAsync(name);
1107+
1108+
}
1109+
catch (Microsoft.Graph.ServiceException)
1110+
{
1111+
// todo: add exception handling
1112+
// won't clear conversation state hear, because sometime use api is not available, like user msa account.
1113+
}
1114+
1115+
return result;
1116+
}
1117+
11251118
private string GetSelectPromptString(PromptOptions selectOption, bool containNumbers)
11261119
{
11271120
var result = string.Empty;

solutions/Virtual-Assistant/src/csharp/skills/calendarskill/Models/EventModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public dynamic Value
148148
gmailEventData = value;
149149
break;
150150
case EventSource.Other:
151-
throw new Exception("Get defaut type, please check");
151+
throw new Exception("The default event source is not initialized.");
152152
default:
153153
throw new Exception("Event Type not Defined");
154154
}

0 commit comments

Comments
 (0)