예제 #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
            });
        }
예제 #2
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
                }
            });
        }