public async Task ListIndexes(IDialogContext context, LuisResult result) { var isAuthenticated = await context.IsAuthenticated(); if (!isAuthenticated) { context.SetUnauthorizedMessageText(result.Query); await context.Forward(new AuthenticationDialog(), this.ResumeAfterAuth, context.MakeMessage(), CancellationToken.None); } else if (context.IsAdministrator()) { var response = await SitecoreIndexAPI.Instance(context.AccessToken()).List(); StringBuilder sb = new StringBuilder(); sb.AppendLine("I've got the following indexes for you \n"); response.ToList().ForEach(index => { sb.AppendLine($"**{index.IndexName}** \n"); sb.AppendLine($"*Documents: {index.NumberOfDocuments}* \n"); sb.AppendLine($"*Rebuild time: {index.IndexRebuildTime}* \n"); sb.AppendLine($" \n"); sb.AppendLine($" \n"); }); var reply = context.MakeMessage(); reply.Text = sb.ToString(); await context.PostAsync(reply); } }
public async Task CreateUser(IDialogContext context, LuisResult result) { var isAuthenticated = await context.IsAuthenticated(); if (!isAuthenticated) { context.SetUnauthorizedMessageText(result.Query); await context.Forward(new AuthenticationDialog(), this.ResumeAfterAuth, context.MakeMessage(), CancellationToken.None); } else if (context.IsAdministrator()) { var model = new Models.Forms.CreateUser(); EntityRecommendation emailaddress; if (result.TryFindEntity(Entities.BuiltIn_Email, out emailaddress)) { model.EmailAddress = emailaddress.Entity.ToLowerInvariant(); var suggestedUsername = model.EmailAddress.Split('@')[0]; var exists = await Sitecore(context.AccessToken()).UserExists(suggestedUsername); if (!exists) { model.UserName = suggestedUsername; } } var createUserForm = new FormDialog <Models.Forms.CreateUser>(model, Models.Forms.CreateUser.BuildForm, FormOptions.PromptInStart); context.Call(createUserForm, CreateUser_Callback); } }
public async Task UnSubscribeFromPublishEvent(IDialogContext context, LuisResult result) { var isAuthenticated = await context.IsAuthenticated(); if (!isAuthenticated) { context.SetUnauthorizedMessageText(result.Query); await context.Forward(new AuthenticationDialog(), this.ResumeAfterAuth, context.MakeMessage(), CancellationToken.None); } else if (context.IsAdministrator()) { var response = await SitecorePublishAPI.Instance(context.AccessToken()).UnSubscribe(); NotificationController.UnSubscribeToPublishUpdates(context); await context.PostAsync($"Unsubscribed from publish events"); } }
public async Task SmartPublish(IDialogContext context, LuisResult result) { var isAuthenticated = await context.IsAuthenticated(); if (!isAuthenticated) { context.SetUnauthorizedMessageText(result.Query); await context.Forward(new AuthenticationDialog(), this.ResumeAfterAuth, context.MakeMessage(), CancellationToken.None); } else if (context.IsAdministrator()) { var response = await SitecorePublishAPI.Instance(context.AccessToken()).SmartPublish(); if (!NotificationController.SubscribedPublishConversations.ContainsKey(context.Activity.Conversation.Id)) { await context.PostAsync($"Alright, you have just started a smart publish on {response.ProducedByInstanceName}"); await context.PostAsync("If you would like to receive a notification for publish event just ask me!"); } } }
public async Task RebuildIndex(IDialogContext context, LuisResult result) { var isAuthenticated = await context.IsAuthenticated(); if (!isAuthenticated) { context.SetUnauthorizedMessageText(result.Query); await context.Forward(new AuthenticationDialog(), this.ResumeAfterAuth, context.MakeMessage(), CancellationToken.None); } else if (context.IsAdministrator()) { EntityRecommendation index; result.TryFindEntity(Entities.Index_Name, out index); if (index == null || string.IsNullOrWhiteSpace(index.Entity)) { await context.PostAsync($"I'm sorry, I wasn't able to recognize the index you wanted to rebuild."); } else { var response = await SitecoreIndexAPI.Instance(context.AccessToken()).Rebuild(index.Entity.Replace(" ", string.Empty)); if (!NotificationController.SubscribedIndexingConversations.ContainsKey(context.Activity.Conversation.Id)) { if (response.IndexRebuildMilliseconds == 0) { await context.PostAsync($"Rebuilding for {response.IndexName} started. I don't know when it's ready, it hasn't been indexed before"); } else { await context.PostAsync($"Rebuilding for {response.IndexName} started. Should be ready in approximately {response.IndexRebuildTime} :)"); } await context.PostAsync("If you would like updates on index rebuilds just ask me!"); } } } }
public async Task XDBSessions(IDialogContext context, LuisResult result) { var isAuthenticated = await context.IsAuthenticated(); if (!isAuthenticated) { context.SetUnauthorizedMessageText(result.Query); await context.Forward(new AuthenticationDialog(), this.ResumeAfterAuth, context.MakeMessage(), CancellationToken.None); } else if (context.IsAdministrator() || context.IsInRole(RoleNames.BotAnalytics)) { EntityRecommendation period; Chronic.Span span = null; if (result.TryFindEntity(Constants.Entities.BuiltIn_DateTime, out period)) { var parser = new Chronic.Parser(); foreach (var resolution in period.Resolution) { if (new Regex(@"^(201[0-9])$").IsMatch(resolution.Value)) { try { var year = int.Parse(resolution.Value.Substring(0, 4)); span = new Chronic.Span(new DateTime(year, 1, 1), new DateTime(year, 1, 1).AddYears(1)); } catch { } } else { span = parser.Parse(resolution.Value); } } if (span == null && period.Resolution.Count > 0) { var resolution = period.Resolution.FirstOrDefault(); if (new Regex(@"^(201[0-9]\-W([0-5][0-9]))$").IsMatch(resolution.Value)) { try { var year = int.Parse(resolution.Value.Substring(0, 4)); var week = int.Parse(resolution.Value.Substring(6)); span = new Chronic.Span(DateHelpers.FirstDateOfWeek(year, week), DateHelpers.LastDateOfWeek(year, week)); } catch { } } else if (new Regex(@"^(201[0-9]\-([0-1][0-9]))$").IsMatch(resolution.Value)) { try { var year = int.Parse(resolution.Value.Substring(0, 4)); var month = int.Parse(resolution.Value.Substring(5)); span = new Chronic.Span(new DateTime(year, month, 1), new DateTime(year, month, 1).AddMonths(1)); } catch { } } } } if (span == null) { span = new Chronic.Span(DateTime.Now, DateTime.Now.AddDays(1)); } var response = await SitecoreXdbAPI.Instance(context.AccessToken()).Interactions(span.Start, span.End); if (response.Count > 0) { var contacts = response.Select(i => i.ContactId).Distinct(); await context.PostAsync($"Between {span.Start.Value.ToString("dd-MM-yyyy")} and {span.End.Value.ToString("dd-MM-yyyy")} we've had a total of {contacts.Count()} unique contacts in {response.Count} interactions. Let's look at some facts."); StringBuilder sb = new StringBuilder(); sb.AppendLine($"The average value was {Math.Round((double)response.Sum(i => i.Value) / response.Count, 1, MidpointRounding.ToEven)} \n"); sb.AppendLine($"The average number of pages visited was {Math.Round((double)response.Sum(i => i.VisitPageCount) / response.Count, 1, MidpointRounding.ToEven)} \n"); if (response.Any(i => i.CampaignId.HasValue)) { sb.AppendLine($"{response.Count(i => i.CampaignId.HasValue)} triggered a campaign. \n"); } else { sb.AppendLine($"None of them triggered a campaign. \n"); } await context.PostAsync(sb.ToString()); } else { await context.PostAsync($"I'm very sorry to say that you haven't had any interactions between {span.Start} and {span.End}"); } } }
public async Task ListUser(IDialogContext context, LuisResult result) { var isAuthenticated = await context.IsAuthenticated(); if (!isAuthenticated) { context.SetUnauthorizedMessageText(result.Query); await context.Forward(new AuthenticationDialog(), this.ResumeAfterAuth, context.MakeMessage(), CancellationToken.None); } else if (context.IsAdministrator()) { EntityRecommendation domain; EntityRecommendation role; result.TryFindEntity(Entities.Domain, out domain); result.TryFindEntity(Entities.Role, out role); await context.PostAsync(context.CreateTypingActivity()); var users = await SitecoreUserManagementAPI.Instance(context.AccessToken()).GetUsers(domain == null ? null : domain.Entity, role == null ? null : role.Entity); StringBuilder sb = new StringBuilder(); if (users.Count == 0) { if (domain != null && role != null) { sb.AppendLine($"I'm sorry, we don't have {role.Entity} users in the {domain.Entity} domain \n \n"); } else if (domain != null && role == null) { sb.AppendLine($"I'm sorry, we don't have {domain.Entity} domain \n \n"); } else { sb.AppendLine($"I'm sorry, I haven't found any Sitecore users \n \n"); } } else { if (domain != null && role != null) { sb.AppendLine($"Here is the list of {role.Entity} users in the {domain.Entity} domain \n \n"); } else if (domain != null && role == null) { sb.AppendLine($"Here is the list of users in the {domain.Entity} domain \n \n"); } else { sb.AppendLine($"Here is the list of Sitecore users \n \n"); } users.ForEach(user => { if (user.IsOnline) { sb.AppendLine($"{user.FullName} ({user.UserName}) is currently online! \n"); } else { sb.AppendLine( user.LastActivity.HasValue ? $"{user.FullName} ({user.UserName}) last activity {user.LastActivity.Value} \n" : $"{user.FullName} ({user.UserName}) \n"); } }); } await context.PostAsync(sb.ToString()); } }