/// <summary> /// Manipulate JSON file /// </summary> /// <param name="Query"></param> /// <returns>LuisIntentObject</returns> private static async Task <LuisIntentObject> GetEntityFromLUIS(string userInput) { string Query = Uri.EscapeDataString(userInput); LuisIntentObject luisObject = new LuisIntentObject(); HttpClient client = new HttpClient(); string luisRequestUrl = "https://api.projectoxford.ai/luis/v2.0/apps/8f510ad0-4e70-4b25-9fde-d136dba07d7f?subscription-key=719459cce3b34a8fbf4aeee23cde4be0&q="; string luisResponse = await client.GetStringAsync(new Uri(luisRequestUrl + Query)); //deserialize LuisIntentObject luisIntentObject; luisIntentObject = JsonConvert.DeserializeObject <LuisIntentObject>(luisResponse); return(luisIntentObject); }
/// <summary> /// POST: api/Messages /// Receive a message from a user and reply to it /// </summary> public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { if (activity.Type == ActivityTypes.Message) { ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); var userInput = activity.Text; string replyToUser = $"Welcome to Cortoso Bank"; /// --- Store client information ---------------- StateClient stateClient = activity.GetStateClient(); BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id); userLoggedIn = userData.GetProperty <bool>("userLoggedIn"); userLogin = userData.GetProperty <bool>("userLogin"); createAccount = userData.GetProperty <bool>("createAccount"); deleteAccount = userData.GetProperty <bool>("deleteAccount"); userAccountNo = userData.GetProperty <string>("userAccountNo") ?? ""; clientDetails = userData.GetProperty <Customer>("clientDetails") ?? new Customer(); newUserInformation = userData.GetProperty <List <object> >("newUserInformation") ?? new List <object>(); loginInformation = userData.GetProperty <List <string> >("loginInformation") ?? new List <string>(); /// --- Clear state ---------------------- if (userInput.ToLower().Equals("clear") || userInput.ToLower().Equals("log out")) { replyToUser = "******"; userData.SetProperty <bool>("userLogin", false); userData.SetProperty <bool>("userLoggedIn", false); userData.SetProperty <bool>("createAccount", false); userData.SetProperty <bool>("deleteAccount", false); userData.SetProperty <string>("userAccountNo", ""); userData.SetProperty <Customer>("clientDetails", new Customer()); userData.SetProperty <List <object> >("newUserInformation", new List <object>()); userData.SetProperty <List <string> >("loginInformation", new List <string>()); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); await stateClient.BotState.DeleteStateForUserAsync(activity.ChannelId, activity.From.Id); // ## Introduction Page ## Activity replyToConversation = activity.CreateReply(replyToUser); replyToConversation.Recipient = activity.From; replyToConversation.Type = "message"; replyToConversation.Attachments = new List <Attachment>(); // CardButtons var actions = new List <CardAction>(); for (int i = 0; i < options.Length; i++) { actions.Add(new CardAction { Title = $"{options[i]}", Value = $"{options[i]}", Type = ActionTypes.ImBack }); } // CardImage List <CardImage> cardImages = new List <CardImage>(); cardImages.Add(new CardImage(url: "https://cloud.githubusercontent.com/assets/20731546/20589442/69cc41da-b281-11e6-8617-6fdb7cc451cc.png")); // Reply with thumbnail card replyToConversation.Attachments.Add( new ThumbnailCard { Title = $"Welcome to Cortoso Bank", Subtitle = "May i help you?", Images = cardImages, Buttons = actions }.ToAttachment() ); await connector.Conversations.SendToConversationAsync(replyToConversation); return(Request.CreateResponse(HttpStatusCode.OK)); } /// --- Create account --------------------- if (userInput.ToLower().Equals("create account")) { int counter = newUserInformation.Count; replyToUser = $"({counter + 1}/6): Please enter your {userInfo[counter]}"; // prompt for email userData.SetProperty <bool>("createAccount", true); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); /// return Activity replyMessage2 = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replyMessage2); return(Request.CreateResponse(HttpStatusCode.OK)); } if (createAccount) { // Get new user's account information int counter = newUserInformation.Count + 1; if (counter < 6) { newUserInformation.Add(userInput); userData.SetProperty <List <object> >("newUserInformation", newUserInformation); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); replyToUser = $"({counter + 1}/6): Please enter your {userInfo[counter]}"; /// return Activity replyMessage2 = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replyMessage2); return(Request.CreateResponse(HttpStatusCode.OK)); } if (counter == 6) { // User's all information are there. Now is to create account ! newUserInformation.Add(userInput); // Get Unique AccountNo string newAccountNo = await AzureManager.AzureManagerInstance.getUniqueAccountNo(); Customer cust = new Customer(); cust.AccountNo = newAccountNo; cust.Email = newUserInformation[0].ToString(); cust.Password = newUserInformation[1].ToString(); cust.FirstName = newUserInformation[2].ToString(); cust.LastName = newUserInformation[3].ToString(); cust.Age = Int32.Parse(newUserInformation[4].ToString()); cust.Balance = Double.Parse(newUserInformation[5].ToString()); await AzureManager.AzureManagerInstance.AddCustomer(cust); // ## User Created Account ## createAccount = false; userData.SetProperty <bool>("userLoggedIn", true); userData.SetProperty <bool>("createAccount", false); userData.SetProperty <string>("userAccountNo", newAccountNo); userData.SetProperty <Customer>("clientDetails", cust); userData.SetProperty <List <object> >("newUserInformation", newUserInformation); // ## Account created page ## Activity replyToConversation = activity.CreateReply(replyToUser); replyToConversation.Recipient = activity.From; replyToConversation.Type = "message"; replyToConversation.Attachments = new List <Attachment>(); // CardImage List <CardImage> cardImages = new List <CardImage>(); cardImages.Add(new CardImage(url: "http://stock.wikimini.org/w/images/9/95/Gnome-stock_person-avatar-profile.png")); // CardButtons var actions2 = new List <CardAction>(); actions2.Add(new CardAction { Title = "Home", Value = "Help", Type = ActionTypes.ImBack }); // Reply with Hero card replyToConversation.Attachments.Add( new HeroCard { Title = "Your details", Text = $"Account No: {cust.AccountNo} \nBalance: ${string.Format("{0:0.00}", cust.Balance)}", Images = cardImages, Buttons = actions2 }.ToAttachment() ); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); await connector.Conversations.SendToConversationAsync(replyToConversation); return(Request.CreateResponse(HttpStatusCode.OK)); } } /// --- Handle log in ------------------------- if (userInput.ToLower().Equals("log in")) { replyToUser = "******"; userData.SetProperty <bool>("userLogin", true); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); /// return Activity replyMessage2 = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replyMessage2); return(Request.CreateResponse(HttpStatusCode.OK)); } if (userLogin) { int counter = loginInformation.Count; if (counter == 0) { loginInformation.Add(userInput); // this is to store email. replyToUser = "******"; userData.SetProperty <List <string> >("loginInformation", loginInformation); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); /// return Activity replyMessage2 = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replyMessage2); return(Request.CreateResponse(HttpStatusCode.OK)); } if (counter == 1) { loginInformation.Add(userInput); // this is to store password. string userEmail = loginInformation[0].ToString(); string userPassword = userInput; //loginInformation[1].ToString(); // Authenticate user bool validEmailAndPassword = await AzureManager.AzureManagerInstance.AuthenticateCustomer(userEmail, userPassword); if (validEmailAndPassword) // authenticate successfully { // get user data; string accountNo = await AzureManager.AzureManagerInstance.getAccountNo(userEmail, userPassword); Customer cust = await AzureManager.AzureManagerInstance.getCustomerDetails(accountNo); userData.SetProperty <string>("userAccountNo", accountNo); userData.SetProperty <Customer>("clientDetails", cust); userData.SetProperty <bool>("userLoggedIn", true); userData.SetProperty <bool>("userLogin", false); userData.SetProperty <List <string> >("loginInformation", new List <string>()); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); replyToUser = $"Log in successfully"; // ## Account created page ## Activity replyToConversation = activity.CreateReply(""); replyToConversation.Recipient = activity.From; replyToConversation.Type = "message"; replyToConversation.Attachments = new List <Attachment>(); // CardImage List <CardImage> cardImages = new List <CardImage>(); cardImages.Add(new CardImage(url: "http://stock.wikimini.org/w/images/9/95/Gnome-stock_person-avatar-profile.png")); // CardButtons var actions2 = new List <CardAction>(); actions2.Add(new CardAction { Title = "Home", Value = "Help", Type = ActionTypes.ImBack }); // Reply with Hero card replyToConversation.Attachments.Add( new HeroCard { Title = "Welcome Back", Text = $"Account No: {cust.AccountNo} \nBalance: ${string.Format("{0:0.00}", cust.Balance)}", Images = cardImages, Buttons = actions2 }.ToAttachment() ); await connector.Conversations.SendToConversationAsync(replyToConversation); return(Request.CreateResponse(HttpStatusCode.OK)); } else { replyToUser = "******"; userData.SetProperty <bool>("userLoggedIn", false); userData.SetProperty <bool>("userLogin", false); userData.SetProperty <List <string> >("loginInformation", new List <string>()); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); /// return Activity replyMessage2 = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replyMessage2); return(Request.CreateResponse(HttpStatusCode.OK)); } } } //// --- User haven't log in --------------------------- if (!createAccount && !userLogin && !userLoggedIn && !userInput.ToLower().Equals("log in") && !userInput.ToLower().Equals("create account") && !userInput.ToLower().Equals("delete account")) { /// Luis Intent LuisIntentObject luisIntentObject = await GetEntityFromLUIS(userInput); switch (luisIntentObject.topScoringIntent.intent) { case "getHelp": // ## Introduction Page ## Activity replyToConversation = activity.CreateReply(); replyToConversation.Recipient = activity.From; replyToConversation.Type = "message"; replyToConversation.Attachments = new List <Attachment>(); // CardButtons var actions = new List <CardAction>(); for (int i = 0; i < options.Length; i++) { actions.Add(new CardAction { Title = $"{options[i]}", Value = $"{options[i]}", Type = ActionTypes.ImBack }); } // CardImage List <CardImage> cardImages = new List <CardImage>(); cardImages.Add(new CardImage(url: "https://cloud.githubusercontent.com/assets/20731546/20589442/69cc41da-b281-11e6-8617-6fdb7cc451cc.png")); // Reply with thumbnail card replyToConversation.Attachments.Add( new ThumbnailCard { Title = $"Welcome to Cortoso Bank", Subtitle = "May i help you?", Images = cardImages, Buttons = actions }.ToAttachment() ); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); await connector.Conversations.SendToConversationAsync(replyToConversation); return(Request.CreateResponse(HttpStatusCode.OK)); case "getCurrencyRate": bool validCountryCode = CheckCountryCode(userInput); switch (validCountryCode) { case true: replyToUser = await GetCurrencyRate(userInput); // return Activity replywithCurrencyMessage = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replywithCurrencyMessage); return(Request.CreateResponse(HttpStatusCode.OK)); default: replyToUser = "******"; // return Activity replywithNullIntentMessage = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replywithNullIntentMessage); return(Request.CreateResponse(HttpStatusCode.OK)); } case "None": // ## Introducting Page ## Activity replyToConversation2 = activity.CreateReply(); replyToConversation2.Recipient = activity.From; replyToConversation2.Type = "message"; replyToConversation2.Attachments = new List <Attachment>(); // CardButtons var actions2 = new List <CardAction>(); for (int i = 0; i < options.Length; i++) { actions2.Add(new CardAction { Title = $"{options[i]}", Value = $"{options[i]}", Type = ActionTypes.ImBack }); } // CardImage List <CardImage> cardImages2 = new List <CardImage>(); cardImages2.Add(new CardImage(url: "https://cloud.githubusercontent.com/assets/20731546/20589442/69cc41da-b281-11e6-8617-6fdb7cc451cc.png")); // Reply with thumbnail card replyToConversation2.Attachments.Add( new ThumbnailCard { Title = $"Welcome to Cortoso Bank", Subtitle = "May i help you?", Images = cardImages2, Buttons = actions2 }.ToAttachment() ); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); await connector.Conversations.SendToConversationAsync(replyToConversation2); return(Request.CreateResponse(HttpStatusCode.OK)); } } /// handle delete account. If user want to delete account, he or she must already log in first. if (userLoggedIn && userInput.ToLower().Equals("delete account")) { await AzureManager.AzureManagerInstance.DeleteCustomer(clientDetails); userData.SetProperty <bool>("userLoggedIn", false); userData.SetProperty <bool>("userLoggedIn", false); userData.SetProperty <bool>("createAccount", false); userData.SetProperty <bool>("deleteAccount", false); userData.SetProperty <string>("userAccountNo", ""); userData.SetProperty <Customer>("clientDetails", new Customer()); userData.SetProperty <List <object> >("newUserInformation", new List <object>()); userData.SetProperty <List <string> >("loginInformation", new List <string>()); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); replyToUser = "******"; // ## Introduction Page ## Activity replyToConversation = activity.CreateReply(replyToUser); replyToConversation.Recipient = activity.From; replyToConversation.Type = "message"; replyToConversation.Attachments = new List <Attachment>(); // CardButtons var actions = new List <CardAction>(); for (int i = 0; i < options.Length; i++) { actions.Add(new CardAction { Title = $"{options[i]}", Value = $"{options[i]}", Type = ActionTypes.ImBack }); } // CardImage List <CardImage> cardImages = new List <CardImage>(); cardImages.Add(new CardImage(url: "https://cloud.githubusercontent.com/assets/20731546/20589442/69cc41da-b281-11e6-8617-6fdb7cc451cc.png")); // Reply with thumbnail card replyToConversation.Attachments.Add( new ThumbnailCard { Title = $"Welcome to Cortoso Bank", Subtitle = "May i help you?", Images = cardImages, Buttons = actions }.ToAttachment() ); // return await connector.Conversations.SendToConversationAsync(replyToConversation); return(Request.CreateResponse(HttpStatusCode.OK)); } /// --- User logged in --------------------------- if (userLoggedIn & !userInput.ToLower().Equals("clear")) { /// Luis Intent LuisIntentObject luisIntentObject = await GetEntityFromLUIS(userInput); switch (luisIntentObject.topScoringIntent.intent) { case "viewbalance": double Balance = await ViewBalance(userAccountNo); replyToUser = $"Your balance is ${string.Format("{0:0.00}", Balance)}."; break; case "getHelp": await Conversation.SendAsync(activity, () => new TransactionDialog()); return(Request.CreateResponse(HttpStatusCode.OK)); case "withdraw": bool WithDrawSuccess = await Withdraw(clientDetails, userInput); switch (WithDrawSuccess) { case true: userData.SetProperty <Customer>("clientDetails", clientDetails); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); replyToUser = $"Withdraw successfully. Your new balance is ${string.Format("{0:0.00}", clientDetails.Balance)}"; // return Activity replywithdrawMessage = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replywithdrawMessage); return(Request.CreateResponse(HttpStatusCode.OK)); default: replyToUser = "******"; // return Activity replyConfirmWithdrawMessage = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replyConfirmWithdrawMessage); return(Request.CreateResponse(HttpStatusCode.OK)); } case "deposit": bool DepositSuccess = await Deposit(clientDetails, userInput); switch (DepositSuccess) { case true: userData.SetProperty <Customer>("clientDetails", clientDetails); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); replyToUser = $"Deposited Successfully. Your new balance is ${string.Format("{0:0.00}", clientDetails.Balance)}"; // return Activity replyDepositMessage = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replyDepositMessage); return(Request.CreateResponse(HttpStatusCode.OK)); default: replyToUser = "******"; // return Activity replywithNullIntentMessage = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replywithNullIntentMessage); return(Request.CreateResponse(HttpStatusCode.OK)); } case "getCurrencyRate": bool validCountryCode = CheckCountryCode(userInput); switch (validCountryCode) { case true: replyToUser = await GetCurrencyRate(userInput); // return Activity replywithCurrencyMessage = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replywithCurrencyMessage); return(Request.CreateResponse(HttpStatusCode.OK)); default: replyToUser = "******"; // return Activity replywithNullIntentMessage = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replywithNullIntentMessage); return(Request.CreateResponse(HttpStatusCode.OK)); } case "transfer": replyToUser = "******"; break; default: await Conversation.SendAsync(activity, () => new TransactionDialog()); Activity replyMessage3 = activity.CreateReply(replyToUser); await connector.Conversations.ReplyToActivityAsync(replyMessage3); return(Request.CreateResponse(HttpStatusCode.OK)); } } /// --- Create a reply message ---------------- Activity replyMessage = activity.CreateReply(replyToUser); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); await connector.Conversations.ReplyToActivityAsync(replyMessage); } else { HandleSystemMessage(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }