Пример #1
0
        private async Task <DialogTurnResult> DisplayContextInfoStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            if (stepContext.Result != null)
            {
                var tokenResponse = stepContext.Result as TokenResponse;
                if (tokenResponse?.Token != null)
                {
                    var teamsContext = stepContext.Context.TurnState.Get <ITeamsContext>();

                    if (teamsContext != null)          // the bot is used inside MS Teams
                    {
                        if (teamsContext.Team != null) // inside team
                        {
                            await stepContext.Context.SendActivityAsync(MessageFactory.Text("We're in MS Teams, inside a Team! :)"), cancellationToken);

                            var team        = teamsContext.Team;
                            var teamDetails = await teamsContext.Operations.FetchTeamDetailsWithHttpMessagesAsync(team.Id);

                            var token      = tokenResponse.Token;
                            var aadGroupId = teamDetails.Body.AadGroupId;

                            var siteInfo = await MSGraphHelper.GetSiteContext(tokenResponse, aadGroupId);

                            await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Site Id: {siteInfo.Id}, Site Title: {siteInfo.DisplayName}, Site Url: {siteInfo.WebUrl}"), cancellationToken).ConfigureAwait(false);

                            return(await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("Would you like to do? (type 'agenda')") }, cancellationToken));
                        }
                        else // private or group chat
                        {
                            await stepContext.Context.SendActivityAsync(MessageFactory.Text($"We're in MS Teams but not in Team"), cancellationToken).ConfigureAwait(false);

                            return(await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("Would you like to do? (type 'agenda')") }, cancellationToken));
                        }
                    }
                    else // outside MS Teams
                    {
                        await stepContext.Context.SendActivityAsync(MessageFactory.Text("We're not in MS Teams context"), cancellationToken).ConfigureAwait(false);
                    }
                }
            }

            return(await stepContext.EndDialogAsync());
        }
Пример #2
0
        private async Task <DialogTurnResult> ProcessStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            if (stepContext.Result != null)
            {
                // We do not need to store the token in the bot. When we need the token we can
                // send another prompt. If the token is valid the user will not need to log back in.
                // The token will be available in the Result property of the task.
                var tokenResponse = stepContext.Result as TokenResponse;

                var teamsContext = stepContext.Context.TurnState.Get <ITeamsContext>();


                // If we have the token use the user is authenticated so we may use it to make API calls.
                if (tokenResponse?.Token != null)
                {
                    var temp = ((string)stepContext.Values["command"] ?? string.Empty).ToLowerInvariant();

                    var parts = teamsContext.GetActivityTextWithoutMentions().Split(' ');
                    //Regex.Replace(HttpUtility.HtmlDecode(temp), "<.*?>", string.Empty).Split(' ');

                    var command = parts[0];

                    if (command == "agenda")
                    {
                        var res = MSGraphHelper.GetFreeTime(tokenResponse, "");

                        IMessageActivity reply = null;

                        if (res.MeetingTimeSuggestions.Any())
                        {
                            var count = res.MeetingTimeSuggestions.Count();
                            if (count > 3)
                            {
                                count = 3;
                            }

                            reply = MessageFactory.Attachment(new List <Microsoft.Bot.Schema.Attachment>());
                            reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;

                            for (var i = 0; i < count; i++)
                            {
                                var slot = res.MeetingTimeSuggestions.ToList()[i];


                                var card = new HeroCard(
                                    $"Proposal number #{i}",
                                    $"{slot.SuggestionReason}",
                                    $"{DateTime.Parse(slot.MeetingTimeSlot.Start.DateTime).ToString("dd/MM/yyyy H:mm")} --> {DateTime.Parse(slot.MeetingTimeSlot.End.DateTime).ToString("dd/MM/yyyy H:mm")}");
                                reply.Attachments.Add(card.ToAttachment());
                            }
                        }
                        else
                        {
                            reply = MessageFactory.Text("Unable to find any recent unread mail.");
                        }

                        await stepContext.Context.SendActivityAsync(reply, cancellationToken);
                    }
                    else
                    {
                        await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Your token is: {tokenResponse?.Token}"), cancellationToken);
                    }
                }
            }
            else
            {
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("We couldn't log you in. Please try again later."), cancellationToken);
            }

            return(await stepContext.EndDialogAsync(cancellationToken : cancellationToken));
        }