Esempio n. 1
0
        public void Execute(CallbackQuery callbackQuery)
        {
            if (CacheData.Operators
                .SingleOrDefault(x => x.TelegramUserId ==
                                 callbackQuery.From.Id &&
                                 x.Level == Models.Operator.Levels.Super) == null)
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = callbackQuery.Message.Chat,
                    ReplyToMessageId = callbackQuery.Message.MessageId,
                    Text             = CacheData.GetTranslation("en", "error_not_auth_command")
                });
                Manager.BotClient.SendTextMessageAsync(
                    chatId: CacheData.ControlChatId,
                    parseMode: ParseMode.Markdown,
                    text: String.Format(
                        "User *{0}:{1}* tried to use command GetTranslation.",
                        callbackQuery.Message.From.Id,
                        callbackQuery.Message.From.Username)
                    );
                return;
            }

            string[] parameters = callbackQuery.Data.Split(" ");
            string   dataSource = parameters[1].Split('|')[0];
            string   keyId      = parameters[1].Split('|')[1].Trim();

            List <Entry> entries = new List <Entry>();

            if (dataSource == "InMemory")
            {
                foreach (string language in CacheData.Translations.Keys)
                {
                    bool isTranslated = CacheData.Translations[language].TryGetValue(keyId, out Entry entry);
                    if (isTranslated)
                    {
                        entries.Add(entry);
                    }
                }

                if (entries.Count == 0)
                {
                    MessageQueueManager.EnqueueMessage(
                        new Models.ChatMessage()
                    {
                        Timestamp        = DateTime.UtcNow,
                        Chat             = callbackQuery.Message.Chat,
                        ReplyToMessageId = callbackQuery.Message.MessageId,
                        Text             = "Translation key exists but not translation is present."
                    });
                }
            }
            else if (dataSource == "Database")
            {
                BusinessLogic.TranslationLogic translationLogic = new BusinessLogic.TranslationLogic();
                Key translationKey = translationLogic.GetKeyById(keyId);
                if (translationKey == null)
                {
                    MessageQueueManager.EnqueueMessage(
                        new Models.ChatMessage()
                    {
                        Timestamp        = DateTime.UtcNow,
                        Chat             = callbackQuery.Message.Chat,
                        ReplyToMessageId = callbackQuery.Message.MessageId,
                        Text             = "This translation key does not exist."
                    });
                }

                entries = translationLogic.GetEntriesById(keyId);
                if (entries.Count == 0)
                {
                    MessageQueueManager.EnqueueMessage(
                        new Models.ChatMessage()
                    {
                        Timestamp        = DateTime.UtcNow,
                        Chat             = callbackQuery.Message.Chat,
                        ReplyToMessageId = callbackQuery.Message.MessageId,
                        Text             = "Translation key exists but not translation is present."
                    });
                }
            }

            string answer = $"Available {dataSource} translations:";

            foreach (Entry translation in entries)
            {
                answer += $"\n*{translation.Language.Name}* : {translation.Translation}";
            }
            MessageQueueManager.EnqueueMessage(
                new Models.ChatMessage()
            {
                Timestamp        = DateTime.UtcNow,
                Chat             = callbackQuery.Message.Chat,
                ReplyToMessageId = callbackQuery.Message.MessageId,
                ParseMode        = ParseMode.Markdown,
                Text             = answer
            });
        }
Esempio n. 2
0
        public static void AddTranslationEntry(CommandMessage commandMessage,
                                               Message replyMessage)
        {
            if (CacheData.Operators
                .SingleOrDefault(x => x.TelegramUserId ==
                                 commandMessage.Message.From.Id &&
                                 x.Level == Models.Operator.Levels.Super) == null)
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = replyMessage.Chat,
                    ReplyToMessageId = replyMessage.MessageId,
                    Text             = CacheData.GetTranslation("en", "error_not_auth_command")
                });
                Manager.BotClient.SendTextMessageAsync(
                    chatId: CacheData.ControlChatId,
                    parseMode: ParseMode.Markdown,
                    text: String.Format(
                        "User *{0}:{1}* tried to use command AddTranslationEntry.",
                        replyMessage.From.Id,
                        replyMessage.From.Username)
                    );
                return;
            }

            string[] parameters = commandMessage.Value.Split('|');
            if (parameters.Length != 2)
            {
                CommandQueueManager.DenqueueMessage(commandMessage);
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = replyMessage.Chat,
                    ReplyToMessageId = replyMessage.MessageId,
                    Text             = CacheData.GetTranslation("en", "error_invalid_parameters"),
                    ReplyMarkup      = new ReplyKeyboardRemove()
                    {
                        Selective = true
                    }
                });
            }
            string languageId = parameters[0];
            string keyId      = parameters[1];

            BusinessLogic.TranslationLogic translationLogic = new BusinessLogic.TranslationLogic();
            Entry translationEntry = translationLogic.AddEntry(
                languageId, keyId,
                replyMessage.Text, replyMessage.From.Id);

            if (translationEntry == null)
            {
                CommandQueueManager.DenqueueMessage(commandMessage);
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = replyMessage.Chat,
                    ReplyToMessageId = replyMessage.MessageId,
                    ParseMode        = ParseMode.Markdown,
                    Text             = "*Error* adding translation entry.\nCheck internal logs.",
                    ReplyMarkup      = new ReplyKeyboardRemove()
                    {
                        Selective = true
                    }
                });
                return;
            }

            CommandQueueManager.DenqueueMessage(commandMessage);
            MessageQueueManager.EnqueueMessage(
                new Models.ChatMessage()
            {
                Timestamp        = DateTime.UtcNow,
                Chat             = replyMessage.Chat,
                ReplyToMessageId = replyMessage.MessageId,
                ParseMode        = ParseMode.Markdown,
                Text             = "*OK!*\nTranslation added successfully!\nRemember to reload them manually!",
                ReplyMarkup      = new ReplyKeyboardRemove()
                {
                    Selective = true
                }
            });
        }
