Example #1
0
        public async Task <DialogTurnResult> ShowEventsSummary(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var tokenResponse = sc.Result as TokenResponse;

                var state = await Accessor.GetAsync(sc.Context);

                if (state.SummaryEvents == null)
                {
                    // this will lead to error when test
                    if (string.IsNullOrEmpty(state.APIToken))
                    {
                        return(await sc.EndDialogAsync(true));
                    }

                    var calendarAPI     = GraphClientHelper.GetCalendarService(state.APIToken, state.EventSource, ServiceManager.GetGoogleClient());
                    var calendarService = ServiceManager.InitCalendarService(calendarAPI, state.EventSource);

                    var searchDate = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, state.GetUserTimeZone());

                    if (state.StartDate != null)
                    {
                        searchDate = state.StartDate.Value;
                    }

                    var results = await GetEventsByTime(searchDate, state.StartTime, state.EndDate, state.EndTime, state.GetUserTimeZone(), calendarService);

                    var  searchedEvents     = new List <EventModel>();
                    bool searchTodayMeeting = state.StartDate != null &&
                                              state.StartTime == null &&
                                              state.EndDate == null &&
                                              state.EndTime == null &&
                                              EventModel.IsSameDate(state.StartDate.Value, TimeConverter.ConvertUtcToUserTime(DateTime.UtcNow, state.GetUserTimeZone()));
                    foreach (var item in results)
                    {
                        if (!searchTodayMeeting || item.StartTime >= DateTime.UtcNow)
                        {
                            searchedEvents.Add(item);
                        }
                    }

                    if (searchedEvents.Count == 0)
                    {
                        await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(SummaryResponses.ShowNoMeetingMessage));

                        return(await sc.EndDialogAsync(true));
                    }
                    else
                    {
                        var responseParams = new StringDictionary()
                        {
                            { "Count", searchedEvents.Count.ToString() },
                            { "EventName1", searchedEvents[0].Title },
                            { "EventDuration", searchedEvents[0].ToDurationString() },
                        };

                        if (searchedEvents.Count == 1)
                        {
                            await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(SummaryResponses.ShowOneMeetingSummaryMessage, ResponseBuilder, responseParams));
                        }
                        else
                        {
                            responseParams.Add("EventName2", searchedEvents[searchedEvents.Count - 1].Title);
                            if (searchedEvents[searchedEvents.Count - 1].IsAllDay == true)
                            {
                                responseParams.Add("EventTime", TimeConverter.ConvertUtcToUserTime(searchedEvents[searchedEvents.Count - 1].StartTime, state.GetUserTimeZone()).ToString("MMMM dd all day"));
                            }
                            else
                            {
                                responseParams.Add("EventTime", TimeConverter.ConvertUtcToUserTime(searchedEvents[searchedEvents.Count - 1].StartTime, state.GetUserTimeZone()).ToString("h:mm tt"));
                            }

                            await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(SummaryResponses.ShowMultipleMeetingSummaryMessage, ResponseBuilder, responseParams));
                        }
                    }

                    await ShowMeetingList(sc, searchedEvents.GetRange(0, Math.Min(CalendarSkillState.PageSize, searchedEvents.Count)), false);

                    state.Clear();
                    state.SummaryEvents = searchedEvents;
                }
                else
                {
                    await ShowMeetingList(sc, state.SummaryEvents.GetRange(state.ShowEventIndex *CalendarSkillState.PageSize, Math.Min(CalendarSkillState.PageSize, state.SummaryEvents.Count - (state.ShowEventIndex *CalendarSkillState.PageSize))), false);
                }

                return(await sc.NextAsync());
            }
            catch
            {
                await HandleDialogExceptions(sc);

                throw;
            }
        }
