public async Task DisableUser(IDialogContext context, LuisResult result) { var isAuthenticated = await context.IsAuthenticated(); EntityRecommendation username; if (!isAuthenticated) { context.SetUnauthorizedMessageText(result.Query); await context.Forward(new AuthenticationDialog(), this.ResumeAfterAuth, context.MakeMessage(), CancellationToken.None); } else if (!result.TryFindEntity(Entities.Username, out username)) { await context.PostAsync("I'm sorry I really don't have a clue which user you mean."); } else if (context.IsAdministrator()) { var user = await Sitecore(context.GetProfile().AccessToken).GetProfile(username.Entity); if (user == null) { await context.PostAsync($"There doesn't exist a user with username '{username.Entity}'"); } else { var profile = await Sitecore(context.GetProfile().AccessToken).GetProfile(username.Entity); if (!profile.Success) { context.PrivateConversationData.SetValue(ContextConstants.ActionOnUserKey, username.Entity); PromptDialog.Confirm(context, DisableUser_Callback, $"Are you sure you would like to disable {user.FullName} ({username.Entity})", "Please, make a choice."); } } } }
private async Task ResumeAfterAuth(IDialogContext context, IAwaitable <string> result) { var message = await result; var profile = context.GetProfile(); await context.PostAsync($"Welcome back {profile.FullName}!"); context.Wait(MessageReceived); }
private async Task MessageReceived(IDialogContext context, IAwaitable <IMessageActivity> argument) { var message = await argument; var userprofile = context.GetProfile(); if (userprofile == null || string.IsNullOrWhiteSpace(userprofile.AccessToken)) { if (message.Text.StartsWith("token:")) { var token = message.Text.Remove(0, "token:".Length); var validtoken = await SitecoreAuthenticationAPI.Instance().ValidateAccessToken(token); if (validtoken) { var profile = await SitecoreUserManagementAPI.Instance(token).GetProfile(); userprofile = new Models.UserProfile { AccessToken = token, FullName = profile.FullName, Login = profile.UserName, IsAdministrator = profile.IsAdministrator, Roles = profile.Roles, ApplicationInsights = profile.ApplicationInsights }; context.SetProfile(userprofile); context.Done(context.GetUnauthorizedMessageText()); return; } } await context.PostAsync("Hi I'm Sitecore Bot! I don't really know who you are, so you have to show me some proof!"); await LogIn(context); context.Wait(MessageReceived); return; } var authenticated = await userprofile.IsAuthenticated(); if (!authenticated) { userprofile.AccessToken = null; context.SetProfile(userprofile); await context.PostAsync("Okay, I don't really remember who you were, could you please identify yourself?"); await LogIn(context); context.Wait(MessageReceived); return; } return; }
public async Task SignOut(IDialogContext context, LuisResult result) { var isAuthenticated = await context.IsAuthenticated(); if (!isAuthenticated) { await context.PostAsync("Uhhhh, okay! I already have forgotten you!"); } else { var profile = context.GetProfile(); PromptDialog.Confirm(context, SignOut_Callback, $"{profile.FullName}, are you sure you would like to signout?", "Please, make a choice."); } }
private async Task DeleteUser_Callback(IDialogContext context, IAwaitable <bool> result) { var confirmed = await result; string username; if (confirmed && context.PrivateConversationData.TryGetValue(ContextConstants.ActionOnUserKey, out username) && !string.IsNullOrWhiteSpace(username)) { var user = await Sitecore(context.GetProfile().AccessToken).GetProfile(username); var response = await Sitecore(context.GetProfile().AccessToken).DeleteUser(username); if (response.Success) { await context.PostAsync($"{user.FullName} ({username}) has been deleted."); } else { await context.PostAsync($"{user.FullName} ({username}) couldn't be deleted because you {response.Message}."); } } context.Wait(MessageReceived); }
private async Task CreateUser_Callback(IDialogContext context, IAwaitable <Models.Forms.CreateUser> result) { var create = await result; var response = await Sitecore(context.GetProfile().AccessToken).CreateUser(create); if (response.Success) { await context.PostAsync($"{create.FullName} ({create.UserName}) has been created."); } else { await context.PostAsync($"{create.UserName} couldn't be created because {response.Message}."); } context.Wait(MessageReceived); }
public async Task Greeting(IDialogContext context, LuisResult result) { var isAuthenticated = await context.IsAuthenticated(); if (isAuthenticated) { var profile = context.GetProfile(); await context.PostAsync($"{profile.FullName}, want some coffee? Hold on!"); await context.PostAsync(context.CreateTypingActivity()); Thread.Sleep(1500); } int random = new Random(DateTime.Now.Second).Next(0, 4); switch (random) { case 1: await context.PostAsync($"Yes, tell me! What are you looking for?"); break; case 2: await context.PostAsync($"Hello from the other side!"); break; case 3: await context.PostAsync($"Bots are just \"fake\" technology!"); break; case 4: await context.PostAsync($"Peace man!"); break; default: await context.PostAsync($"Yes, I'm listening..."); break; } }
public async Task AppInsightsTraces(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)) { int top = 25; EntityRecommendation entity; if (result.TryFindEntity(Constants.Entities.Top, out entity)) { int.TryParse(entity.Entity, out top); } await context.PostAsync($"Hold on! I'm pulling the last {top} traces from the cloud!"); await context.PostAsync(context.CreateTypingActivity()); var profile = context.GetProfile(); var service = new Connector.AppInsights.ApplicationInsightsService(profile.ApplicationInsights.ApplicationId, profile.ApplicationInsights.ApiKey); var traces = service.GetTraces(new TimeSpan(1, 0, 0), top).ToList(); StringBuilder sb = new StringBuilder(); traces.ForEach(trace => { sb.AppendLine($"{trace.timestamp} {trace.trace.message} \n"); }); await context.PostAsync(sb.ToString()); } }
public async static Task <bool> IsAuthenticated(this IDialogContext context) { var profile = context.GetProfile(); return(await profile.IsAuthenticated()); }
/// <summary> /// User is administrator /// </summary> /// <param name="context"></param> /// <returns></returns> public static bool IsAdministrator(this IDialogContext context) { return(context.GetProfile().IsAdministrator); }
/// <summary> /// Returns true if the user is in role /// </summary> /// <param name="context"></param> /// <param name="role"></param> /// <returns></returns> public static bool IsInRole(this IDialogContext context, string role) { return(context.GetProfile().Roles.Contains(role)); }
/// <summary> /// Get the access token for API access /// </summary> /// <param name="context"></param> /// <returns></returns> public static string AccessToken(this IDialogContext context) { return(context.GetProfile().AccessToken); }