private async Task WaitForToken(IDialogContext context, IAwaitable <object> result) { var activity = await result as Activity; var tokenResponse = activity.ReadTokenResponseContent(); if (tokenResponse != null) { context.Done(tokenResponse.Token); } else { if (!string.IsNullOrEmpty(activity.Text)) { tokenResponse = await context.GetUserTokenAsync(ConnectionName, activity.Text); if (tokenResponse != null) { context.Done(tokenResponse.Token); return; } } await context.PostAsync($"Hmm. Something went wrong trying to sign in. Let's try again."); await SendOAuthCardAsync(context, activity); } }
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> argument) { var message = await argument; //signout flow too if (message.Text.Equals("signout", StringComparison.InvariantCultureIgnoreCase)) { await context.SignOutUserAsync(ConnectionName); await context.PostAsync("You have been signed out."); } else { var token = await context.GetUserTokenAsync(ConnectionName).ConfigureAwait(false); if (token != null) { //Send to other dialogs , token is there await context.PostAsync("Hello, This is first dialog after authetication success!"); context.Wait(MessageReceivedAsync); } else { await SendOAuthCardAsync(context, (Activity)context.Activity); } } }
private async Task WaitForToken(IDialogContext context, IAwaitable <object> result) { var activity = await result as Activity; var tokenResponse = activity.ReadTokenResponseContent(); if (tokenResponse != null) { await this.SiteRequestDialogAsync(context); } else { if (!string.IsNullOrEmpty(activity.Text)) { tokenResponse = await context.GetUserTokenAsync(ApplicationSettings.ConnectionName, activity.Text); if (tokenResponse != null) { await this.SiteRequestDialogAsync(context); } } else { await context.PostAsync($"Hmm. Something went wrong. Let's try again."); await SendOAuthCardAsync(context, activity); } } }
public async Task Default(IDialogContext context, IActivity activity) { // The Azure Bot Service sends a six digit magic code and ask the user to type this into the chat window // so we need to send this code to the Azure Bot Service if (activity is IInvokeActivity invokeActivity) { if (invokeActivity.IsSigninStateVerificationQuery()) { var data = invokeActivity.GetSigninStateVerificationQueryData(); if (string.IsNullOrEmpty(data?.State)) { return; } var connectionName = ConfigurationManager.AppSettings["BotOAuthConnectionName"]; var tokenResponse = await context.GetUserTokenAsync(connectionName, data.State); if (tokenResponse != null) { await context.PostAsync("You have signed in successfully. Please type command one more time."); } return; } } var query = string.Empty; if (activity is Activity act && act.Text.HasValue()) { query = $" '{act.Text.Trim()}'"; } var message = $"Sorry, I didn't understand{query}. Type {BotCommands.HelpDialogCommand} to explore commands."; await context.PostAsync(message); }
private async Task WaitForToken(IDialogContext context, IAwaitable <object> result) { var activity = await result as Activity; var tokenResponse = activity.ReadTokenResponseContent(); string verificationCode = null; if (tokenResponse != null) { context.Done(new GetTokenResponse() { Token = tokenResponse.Token }); await context.PostAsync("Login Success! How may I help you?"); return; } else if (activity.IsTeamsVerificationInvoke()) { JObject value = activity.Value as JObject; if (value != null) { verificationCode = (string)(value["state"]); } } else if (!string.IsNullOrEmpty(activity.Text)) { verificationCode = activity.Text; } tokenResponse = await context.GetUserTokenAsync(ConnectionName, verificationCode); if (tokenResponse != null) { await context.PostAsync("Login Success!"); context.Done(new GetTokenResponse() { Token = tokenResponse.Token }); return; } // decide whether to retry or not if (_reties > 0) { _reties--; await context.PostAsync(_retryMessage); await SendOAuthCardAsync(context, activity); } else { context.Done(new GetTokenResponse() { NonTokenResponse = activity.Text }); return; } }
private async Task WaitForToken(IDialogContext context, IAwaitable <object> result) { var activity = await result as Activity; var tokenResponse = activity.ReadTokenResponseContent(); if (tokenResponse != null) { // Use the token to do exciting things! await AddUserToDatabase(context, tokenResponse); context.Wait(MessageReceivedAsync); } else { // Get the Activity Message as well as activity.value in case of Auto closing of pop-up string input = activity.Type == ActivityTypes.Message ? Microsoft.Bot.Connector.Teams.ActivityExtensions.GetTextWithoutMentions(activity) : ((dynamic)(activity.Value)).state.ToString(); if (!string.IsNullOrEmpty(input)) { tokenResponse = await context.GetUserTokenAsync(ApplicationSettings.ConnectionName, input.Trim()); if (tokenResponse != null) { try { await AddUserToDatabase(context, tokenResponse); } catch (Exception ex) { ErrorLogService.LogError(ex); } context.Wait(MessageReceivedAsync); return; } } await context.PostAsync($"Hmm. Something went wrong. Please initiate the SignIn again. Try sending help."); //await SendOAuthCardAsync(context, activity); // await MessageReceivedAsync(context, result); context.Wait(MessageReceivedAsync); } }
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); }
public async Task StartAsync(IDialogContext context) { // First ask Bot Service if it already has a token for this user var token = await context.GetUserTokenAsync(ConnectionName).ConfigureAwait(false); if (token != null) { context.Done(token.Token); } else { // If Bot Service does not have a token, send an OAuth card to sign in await SendOAuthCardAsync(context, (Activity)context.Activity); } }
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> argument) { TokenResponse tokenResponse = await context.GetUserTokenAsync(ConnectionName); if (tokenResponse != null && !string.IsNullOrEmpty(tokenResponse.Token)) { if (!options.Contains(Selection.SignOut)) { options.Add(Selection.SignOut); optionDescription.Add(SignOut); options.Add(Selection.PasswordResetOption); optionDescription.Add(PasswordResetOption); } if (options.Contains(Selection.SignIn)) { options.Remove(Selection.SignIn); optionDescription.Remove(SignIn); } } else { if (!options.Contains(Selection.SignIn)) { options.Add(Selection.SignIn); optionDescription.Add(SignIn); } if (options.Contains(Selection.SignOut)) { options.Remove(Selection.SignOut); optionDescription.Remove(SignOut); options.Remove(Selection.PasswordResetOption); optionDescription.Remove(PasswordResetOption); } } PromptDialog.Choice <Selection>(context, ReceivedOperationChoice, options, "Which selection do you want?", descriptions: optionDescription, promptStyle: PromptStyle.Auto); //context.Wait(MessageReceivedOperationChoice); }
public async Task StartAsync(IDialogContext context) { // First ask Bot Service if it already has a token for this user var token = await context.GetUserTokenAsync(_connectionName); if (token != null) { context.Done(new GetTokenResponse() { Token = token.Token }); } else { // If Bot Service does not have a token, send an OAuth card to sign in await SendOAuthLinkAsync(context, (Activity)context.Activity); } }
private async Task WaitForToken(IDialogContext context, IAwaitable <object> result) { var activity = await result as Activity; var tokenResponse = activity.ReadTokenResponseContent(); if (tokenResponse != null) { // Use the token to do exciting things! } else { // Get the Activity Message as well as activity.value in case of Auto closing of pop-up string input = activity.Type == ActivityTypes.Message ? Microsoft.Bot.Connector.Teams.ActivityExtensions.GetTextWithoutMentions(activity) : ((dynamic)(activity.Value)).state.ToString(); if (!string.IsNullOrEmpty(input)) { tokenResponse = await context.GetUserTokenAsync(ApplicationSettings.ConnectionName, input.Trim()); if (tokenResponse != null) { context.ConversationData.SetValue <string>(GetEmailKey(context.Activity), tokenResponse.ToString()); await context.PostAsync($"Your sign in was successful.Please check the commands to see what i can do!!"); //string url = await getSigninUrl(activity); var reply = context.MakeMessage(); List <Attachment> res = Helper.CardHelper.WelcomeCard(); for (int i = 0; i < res.Count(); i++) { reply.Attachments.Add(res.ElementAt(i)); } reply.AttachmentLayout = AttachmentLayoutTypes.Carousel; await context.PostAsync(reply); context.Wait(MessageReceivedAsync); return; } } await context.PostAsync($"Hmm. Something went wrong. Please initiate the SignIn again. Try sending help."); context.Wait(MessageReceivedAsync); } }
private async Task WaitForToken(IDialogContext context, IAwaitable <object> result) { var activity = await result as Activity; var tokenResponse = activity.ReadTokenResponseContent(); if (tokenResponse != null) { // Use the token to do exciting things! } else { // Get the Activity Message as well as activity.value in case of Auto closing of pop-up string input = activity.Type == ActivityTypes.Message ? Connector.Teams.ActivityExtensions.GetTextWithoutMentions(activity) : ((dynamic)(activity.Value)).state.ToString(); if (!string.IsNullOrEmpty(input)) { tokenResponse = await context.GetUserTokenAsync(ConnectionName, input.Trim()); if (tokenResponse != null && tokenResponse.Token != null) { try { await context.PostAsync($"You are successfully signed in. Now, you can use create team command."); } catch (Exception ex) { Console.WriteLine(ex); } } else { await context.PostAsync($"Hmm. Something went wrong. Let's try again."); } context.Wait(MessageReceivedAsync); return; } await context.PostAsync($"Hmm. Something went wrong. Let's try again."); await SendOAuthCardAsync(context, activity); } }
private async Task UpdateTeam(IDialogContext context, Activity activity) { var token = await context.GetUserTokenAsync(ConnectionName).ConfigureAwait(false); if (token != null && token.Token != null) { Activity reply = activity.CreateReply(); ThumbnailCard card = GetThumbnailForTeamsAction(); card.Title = "Update existing team"; card.Subtitle = "Automate adding members/channels by sharing team details"; reply.TextFormat = TextFormatTypes.Xml; reply.Attachments.Add(card.ToAttachment()); await context.PostAsync(reply); } else { await SendOAuthCardAsync(context, activity); } }
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> argument) { var activity = await argument as Activity; if (activity.Text == null) { activity.Text = string.Empty; } var message = Connector.Teams.ActivityExtensions.GetTextWithoutMentions(activity).ToLowerInvariant().Trim(); if (message.Equals("help") || message.Equals("hi") || message.Equals("hello")) { await SendHelpMessage(context, activity); } else { // Check for file upload. if (activity.Attachments != null && activity.Attachments.Any()) { var token = await context.GetUserTokenAsync(ConnectionName).ConfigureAwait(false); if (token == null || token.Token == null) { await SendOAuthCardAsync(context, activity); return; } try { var attachment = activity.Attachments.First(); await HandleExcelAttachement(context, activity, token, attachment); } catch (Exception ex) { await context.PostAsync(ex.ToString()); } } else { // All the commands can be executed... switch (message) { case "create team": context.UserData.SetValue(LastAction, message); await CreateTeam(context, activity); break; case "add members": case "add channels": case "add members/channels": context.UserData.SetValue(LastAction, message); await UpdateTeam(context, activity); break; case "logout": await Signout(context); break; default: await context.PostAsync("Please check type help commands to know options."); break; } } } }
private async Task SendArchiveTeamCard(IDialogContext context, Activity activity) { var token = await context.GetUserTokenAsync(ConnectionName).ConfigureAwait(false); if (token != null && token.Token != null) { GraphAPIHelper helper = new GraphAPIHelper(); var allTeams = await helper.GetAllTeams(token.Token); var getAllNonArchivedTeams = await helper.GetAllNonArchivedTeams(token.Token, allTeams); var durations = new List <AdaptiveChoice>(); foreach (var team in getAllNonArchivedTeams) { durations.Add(new AdaptiveChoice() { Title = team.displayName, Value = team.id }); } var Card = new AdaptiveCard() { Body = new List <AdaptiveElement>() { new AdaptiveTextBlock() { Text = "Archive Team", Size = AdaptiveTextSize.Large, Weight = AdaptiveTextWeight.Bolder }, new AdaptiveTextBlock() { Text = "Please select teams to archive:" }, new AdaptiveChoiceSetInput() { Id = "id", Choices = durations, IsMultiSelect = true, Style = AdaptiveChoiceInputStyle.Compact, IsRequired = true } }, Actions = new List <AdaptiveAction>() { new AdaptiveSubmitAction() { Title = "Archive Team", } } }; var attachment = new Attachment() { ContentType = AdaptiveCard.ContentType, Content = Card }; Activity reply = activity.CreateReply(); reply.Attachments.Add(attachment); await context.PostAsync(reply); } else { await SendOAuthCardAsync(context, activity); } }
private async Task HandleActions(IDialogContext context, Activity activity) { string type = string.Empty; var details = JsonConvert.DeserializeObject <ResponseData>(activity.Value.ToString()); if (details.id == null) { await context.PostAsync("Please select at least one team to archive."); return; } var teamsToArchive = details.id.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); var token = await context.GetUserTokenAsync(ConnectionName).ConfigureAwait(false); if (token == null || token.Token == null) { await SendOAuthCardAsync(context, activity); return; } GraphAPIHelper helper = new GraphAPIHelper(); var successCount = 0; foreach (var teamId in teamsToArchive) { if (!await helper.ArchiveTeamAsync(token.Token, teamId)) { await context.PostAsync("Failed to archive team with teamId: " + teamId); } else { successCount++; } } ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); var reply = activity.CreateReply(); var Card = new AdaptiveCard() { Body = new List <AdaptiveElement>() { new AdaptiveTextBlock() { Text = "Archive Team", Size = AdaptiveTextSize.Large, Weight = AdaptiveTextWeight.Bolder }, new AdaptiveTextBlock() { Text = $"Archive complete. Successful count: {successCount}" }, } }; var attachment = new Attachment() { ContentType = AdaptiveCard.ContentType, Content = Card }; reply.Attachments.Add(attachment); await connector.Conversations.UpdateActivityAsync(activity.Conversation.Id, activity.ReplyToId, reply); // await context.Update(reply); }