Example #2
0
        public async Task <DialogTurnResult> AfterUpdateStartTime(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await Accessor.GetAsync(sc.Context);

                var events = new List <EventModel>();

                var calendarAPI     = GraphClientHelper.GetCalendarService(state.APIToken, state.EventSource, ServiceManager.GetGoogleClient());
                var calendarService = ServiceManager.InitCalendarService(calendarAPI, state.EventSource);

                if (state.StartDate != null || state.StartTime != null)
                {
                    events = await GetEventsByTime(state.StartDate, state.StartTime, state.EndDate, state.EndTime, state.GetUserTimeZone(), calendarService);

                    state.StartDate = null;
                    state.StartTime = null;
                }
                else if (state.Title != null)
                {
                    events = await calendarService.GetEventsByTitle(state.Title);

                    state.Title = null;
                }
                else
                {
                    DateTime?startTime = null;
                    sc.Context.Activity.Properties.TryGetValue("OriginText", out var content);
                    var userInput = content != null?content.ToString() : sc.Context.Activity.Text;

                    try
                    {
                        IList <DateTimeResolution> dateTimeResolutions = sc.Result as List <DateTimeResolution>;
                        if (dateTimeResolutions.Count > 0)
                        {
                            startTime = DateTime.Parse(dateTimeResolutions.First().Value);
                            var  dateTimeConvertType = dateTimeResolutions.First().Timex;
                            bool isRelativeTime      = IsRelativeTime(sc.Context.Activity.Text, dateTimeResolutions.First().Value, dateTimeResolutions.First().Timex);
                            startTime = isRelativeTime ? TimeZoneInfo.ConvertTime(startTime.Value, TimeZoneInfo.Local, state.GetUserTimeZone()) : startTime;
                        }
                    }
                    catch
                    {
                    }

                    if (startTime != null)
                    {
                        startTime = TimeConverter.ConvertLuisLocalToUtc(startTime.Value, state.GetUserTimeZone());
                        events    = await calendarService.GetEventsByStartTime(startTime.Value);
                    }
                    else
                    {
                        state.Title = userInput;
                        events      = await calendarService.GetEventsByTitle(userInput);
                    }
                }

                state.Events = events;

                if (events.Count <= 0)
                {
                    return(await sc.BeginDialogAsync(Actions.UpdateStartTime, new UpdateDateTimeDialogOptions(UpdateDateTimeDialogOptions.UpdateReason.NoEvent)));
                }
                else if (events.Count > 1)
                {
                    var options = new PromptOptions()
                    {
                        Choices = new List <Choice>(),
                    };

                    for (var i = 0; i < events.Count; i++)
                    {
                        var item   = events[i];
                        var choice = new Choice()
                        {
                            Value    = string.Empty,
                            Synonyms = new List <string> {
                                (i + 1).ToString(), item.Title
                            },
                        };
                        options.Choices.Add(choice);
                    }

                    var replyToConversation = sc.Context.Activity.CreateReply(DeleteEventResponses.MultipleEventsStartAtSameTime);
                    replyToConversation.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                    replyToConversation.Attachments      = new List <Microsoft.Bot.Schema.Attachment>();

                    var cardsData = new List <CalendarCardData>();
                    foreach (var item in events)
                    {
                        var meetingCard = item.ToAdaptiveCardData(state.GetUserTimeZone());
                        var replyTemp   = sc.Context.Activity.CreateAdaptiveCardReply(CalendarMainResponses.GreetingMessage, item.OnlineMeetingUrl == null ? "Dialogs/Shared/Resources/Cards/CalendarCardNoJoinButton.json" : "Dialogs/Shared/Resources/Cards/CalendarCard.json", meetingCard);
                        replyToConversation.Attachments.Add(replyTemp.Attachments[0]);
                    }

                    options.Prompt = replyToConversation;

                    return(await sc.PromptAsync(Actions.EventChoice, options));
                }
                else
                {
                    return(await sc.EndDialogAsync(true));
                }
            }
            catch
            {
                await HandleDialogExceptions(sc);

                throw;
            }
        }
