public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default)
        {
            var text      = dc.Context.GetTextWithoutCommand(BotCommands.NewTeamDialog);
            var positions = await _positionService.Search(text, 15, cancellationToken);

            if (positions.Count == 1)
            {
                var connectionName = _appSettings.OAuthConnectionName;
                var token          = await((IUserTokenProvider)dc.Context.Adapter)
                                     .GetUserTokenAsync(dc.Context, connectionName, null, cancellationToken);
                var(team, displayName) = await _graphApiService.CreateNewTeamForPosition(positions[0], token.Token, cancellationToken);

                await dc.Context.SendActivityAsync($"[Team {displayName}]({team.WebUrl}) has been created.", cancellationToken : cancellationToken);
            }
            else
            {
                var positionsTemplate = new PositionTemplateModel
                {
                    Items         = positions,
                    NoItemsLabel  = "You don't have such open positions.",
                    BotCommand    = BotCommands.NewTeamDialog,
                    ListCardTitle = "I found following positions:",
                };

                await _positionsTemplate.ReplyWith(dc.Context, TemplateConstants.PositionAsAdaptiveCardWithMultipleItems, positionsTemplate);
            }

            return(await dc.EndDialogAsync(cancellationToken : cancellationToken));
        }
Exemplo n.º 2
0
        public async Task StartAsync(IDialogContext context)
        {
            var reply = context.MakeMessage();

            reply.Attachments = new List <Attachment>();

            var text      = context.Activity.GetTextWithoutCommand(BotCommands.NewTeamDialog);
            var positions = await _positionService.Search(text, 15, context.CancellationToken);

            if (positions.Any())
            {
                if (positions.Count == 1)
                {
                    //await context.SignOutUserAsync(_connectionName);
                    var token = await context.GetUserTokenAsync(_connectionName);

                    if (string.IsNullOrEmpty(token?.Token))
                    {
                        reply = await context.Activity.CreateOAuthReplyAsync(_connectionName, "Please sign in to proceed.", "Sign In");
                    }
                    else
                    {
                        var team = await _graphApiService.CreateNewTeamForPosition(positions[0], token.Token);

                        reply.Text = $"[Team {team.DisplayName}]({team.WebUrl}) has been created.";
                    }
                }
                else
                {
                    var cardListItems = _mapper.Map <List <CardListItem> >(positions,
                                                                           opt => opt.Items["botCommand"] = BotCommands.NewTeamDialog);

                    reply.Attachments.Add(new Attachment
                    {
                        ContentType = ListCard.ContentType,
                        Content     = new ListCard
                        {
                            Title = "I found following positions:",
                            Items = cardListItems
                        }
                    });
                }
            }
            else
            {
                reply.Text = "You don't have such open positions.";
            }

            await context.PostAsync(reply);

            context.Done(string.Empty);
        }