Esempio n. 3
0
        public static void AddTranslationKey(CommandMessage commandMessage,
                                             Message replyMessage)
        {
            if (CacheData.Operators
                .SingleOrDefault(x => x.TelegramUserId ==
                                 commandMessage.Message.ReplyToMessage.From.Id &&
                                 x.Level == Models.Operator.Levels.Super) == null)
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = replyMessage.Chat,
                    ReplyToMessageId = replyMessage.MessageId,
                    Text             = CacheData.GetTranslation("en", "error_not_auth_command")
                });
                Manager.BotClient.SendTextMessageAsync(
                    chatId: CacheData.ControlChatId,
                    parseMode: ParseMode.Markdown,
                    text: String.Format(
                        "User *{0}:{1}* tried to use command AddTranslationKey.",
                        replyMessage.From.Id,
                        replyMessage.From.Username),
                    replyMarkup: new ReplyKeyboardRemove()
                {
                    Selective = true
                }
                    );
                return;
            }

            CommandQueueManager.DenqueueMessage(commandMessage);

            var regexItem = new Regex("^[a-zA-Z0-9_]*$");

            if (!regexItem.IsMatch(replyMessage.Text.Trim()))
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = replyMessage.Chat,
                    ReplyToMessageId = replyMessage.MessageId,
                    ParseMode        = ParseMode.Markdown,
                    Text             = "Error: translation key cannot contain space and/or special chars.\n"
                                       + "Start again the process.",
                    ReplyMarkup = new ReplyKeyboardRemove()
                    {
                        Selective = true
                    }
                });
                return;
            }

            BusinessLogic.TranslationLogic translationLogic = new BusinessLogic.TranslationLogic();
            Key translationKey = translationLogic.GetKeyById(replyMessage.Text.Trim());

            if (translationKey == null)
            {
                translationKey = translationLogic.AddKey(
                    replyMessage.Text.Trim(),
                    replyMessage.From.Id);
            }

            if (translationKey == null)
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = replyMessage.Chat,
                    ReplyToMessageId = replyMessage.MessageId,
                    ParseMode        = ParseMode.Markdown,
                    Text             = "*Error* adding translation key.\nCheck internal logs.",
                    ReplyMarkup      = new ReplyKeyboardRemove()
                    {
                        Selective = true
                    }
                });
                return;
            }

            Entry entryExists = translationLogic.GetEntryById(commandMessage.Value, translationKey.KeyId);

            if (entryExists != null)
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = replyMessage.Chat,
                    ReplyToMessageId = replyMessage.MessageId,
                    ParseMode        = ParseMode.Markdown,
                    Text             = $"*Error*\n {commandMessage.Value} translation for key `{translationKey.KeyId}` already exists!",
                    ReplyMarkup      = new ReplyKeyboardRemove()
                    {
                        Selective = true
                    }
                });
                return;
            }

            CommandMessage newCommandMessage = new CommandMessage()
            {
                Command   = "AddTranslationEntry",
                Value     = commandMessage.Value + "|" + translationKey.KeyId,
                Message   = replyMessage,
                Timestamp = DateTime.UtcNow
            };

            CommandQueueManager.EnqueueMessage(newCommandMessage);

            MessageQueueManager.EnqueueMessage(
                new Models.ChatMessage()
            {
                Timestamp        = DateTime.UtcNow,
                Chat             = replyMessage.Chat,
                ReplyToMessageId = replyMessage.MessageId,
                ParseMode        = ParseMode.Markdown,
                Text             = $"*[ADMIN] [r:{replyMessage.MessageId}]*\nType translation for " +
                                   $"`{translationKey.KeyId}` in `{CacheData.Languages[commandMessage.Value].Name}`:",
                ReplyMarkup = new ForceReplyMarkup()
                {
                    Selective = true
                }
            });
        }