Example #3
0
        public async Task <DialogTurnResult> DeleteEventByStartTime(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await Accessor.GetAsync(sc.Context);

                var calendarAPI     = GraphClientHelper.GetCalendarService(state.APIToken, state.EventSource, ServiceManager.GetGoogleClient());
                var calendarService = ServiceManager.InitCalendarService(calendarAPI, state.EventSource);
                var confirmResult   = (bool)sc.Result;
                if (confirmResult)
                {
                    var deleteEvent = state.Events[0];
                    await calendarService.DeleteEventById(deleteEvent.Id);

                    await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(DeleteEventResponses.EventDeleted));
                }
                else
                {
                    await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(CalendarSharedResponses.ActionEnded));
                }

                state.Clear();
                return(await sc.EndDialogAsync(true));
            }
            catch
            {
                await HandleDialogExceptions(sc);

                throw;
            }
        }
Example #4
0
        public async Task <DialogTurnResult> FromTokenToStartTime(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await Accessor.GetAsync(sc.Context);

                if (string.IsNullOrEmpty(state.APIToken))
                {
                    return(await sc.EndDialogAsync(true));
                }

                var calendarAPI     = GraphClientHelper.GetCalendarService(state.APIToken, state.EventSource, ServiceManager.GetGoogleClient());
                var calendarService = ServiceManager.InitCalendarService(calendarAPI, state.EventSource);
                if (state.StartDateTime == null)
                {
                    return(await sc.BeginDialogAsync(Actions.UpdateStartTime, new UpdateDateTimeDialogOptions(UpdateDateTimeDialogOptions.UpdateReason.NotFound)));
                }
                else
                {
                    return(await sc.NextAsync());
                }
            }
            catch
            {
                await HandleDialogExceptions(sc);

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

                var confirmResult = (bool)sc.Result;
                if (confirmResult)
                {
                    var source   = state.EventSource;
                    var newEvent = new EventModel(source)
                    {
                        Title     = state.Title,
                        Content   = state.Content,
                        Attendees = state.Attendees,
                        StartTime = (DateTime)state.StartDateTime,
                        EndTime   = (DateTime)state.EndDateTime,
                        TimeZone  = TimeZoneInfo.Utc,
                        Location  = state.Location,
                    };
                    var calendarAPI     = GraphClientHelper.GetCalendarService(state.APIToken, state.EventSource, ServiceManager.GetGoogleClient());
                    var calendarService = ServiceManager.InitCalendarService(calendarAPI, state.EventSource);
                    if (await calendarService.CreateEvent(newEvent) != null)
                    {
                        var replyMessage = sc.Context.Activity.CreateAdaptiveCardReply(CreateEventResponses.EventCreated, newEvent.OnlineMeetingUrl == null ? "Dialogs/Shared/Resources/Cards/CalendarCardNoJoinButton.json" : "Dialogs/Shared/Resources/Cards/CalendarCard.json", newEvent.ToAdaptiveCardData(state.GetUserTimeZone()));
                        await sc.Context.SendActivityAsync(replyMessage, cancellationToken);
                    }
                    else
                    {
                        await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(CreateEventResponses.EventCreationFailed), cancellationToken);
                    }

                    state.Clear();
                }
                else
                {
                    await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(CalendarSharedResponses.ActionEnded), cancellationToken);

                    state.Clear();
                }

                return(await sc.EndDialogAsync(true, cancellationToken));
            }
            catch
            {
                await HandleDialogExceptions(sc);

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

                if (string.IsNullOrEmpty(state.APIToken))
                {
                    return(await sc.EndDialogAsync(true, cancellationToken));
                }

                var calendarAPI = GraphClientHelper.GetCalendarService(state.APIToken, state.EventSource, ServiceManager.GetGoogleClient());
                ServiceManager.InitCalendarService(calendarAPI, state.EventSource);

                if (state.Attendees.Count == 0)
                {
                    return(await sc.BeginDialogAsync(Actions.UpdateAddress, cancellationToken : cancellationToken));
                }
                else
                {
                    return(await sc.NextAsync(cancellationToken : cancellationToken));
                }
            }
            catch
            {
                await HandleDialogExceptions(sc);

                throw;
            }
        }
