private async Task <DialogTurnResult> GetUserFromUserNameAsync(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await Accessor.GetAsync(sc.Context, cancellationToken : cancellationToken);

                var options = (FindContactDialogOptions)sc.Options;
                var currentRecipientName = state.MeetingInfo.ContactInfor.CurrentContactName;

                // if it's an email
                if (!string.IsNullOrEmpty(currentRecipientName) && IsEmail(currentRecipientName))
                {
                    state.MeetingInfo.ContactInfor.CurrentContactName = string.Empty;
                    state.MeetingInfo.ContactInfor.ConfirmedContact   = new CustomizedPerson()
                    {
                        DisplayName = currentRecipientName,
                        Emails      = new List <ScoredEmailAddress>()
                        {
                            new ScoredEmailAddress()
                            {
                                Address = currentRecipientName
                            }
                        }
                    };
                    return(await sc.EndDialogAsync(cancellationToken : cancellationToken));
                }

                var unionList = new List <CustomizedPerson>();

                if (CreateEventWhiteList.GetMyself(currentRecipientName))
                {
                    var me = await GetMeAsync(sc.Context, cancellationToken);

                    unionList.Add(new CustomizedPerson(me));
                }
                else if (!string.IsNullOrEmpty(currentRecipientName) && state.MeetingInfo.ContactInfor.RelatedEntityInfoDict.ContainsKey(currentRecipientName))
                {
                    string pronounType  = state.MeetingInfo.ContactInfor.RelatedEntityInfoDict[currentRecipientName].PronounType;
                    string relationship = state.MeetingInfo.ContactInfor.RelatedEntityInfoDict[currentRecipientName].RelationshipName;
                    var    personList   = new List <PersonModel>();
                    if (pronounType == PronounType.FirstPerson)
                    {
                        if (Regex.IsMatch(relationship, CalendarCommonStrings.Manager, RegexOptions.IgnoreCase))
                        {
                            var person = await GetMyManagerAsync(sc, cancellationToken);

                            if (person != null)
                            {
                                personList.Add(person);
                            }
                        }
                    }
                    else if (pronounType == PronounType.ThirdPerson && state.MeetingInfo.ContactInfor.Contacts.Count > 0)
                    {
                        int    count   = state.MeetingInfo.ContactInfor.Contacts.Count;
                        string prename = state.MeetingInfo.ContactInfor.Contacts[count - 1].UserPrincipalName;
                        if (Regex.IsMatch(relationship, CalendarCommonStrings.Manager, RegexOptions.IgnoreCase))
                        {
                            var person = await GetManagerAsync(sc, prename, cancellationToken);

                            if (person != null)
                            {
                                personList.Add(person);
                            }
                        }
                    }

                    foreach (var person in personList)
                    {
                        unionList.Add(new CustomizedPerson(person));
                    }
                }
                else
                {
                    var originPersonList = await GetPeopleWorkWithAsync(sc, currentRecipientName, cancellationToken);

                    var originContactList = await GetContactsAsync(sc, currentRecipientName, cancellationToken);

                    originPersonList.AddRange(originContactList);

                    var originUserList = new List <PersonModel>();
                    try
                    {
                        originUserList = await GetUserAsync(sc, currentRecipientName, cancellationToken);
                    }
                    catch
                    {
                        // do nothing when get user failed. because can not use token to ensure user use a work account.
                    }

                    (var personList, var userList) = FormatRecipientList(originPersonList, originUserList);

                    // people you work with has the distinct email address has the highest priority
                    if (personList.Count == 1 && personList.First().Emails.Any() && personList.First().Emails.First() != null)
                    {
                        unionList.Add(new CustomizedPerson(personList.First()));
                    }
                    else
                    {
                        personList.AddRange(userList);

                        foreach (var person in personList)
                        {
                            if (unionList.Find(p => p.DisplayName == person.DisplayName) == null)
                            {
                                var personWithSameName = personList.FindAll(p => p.DisplayName == person.DisplayName);
                                if (personWithSameName.Count == 1)
                                {
                                    unionList.Add(new CustomizedPerson(personWithSameName.First()));
                                }
                                else
                                {
                                    var unionPerson  = new CustomizedPerson(personWithSameName.FirstOrDefault());
                                    var curEmailList = new List <ScoredEmailAddress>();
                                    foreach (var sameNamePerson in personWithSameName)
                                    {
                                        sameNamePerson.Emails.ToList().ForEach(e =>
                                        {
                                            if (!string.IsNullOrEmpty(e))
                                            {
                                                curEmailList.Add(new ScoredEmailAddress {
                                                    Address = e
                                                });
                                            }
                                        });
                                    }

                                    unionPerson.Emails = curEmailList;
                                    unionList.Add(unionPerson);
                                }
                            }
                        }
                    }
                }

                unionList.RemoveAll(person => !person.Emails.Exists(email => email.Address != null));
                unionList.RemoveAll(person => !person.Emails.Any());

                state.MeetingInfo.ContactInfor.UnconfirmedContact = unionList;

                if (unionList.Count == 0)
                {
                    if (!(options.UpdateUserNameReason == FindContactDialogOptions.UpdateUserNameReasonType.Initialize))
                    {
                        options.FirstRetry = false;
                    }

                    options.UpdateUserNameReason = FindContactDialogOptions.UpdateUserNameReasonType.NotFound;
                    return(await sc.ReplaceDialogAsync(Actions.UpdateName, options, cancellationToken));
                }

                return(await sc.EndDialogAsync(cancellationToken : cancellationToken));
            }
            catch (SkillException skillEx)
            {
                await HandleDialogExceptionsAsync(sc, skillEx, cancellationToken);

                return(new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs));
            }
            catch (Exception ex)
            {
                await HandleDialogExceptionsAsync(sc, ex, cancellationToken);

                return(new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs));
            }
        }
