Skip to content
This repository has been archived by the owner on Sep 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #40 from dmitrydnl/dmitrydnl/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrydnl authored Oct 24, 2019
2 parents aa4fcb9 + 50b0036 commit 1008612
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 53 deletions.
27 changes: 27 additions & 0 deletions src/StashBot/app/ITelegramUserMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace StashBot
{
internal interface ITelegramUserMessage
{
long ChatId
{
get;
}

int MessageId
{
get;
}

DateTime DateSent
{
get;
}

string Message
{
get;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
using StashBot.Module.User;

namespace StashBot.Module.Message.Handler.ChatStateHandler
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/StashBot/app/Module/Message/Handler/IMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
internal interface IMessageHandler
{
void HandleUserMessage(long chatId, int messageId, string message);
void HandleUserMessage(ITelegramUserMessage message);
}
}
18 changes: 9 additions & 9 deletions src/StashBot/app/Module/Message/Handler/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/StashBot/app/Module/Message/MessageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/StashBot/app/Module/Session/ChatSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ internal class ChatSession : IChatSession
private readonly List<int> userMessagesId;
private readonly List<int> botMessagesId;

public long ChatId {
public long ChatId
{
get;
}

public ChatSessionState State {
public ChatSessionState State
{
get;
set;
}
Expand Down
2 changes: 2 additions & 0 deletions src/StashBot/app/Module/Session/IChatSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ long ChatId
{
get;
}

ChatSessionState State
{
get;
set;
}

void UserSentMessage(int messageId);
void BotSentMessage(int messageId);
void Kill();
Expand Down
5 changes: 1 addition & 4 deletions src/StashBot/app/StashBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
36 changes: 36 additions & 0 deletions src/StashBot/app/TelegramUserMessage.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 1008612

Please sign in to comment.