Example #7
0
        public async Task <DialogTurnResult> UpdateEventTime(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await Accessor.GetAsync(sc.Context);

                var confirmResult = (bool)sc.Result;
                if (confirmResult)
                {
                    var newStartTime = (DateTime)state.NewStartDateTime;
                    var origin       = state.Events[0];
                    var updateEvent  = new EventModel(origin.Source);
                    var last         = origin.EndTime - origin.StartTime;
                    updateEvent.StartTime = newStartTime;
                    updateEvent.EndTime   = (newStartTime + last).AddSeconds(1);
                    updateEvent.TimeZone  = TimeZoneInfo.Utc;
                    updateEvent.Id        = origin.Id;
                    var calendarAPI     = GraphClientHelper.GetCalendarService(state.APIToken, state.EventSource, ServiceManager.GetGoogleClient());
                    var calendarService = ServiceManager.InitCalendarService(calendarAPI, state.EventSource);
                    var newEvent        = await calendarService.UpdateEventById(updateEvent);

                    var replyMessage = sc.Context.Activity.CreateAdaptiveCardReply(UpdateEventResponses.EventUpdated, newEvent.OnlineMeetingUrl == null ? "Dialogs/Shared/Resources/Cards/CalendarCardNoJoinButton.json" : "Dialogs/Shared/Resources/Cards/CalendarCard.json", newEvent.ToAdaptiveCardData(state.GetUserTimeZone()));
                    await sc.Context.SendActivityAsync(replyMessage);
                }
                else
                {
                    await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(CalendarSharedResponses.ActionEnded));
                }

                state.Clear();
                return(await sc.EndDialogAsync(true));
            }
            catch
            {
                await HandleDialogExceptions(sc);

                throw;
            }
        }
        public async Task <DialogTurnResult> ShowNextEvent(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await Accessor.GetAsync(sc.Context);

                if (string.IsNullOrEmpty(state.APIToken))
                {
                    return(await sc.EndDialogAsync(true));
                }

                var calendarAPI     = GraphClientHelper.GetCalendarService(state.APIToken, state.EventSource, ServiceManager.GetGoogleClient());
                var calendarService = ServiceManager.InitCalendarService(calendarAPI, state.EventSource);

                var eventList = await calendarService.GetUpcomingEvents();

                var nextEventList = new List <EventModel>();
                foreach (var item in eventList)
                {
                    if (item.IsCancelled != true && (nextEventList.Count == 0 || nextEventList[0].StartTime == item.StartTime))
                    {
                        nextEventList.Add(item);
                    }
                }

                if (nextEventList.Count == 0)
                {
                    await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(NextMeetingResponses.ShowNoMeetingMessage));
                }
                else
                {
                    if (nextEventList.Count == 1)
                    {
                        var speakParams = new StringDictionary()
                        {
                            { "EventName", nextEventList[0].Title },
                            { "PeopleCount", nextEventList[0].Attendees.Count.ToString() },
                        };
                        if (nextEventList[0].IsAllDay == true)
                        {
                            speakParams.Add("EventTime", TimeConverter.ConvertUtcToUserTime(nextEventList[0].StartTime, state.GetUserTimeZone()).ToString("MMMM dd all day"));
                        }
                        else
                        {
                            speakParams.Add("EventTime", TimeConverter.ConvertUtcToUserTime(nextEventList[0].StartTime, state.GetUserTimeZone()).ToString("h:mm tt"));
                        }

                        if (string.IsNullOrEmpty(nextEventList[0].Location))
                        {
                            await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(NextMeetingResponses.ShowNextMeetingNoLocationMessage, ResponseBuilder, speakParams));
                        }
                        else
                        {
                            speakParams.Add("Location", nextEventList[0].Location);
                            await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(NextMeetingResponses.ShowNextMeetingMessage, ResponseBuilder, speakParams));
                        }
                    }
                    else
                    {
                        await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(NextMeetingResponses.ShowMultipleNextMeetingMessage));
                    }

                    await ShowMeetingList(sc, nextEventList, true);
                }

                state.Clear();
                return(await sc.EndDialogAsync(true));
            }
            catch
            {
                await HandleDialogExceptions(sc);

                throw;
            }
        }