Exemple #2
0
        public async Task <DialogTurnResult> AfterUpdateUserName(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var userInput = sc.Result as string;
                var state     = await Accessor.GetAsync(sc.Context);

                var options = (UpdateUserNameDialogOptions)sc.Options;

                if (string.IsNullOrEmpty(userInput) && options.Reason != UpdateUserNameDialogOptions.UpdateReason.Initialize)
                {
                    await sc.Context.SendActivityAsync(ResponseManager.GetResponse(FindContactResponses.UserNotFoundAgain, new StringDictionary()
                    {
                        { "source", state.EventSource == EventSource.Microsoft ? "Outlook Calendar" : "Google Calendar" }
                    }));

                    return(await sc.EndDialogAsync());
                }

                string currentRecipientName = string.IsNullOrEmpty(userInput) ? state.CurrentAttendeeName : userInput;
                state.CurrentAttendeeName = currentRecipientName;

                // if it's an email, add to attendee and kepp the state.ConfirmedPerson null
                if (!string.IsNullOrEmpty(currentRecipientName) && IsEmail(currentRecipientName))
                {
                    var attendee = new EventModel.Attendee
                    {
                        DisplayName = currentRecipientName,
                        Address     = currentRecipientName
                    };
                    if (state.Attendees.All(r => r.Address != attendee.Address))
                    {
                        state.Attendees.Add(attendee);
                    }

                    state.CurrentAttendeeName = string.Empty;
                    state.ConfirmedPerson     = null;
                    return(await sc.EndDialogAsync());
                }

                List <CustomizedPerson> unionList = new List <CustomizedPerson>();

                if (CreateEventWhiteList.GetMyself(currentRecipientName))
                {
                    var me = await GetMe(sc);

                    unionList.Add(new CustomizedPerson(me));
                }
                else
                {
                    var originPersonList = await GetPeopleWorkWithAsync(sc, currentRecipientName);

                    var originContactList = await GetContactsAsync(sc, currentRecipientName);

                    originPersonList.AddRange(originContactList);

                    var originUserList = new List <PersonModel>();
                    try
                    {
                        originUserList = await GetUserAsync(sc, currentRecipientName);
                    }
                    catch
                    {
                        // do nothing when get user failed. because can not use token to ensure user use a work account.
                    }

                    (var personList, var userList) = FormatRecipientList(originPersonList, originUserList);

                    // people you work with has the distinct email address has the highest priority
                    if (personList.Count == 1 && personList.First().Emails.Any() && personList.First().Emails.First() != null)
                    {
                        unionList.Add(new CustomizedPerson(personList.First()));
                    }
                    else
                    {
                        personList.AddRange(userList);

                        foreach (var person in personList)
                        {
                            if (unionList.Find(p => p.DisplayName == person.DisplayName) == null)
                            {
                                var personWithSameName = personList.FindAll(p => p.DisplayName == person.DisplayName);
                                if (personWithSameName.Count == 1)
                                {
                                    unionList.Add(new CustomizedPerson(personWithSameName.First()));
                                }
                                else
                                {
                                    var unionPerson  = new CustomizedPerson(personWithSameName.FirstOrDefault());
                                    var curEmailList = new List <ScoredEmailAddress>();
                                    foreach (var sameNamePerson in personWithSameName)
                                    {
                                        sameNamePerson.Emails.ToList().ForEach(e =>
                                        {
                                            if (!string.IsNullOrEmpty(e))
                                            {
                                                curEmailList.Add(new ScoredEmailAddress {
                                                    Address = e
                                                });
                                            }
                                        });
                                    }

                                    unionPerson.Emails = curEmailList;
                                    unionList.Add(unionPerson);
                                }
                            }
                        }
                    }
                }

                unionList.RemoveAll(person => !person.Emails.Exists(email => email.Address != null));
                unionList.RemoveAll(person => !person.Emails.Any());

                state.UnconfirmedPerson = unionList;

                if (unionList.Count == 0)
                {
                    return(await sc.ReplaceDialogAsync(Actions.UpdateName, new UpdateUserNameDialogOptions(UpdateUserNameDialogOptions.UpdateReason.NotFound)));
                }
                else
                if (unionList.Count == 1)
                {
                    state.ConfirmedPerson = unionList.First();
                    return(await sc.EndDialogAsync());
                }
                else
                {
                    return(await sc.ReplaceDialogAsync(Actions.SelectPerson, sc.Options, cancellationToken));
                }
            }
            catch (SkillException skillEx)
            {
                await HandleDialogExceptions(sc, skillEx);

                return(new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs));
            }
            catch (Exception ex)
            {
                await HandleDialogExceptions(sc, ex);

                return(new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs));
            }
        }
        public async Task <DialogTurnResult> ConfirmName(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await Accessor.GetAsync(sc.Context);

                if ((state.AttendeesNameList == null) || (state.AttendeesNameList.Count == 0))
                {
                    return(await sc.BeginDialogAsync(Actions.UpdateName, new UpdateUserNameDialogOptions(UpdateUserNameDialogOptions.UpdateReason.NotFound)));
                }

                var unionList = new List <CustomizedPerson>();
                var emailList = new List <string>();

                if (state.FirstEnterFindContact)
                {
                    state.FirstEnterFindContact = false;
                    foreach (var name in state.AttendeesNameList)
                    {
                        if (IsEmail(name))
                        {
                            emailList.Add(name);
                        }
                    }

                    if (state.AttendeesNameList.Count > 1)
                    {
                        var nameString = await GetReadyToSendNameListStringAsync(sc);

                        await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(FindContactResponses.BeforeSendingMessage, null, new StringDictionary()
                        {
                            { "NameList", nameString }
                        }));
                    }
                }

                if (emailList.Count > 0)
                {
                    foreach (var email in emailList)
                    {
                        var attendee = new EventModel.Attendee
                        {
                            DisplayName = email,
                            Address     = email
                        };
                        if (state.Attendees.All(r => r.Address != attendee.Address))
                        {
                            state.Attendees.Add(attendee);
                        }
                    }

                    state.AttendeesNameList.RemoveAll(n => IsEmail(n));

                    if (state.AttendeesNameList.Count > 0)
                    {
                        return(await sc.ReplaceDialogAsync(Actions.ConfirmName, options : sc.Options, cancellationToken : cancellationToken));
                    }
                    else
                    {
                        return(await sc.EndDialogAsync());
                    }
                }

                if (state.ConfirmAttendeesNameIndex < state.AttendeesNameList.Count)
                {
                    var currentRecipientName = state.AttendeesNameList[state.ConfirmAttendeesNameIndex];

                    if (CreateEventWhiteList.GetMyself().Contains(currentRecipientName))
                    {
                        var me = await GetMe(sc);

                        unionList.Add(new CustomizedPerson(me));
                    }
                    else
                    {
                        var originPersonList = await GetPeopleWorkWithAsync(sc, currentRecipientName);

                        var originContactList = await GetContactsAsync(sc, currentRecipientName);

                        originPersonList.AddRange(originContactList);

                        var originUserList = new List <PersonModel>();
                        try
                        {
                            originUserList = await GetUserAsync(sc, currentRecipientName);
                        }
                        catch
                        {
                            // do nothing when get user failed. because can not use token to ensure user use a work account.
                        }

                        (var personList, var userList) = FormatRecipientList(originPersonList, originUserList);

                        // people you work with has the distinct email address has the highest priority
                        if (personList.Count == 1 && personList.First().Emails.Count == 1)
                        {
                            state.ConfirmedPerson = new CustomizedPerson(personList.First());
                            var highestPriorityPerson = new CustomizedPerson(personList.First());
                            return(await sc.ReplaceDialogAsync(Actions.ConfirmEmail, highestPriorityPerson));
                        }

                        personList.AddRange(userList);

                        foreach (var person in personList)
                        {
                            if (unionList.Find(p => p.DisplayName == person.DisplayName) == null)
                            {
                                var personWithSameName = personList.FindAll(p => p.DisplayName == person.DisplayName);
                                if (personWithSameName.Count == 1)
                                {
                                    unionList.Add(new CustomizedPerson(personWithSameName.First()));
                                }
                                else
                                {
                                    var unionPerson  = new CustomizedPerson(personWithSameName.FirstOrDefault());
                                    var curEmailList = new List <ScoredEmailAddress>();
                                    foreach (var sameNamePerson in personWithSameName)
                                    {
                                        sameNamePerson.Emails.ToList().ForEach(e => curEmailList.Add(new ScoredEmailAddress {
                                            Address = e
                                        }));
                                    }

                                    unionPerson.Emails = curEmailList;
                                    unionList.Add(unionPerson);
                                }
                            }
                        }
                    }
                }
                else
                {
                    return(await sc.EndDialogAsync());
                }

                state.UnconfirmedPerson = unionList;

                if (unionList.Count == 0)
                {
                    return(await sc.BeginDialogAsync(Actions.UpdateName, new UpdateUserNameDialogOptions(UpdateUserNameDialogOptions.UpdateReason.NotFound)));
                }
                else if (unionList.Count == 1)
                {
                    state.ConfirmedPerson = unionList.First();
                    return(await sc.ReplaceDialogAsync(Actions.ConfirmEmail, unionList.First()));
                }
                else
                {
                    var nameString = string.Empty;
                    if (unionList.Count <= ConfigData.GetInstance().MaxDisplaySize)
                    {
                        return(await sc.PromptAsync(Actions.Choice, await GenerateOptionsForName(sc, unionList, sc.Context, true)));
                    }
                    else
                    {
                        return(await sc.PromptAsync(Actions.Choice, await GenerateOptionsForName(sc, unionList, sc.Context, false)));
                    }
                }
            }
            catch (SkillException skillEx)
            {
                await HandleDialogExceptions(sc, skillEx);

                return(new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs));
            }
            catch (Exception ex)
            {
                await HandleDialogExceptions(sc, ex);

                return(new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs));
            }
        }