Esempio n. 4
0
        public void Execute(Message message)
        {
            if (CacheData.Operators
                .SingleOrDefault(x => x.TelegramUserId == message.From.Id &&
                                 x.Level == Models.Operator.Levels.Super) == null)
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = message.Chat,
                    ReplyToMessageId = message.MessageId,
                    Text             = CacheData.GetTranslation("en", "error_not_auth_command")
                });
                Manager.BotClient.SendTextMessageAsync(
                    chatId: CacheData.ControlChatId,
                    parseMode: ParseMode.Markdown,
                    text: String.Format(
                        "*[Report]*\n" +
                        "⚠️ Non operator tried to use /addtranslation\n" +
                        "\n*Chat:* {0}" +
                        "\n*ChatId:* {1}" +
                        "\n*UserId:* {2}" +
                        "\n\n*hash_code:* #UB{3}-{4}",
                        message.Chat.Title,
                        message.Chat.Id,
                        message.From.Id,
                        message.Chat.Id.ToString().Replace("-", ""),
                        Guid.NewGuid())
                    );
                return;
            }

            try
            {
                BusinessLogic.TranslationLogic translationLogic = new BusinessLogic.TranslationLogic();
                List <Language>             languages           = translationLogic.GetLanguage();
                List <InlineKeyboardButton> langButtons         = new List <InlineKeyboardButton>();
                foreach (Language lang in languages)
                {
                    langButtons.Add(InlineKeyboardButton.WithCallbackData(
                                        $"{lang.Name} ({lang.LanguageId})",
                                        $"/AddTranslation language:{lang.LanguageId}"
                                        ));
                }

                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = message.Chat,
                    ReplyToMessageId = message.MessageId,
                    ParseMode        = ParseMode.Markdown,
                    Text             = "*[ADMIN]*\nSelect language:",
                    ReplyMarkup      = new InlineKeyboardMarkup(
                        langButtons
                        )
                });
            }
            catch (Exception ex)
            {
                Data.Utils.Logging.AddLog(new Models.SystemLog()
                {
                    LoggerName = CacheData.LoggerName,
                    Date       = DateTime.Now,
                    Function   = "Unifiedban.Terminal.Command.AddTranslation",
                    Level      = Models.SystemLog.Levels.Error,
                    Message    = ex.Message,
                    UserId     = -1
                });

                Manager.BotClient.SendTextMessageAsync(
                    chatId: CacheData.ControlChatId,
                    text: "Error. Check logs."
                    );
            }
        }
Esempio n. 5
0
        public static bool InitializeTranslations()
        {
            try
            {
                CacheData.Translations = new Dictionary <string, Dictionary <string, Models.Translation.Entry> >();
                BusinessLogic.TranslationLogic     translationLogic = new BusinessLogic.TranslationLogic();
                List <Models.Translation.Language> languages        = translationLogic.GetLanguage();
                foreach (Models.Translation.Language language in languages)
                {
                    CacheData.Languages.TryAdd(language.LanguageId, language);
                    List <Models.Translation.Entry> entries = translationLogic
                                                              .GetEntriesByLanguage(language.LanguageId);
                    if (entries.Count == 0)
                    {
                        continue;
                    }

                    CacheData.Translations.TryAdd(language.LanguageId, new Dictionary <string, Models.Translation.Entry>());
                    foreach (Models.Translation.Entry entry in entries)
                    {
                        CacheData.Translations[language.LanguageId].TryAdd(entry.KeyId, entry);
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                Data.Utils.Logging.AddLog(new Models.SystemLog()
                {
                    LoggerName = CacheData.LoggerName,
                    Date       = DateTime.Now,
                    Function   = "Unifiedban.Terminal.Program.InitializeTranslations",
                    Level      = Models.SystemLog.Levels.Fatal,
                    Message    = "Error loading translations.",
                    UserId     = -1
                });

                Data.Utils.Logging.AddLog(new Models.SystemLog()
                {
                    LoggerName = CacheData.LoggerName,
                    Date       = DateTime.Now,
                    Function   = "Unifiedban.Terminal.Program.InitializeTranslations",
                    Level      = Models.SystemLog.Levels.Fatal,
                    Message    = ex.Message,
                    UserId     = -1
                });
                if (ex.InnerException != null)
                {
                    Data.Utils.Logging.AddLog(new Models.SystemLog()
                    {
                        LoggerName = CacheData.LoggerName,
                        Date       = DateTime.Now,
                        Function   = "Unifiedban.Terminal.Program.InitializeTranslations",
                        Level      = Models.SystemLog.Levels.Fatal,
                        Message    = ex.InnerException.Message,
                        UserId     = -1
                    });
                }

                return(false);
            }
        }