diff --git a/src/StashBot/app/ITelegramUserMessage.cs b/src/StashBot/app/ITelegramUserMessage.cs new file mode 100644 index 0000000..952f9c1 --- /dev/null +++ b/src/StashBot/app/ITelegramUserMessage.cs @@ -0,0 +1,27 @@ +using System; + +namespace StashBot +{ + internal interface ITelegramUserMessage + { + long ChatId + { + get; + } + + int MessageId + { + get; + } + + DateTime DateSent + { + get; + } + + string Message + { + get; + } + } +} diff --git a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/AuthorizationStateHandler.cs b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/AuthorizationStateHandler.cs index 12bf666..c57981b 100644 --- a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/AuthorizationStateHandler.cs +++ b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/AuthorizationStateHandler.cs @@ -28,30 +28,30 @@ public void StartStateMessage(long chatId) messageManager.SendMessage(chatId, warningMessage); } - public void HandleUserMessage(long chatId, int messageId, string message, IChatStateHandlerContext context) + public void HandleUserMessage(ITelegramUserMessage message, IChatStateHandlerContext context) { IMessageManager messageManager = ModulesManager.GetModulesManager().GetMessageManager(); IUserManager userManager = ModulesManager.GetModulesManager().GetUserManager(); - if (commands.ContainsKey(message)) + if (commands.ContainsKey(message.Message)) { - commands[message](chatId, context); + commands[message.Message](message.ChatId, context); } else { - bool success = userManager.LoginUser(chatId, message); + bool success = userManager.LoginUser(message.ChatId, message.Message); if (success) { const string successMessage = "Success!"; - messageManager.SendMessage(chatId, successMessage); - context.ChangeChatState(chatId, Session.ChatSessionState.Authorized); + messageManager.SendMessage(message.ChatId, successMessage); + context.ChangeChatState(message.ChatId, Session.ChatSessionState.Authorized); } else { const string wrongMessage = "WRONG"; - messageManager.SendMessage(chatId, wrongMessage); + messageManager.SendMessage(message.ChatId, wrongMessage); } } } diff --git a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/AuthorizedStateHandler.cs b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/AuthorizedStateHandler.cs index 598eb86..a23d202 100644 --- a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/AuthorizedStateHandler.cs +++ b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/AuthorizedStateHandler.cs @@ -30,15 +30,15 @@ public void StartStateMessage(long chatId) messageManager.SendMessage(chatId, loginMessage); } - public void HandleUserMessage(long chatId, int messageId, string message, IChatStateHandlerContext context) + public void HandleUserMessage(ITelegramUserMessage message, IChatStateHandlerContext context) { - if (commands.ContainsKey(message)) + if (commands.ContainsKey(message.Message)) { - commands[message](chatId, context); + commands[message.Message](message.ChatId, context); } else { - AddDataToStashHandle(chatId, message); + AddDataToStashHandle(message.ChatId, message.Message); } } diff --git a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/CreateUserPasswordStateHandler.cs b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/CreateUserPasswordStateHandler.cs index 5a1b238..9e5789d 100644 --- a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/CreateUserPasswordStateHandler.cs +++ b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/CreateUserPasswordStateHandler.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Text.RegularExpressions; using StashBot.Module.User; namespace StashBot.Module.Message.Handler.ChatStateHandler @@ -28,17 +29,17 @@ public void StartStateMessage(long chatId) messageManager.SendMessage(chatId, warningMessage); } - public void HandleUserMessage(long chatId, int messageId, string message, IChatStateHandlerContext context) + public void HandleUserMessage(ITelegramUserMessage message, IChatStateHandlerContext context) { - if (commands.ContainsKey(message)) + if (commands.ContainsKey(message.Message)) { - commands[message](chatId, context); + commands[message.Message](message.ChatId, context); } else { - if (CheckPassword(chatId, message)) + if (CheckPassword(message.ChatId, message.Message)) { - RegistrationUser(chatId, message, context); + RegistrationUser(message.ChatId, message.Message, context); } } } @@ -62,14 +63,21 @@ private bool CheckPassword(long chatId, string password) if (password.Length < 12) { - const string warningMessage = "Min length 12"; + const string warningMessage = "Password min length 12!"; messageManager.SendMessage(chatId, warningMessage); return false; } if (password.Length > 25) { - const string warningMessage = "Max length 25"; + const string warningMessage = "Password max length 25!"; + messageManager.SendMessage(chatId, warningMessage); + return false; + } + + if (!Regex.IsMatch(password, @"^[a-zA-Z0-9!""#$%&'()*+,-./:;<=>?@[\]^_`{|}~]+$")) + { + const string warningMessage = "Password can contain only letters, numbers and special characters!"; messageManager.SendMessage(chatId, warningMessage); return false; } diff --git a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/FirstMessageStateHandler.cs b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/FirstMessageStateHandler.cs index 65a74a5..e193bea 100644 --- a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/FirstMessageStateHandler.cs +++ b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/FirstMessageStateHandler.cs @@ -9,14 +9,14 @@ public void StartStateMessage(long chatId) } - public void HandleUserMessage(long chatId, int messageId, string message, IChatStateHandlerContext context) + public void HandleUserMessage(ITelegramUserMessage message, IChatStateHandlerContext context) { IMessageManager messageManager = ModulesManager.GetModulesManager().GetMessageManager(); const string welcomeMessage = "Hi, good to see you!"; - messageManager.SendMessage(chatId, welcomeMessage); - context.ChangeChatState(chatId, ChatSessionState.Start); + messageManager.SendMessage(message.ChatId, welcomeMessage); + context.ChangeChatState(message.ChatId, ChatSessionState.Start); } } } diff --git a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/IChatStateHandler.cs b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/IChatStateHandler.cs index bf0bec3..d36d6d1 100644 --- a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/IChatStateHandler.cs +++ b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/IChatStateHandler.cs @@ -3,6 +3,6 @@ internal interface IChatStateHandler { void StartStateMessage(long chatId); - void HandleUserMessage(long chatId, int messageId, string message, IChatStateHandlerContext context); + void HandleUserMessage(ITelegramUserMessage message, IChatStateHandlerContext context); } } diff --git a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/RegistrationStateHandler.cs b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/RegistrationStateHandler.cs index 1108fc1..610149d 100644 --- a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/RegistrationStateHandler.cs +++ b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/RegistrationStateHandler.cs @@ -24,22 +24,19 @@ public void StartStateMessage(long chatId) IMessageManager messageManager = ModulesManager.GetModulesManager().GetMessageManager(); - const string warningMessage = "If you have already registered you will lose all your old data!"; + const string warningMessage = "If you have already registered you will lose all your old data!\nAre you sure? /yes or /no"; messageManager.SendMessage(chatId, warningMessage); - - const string questionMessage = "Are you sure? /yes or /no"; - messageManager.SendMessage(chatId, questionMessage); } - public void HandleUserMessage(long chatId, int messageId, string message, IChatStateHandlerContext context) + public void HandleUserMessage(ITelegramUserMessage message, IChatStateHandlerContext context) { - if (commands.ContainsKey(message)) + if (commands.ContainsKey(message.Message)) { - commands[message](chatId, context); + commands[message.Message](message.ChatId, context); } else { - StartStateMessage(chatId); + StartStateMessage(message.ChatId); } } diff --git a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/StartStateHandler.cs b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/StartStateHandler.cs index 46a6ba0..a2f63b5 100644 --- a/src/StashBot/app/Module/Message/Handler/ChatStateHandler/StartStateHandler.cs +++ b/src/StashBot/app/Module/Message/Handler/ChatStateHandler/StartStateHandler.cs @@ -29,15 +29,15 @@ public void StartStateMessage(long chatId) messageManager.SendMessage(chatId, mainCommandsMessage); } - public void HandleUserMessage(long chatId, int messageId, string message, IChatStateHandlerContext context) + public void HandleUserMessage(ITelegramUserMessage message, IChatStateHandlerContext context) { - if (commands.ContainsKey(message)) + if (commands.ContainsKey(message.Message)) { - commands[message](chatId, context); + commands[message.Message](message.ChatId, context); } else { - StartStateMessage(chatId); + StartStateMessage(message.ChatId); } } @@ -56,7 +56,7 @@ private void Information(long chatId, IChatStateHandlerContext context) IMessageManager messageManager = ModulesManager.GetModulesManager().GetMessageManager(); - const string informationMessage = "This is information"; + const string informationMessage = "This is open source bot for stashing in Telegram Messenger.\nThe code you can find here: https://github.com/dmitrydnl/StashBot"; messageManager.SendMessage(chatId, informationMessage); } } diff --git a/src/StashBot/app/Module/Message/Handler/IMessageHandler.cs b/src/StashBot/app/Module/Message/Handler/IMessageHandler.cs index 1e664c8..15f56ef 100644 --- a/src/StashBot/app/Module/Message/Handler/IMessageHandler.cs +++ b/src/StashBot/app/Module/Message/Handler/IMessageHandler.cs @@ -2,6 +2,6 @@ { internal interface IMessageHandler { - void HandleUserMessage(long chatId, int messageId, string message); + void HandleUserMessage(ITelegramUserMessage message); } } diff --git a/src/StashBot/app/Module/Message/Handler/MessageHandler.cs b/src/StashBot/app/Module/Message/Handler/MessageHandler.cs index fa78226..53c9594 100644 --- a/src/StashBot/app/Module/Message/Handler/MessageHandler.cs +++ b/src/StashBot/app/Module/Message/Handler/MessageHandler.cs @@ -12,31 +12,31 @@ internal MessageHandler() chatStateHandlerFactory = new ChatStateHandlerFactory(); } - public void HandleUserMessage(long chatId, int messageId, string message) + public void HandleUserMessage(ITelegramUserMessage message) { ISessionManager sessionManager = ModulesManager.GetModulesManager().GetSessionManager(); - if (string.IsNullOrEmpty(message)) + if (string.IsNullOrEmpty(message.Message)) { return; } - IChatSession chatSession = sessionManager.GetChatSession(chatId); + IChatSession chatSession = sessionManager.GetChatSession(message.ChatId); if (chatSession == null) { - sessionManager.CreateChatSession(chatId); - chatSession = sessionManager.GetChatSession(chatId); + sessionManager.CreateChatSession(message.ChatId); + chatSession = sessionManager.GetChatSession(message.ChatId); } - sessionManager.UserSentMessage(chatId, messageId); - if (string.Equals(message, "/e") || string.Equals(message, "/exit")) + sessionManager.UserSentMessage(message.ChatId, message.MessageId); + if (string.Equals(message.Message, "/e") || string.Equals(message.Message, "/exit")) { - sessionManager.KillChatSession(chatId); + sessionManager.KillChatSession(message.ChatId); } else { - chatStateHandlerFactory.GetChatStateHandler(chatSession.State).HandleUserMessage(chatId, messageId, message, this); + chatStateHandlerFactory.GetChatStateHandler(chatSession.State).HandleUserMessage(message, this); } } diff --git a/src/StashBot/app/Module/Message/MessageManager.cs b/src/StashBot/app/Module/Message/MessageManager.cs index 5a55e9b..28e4048 100644 --- a/src/StashBot/app/Module/Message/MessageManager.cs +++ b/src/StashBot/app/Module/Message/MessageManager.cs @@ -18,9 +18,9 @@ internal MessageManager() messageDelete = new MessageDelete(); } - public void HandleUserMessage(long chatId, int messageId, string message) + public void HandleUserMessage(ITelegramUserMessage message) { - messageHandler.HandleUserMessage(chatId, messageId, message); + messageHandler.HandleUserMessage(message); } public void SendMessage(long chatId, string message) diff --git a/src/StashBot/app/Module/Session/ChatSession.cs b/src/StashBot/app/Module/Session/ChatSession.cs index 9a7fb4c..6802fae 100644 --- a/src/StashBot/app/Module/Session/ChatSession.cs +++ b/src/StashBot/app/Module/Session/ChatSession.cs @@ -14,11 +14,13 @@ internal class ChatSession : IChatSession private readonly List userMessagesId; private readonly List botMessagesId; - public long ChatId { + public long ChatId + { get; } - public ChatSessionState State { + public ChatSessionState State + { get; set; } diff --git a/src/StashBot/app/Module/Session/IChatSession.cs b/src/StashBot/app/Module/Session/IChatSession.cs index 46b1880..e273807 100644 --- a/src/StashBot/app/Module/Session/IChatSession.cs +++ b/src/StashBot/app/Module/Session/IChatSession.cs @@ -6,11 +6,13 @@ long ChatId { get; } + ChatSessionState State { get; set; } + void UserSentMessage(int messageId); void BotSentMessage(int messageId); void Kill(); diff --git a/src/StashBot/app/StashBot.cs b/src/StashBot/app/StashBot.cs index e967ee1..d1d2f17 100644 --- a/src/StashBot/app/StashBot.cs +++ b/src/StashBot/app/StashBot.cs @@ -39,10 +39,7 @@ private void OnMessage(object sender, MessageEventArgs e) IMessageManager messageManager = ModulesManager.GetModulesManager().GetMessageManager(); - long chatId = e.Message.Chat.Id; - int messageId = e.Message.MessageId; - string message = e.Message.Text; - messageManager.HandleUserMessage(chatId, messageId, message); + messageManager.HandleUserMessage(new TelegramUserMessage(e.Message)); } } } diff --git a/src/StashBot/app/TelegramUserMessage.cs b/src/StashBot/app/TelegramUserMessage.cs new file mode 100644 index 0000000..0469c90 --- /dev/null +++ b/src/StashBot/app/TelegramUserMessage.cs @@ -0,0 +1,36 @@ +using System; +using Telegram.Bot.Types; + +namespace StashBot +{ + internal class TelegramUserMessage : ITelegramUserMessage + { + public long ChatId + { + get; + } + + public int MessageId + { + get; + } + + public DateTime DateSent + { + get; + } + + public string Message + { + get; + } + + internal TelegramUserMessage(Message message) + { + ChatId = message.Chat.Id; + MessageId = message.MessageId; + DateSent = message.Date; + Message = message.Text; + } + } +}