Created
October 30, 2016 15:43
-
-
Save dgkanatsios/4bb5e8606508f7a83d8ad2188e6ced63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class CustomTextInputDialog : IDialog<string> | |
{ | |
private CustomTextInputDialog() | |
{ } | |
public string InputPrompt { get; private set; } | |
public string WrongInputPrompt { get; private set; } | |
public ICustomInputValidator Validator { get; private set; } | |
public static CustomTextInputDialog CreateCustomTextInputDialog | |
(string inputPrompt, string wrongInputPrompt, ICustomInputValidator validator) | |
{ | |
return new CustomTextInputDialog() | |
{ InputPrompt = inputPrompt, WrongInputPrompt = wrongInputPrompt, Validator = validator }; | |
} | |
public async Task StartAsync(IDialogContext context) | |
{ | |
await context.PostAsync(InputPrompt); | |
context.Wait(InputGiven); | |
} | |
public async Task InputGiven(IDialogContext context, IAwaitable<IMessageActivity> argument) | |
{ | |
var message = await argument; | |
string msg = message.Text.Trim(); | |
if (!Validator.IsValid(msg)) | |
{ | |
await context.PostAsync(WrongInputPrompt); | |
context.Wait(InputGiven); | |
} | |
else | |
context.Done(msg); | |
} | |
} | |
public interface ICustomInputValidator | |
{ | |
bool IsValid(string input); | |
} | |
[Serializable()] | |
public class PhoneCustomInputValidator: ICustomInputValidator | |
{ | |
public bool IsValid(string input) | |
{ | |
input = input.Replace(" ", ""); | |
input = input.Replace("+", ""); | |
input = input.Replace("(", ""); | |
input = input.Replace(")", ""); | |
if (input.Length > 9) | |
{ | |
long number1 = 0; | |
bool canConvert = long.TryParse(input, out number1); | |
return canConvert; | |
} | |
return false; | |
} | |
} | |
[Serializable()] | |
public class EmailCustomInputValidator : ICustomInputValidator | |
{ | |
public bool IsValid(string input) | |
{ | |
var foo = new EmailAddressAttribute(); | |
return foo.IsValid(input); | |
} | |
} | |
[Serializable()] | |
public class TextCustomInputValidator : ICustomInputValidator | |
{ | |
private int MinLength, MaxLength; | |
public TextCustomInputValidator(int minLength, int maxLength) | |
{ | |
MinLength = minLength; | |
MaxLength = maxLength; | |
} | |
public bool IsValid(string input) | |
{ | |
return input.Length >= MinLength && input.Length <= MaxLength; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment