private InlineMessage TeamListToSend(string username, List <Team> teamsToShow, string message = "")
        {
            var data = new { username, teamsToShow, message }.ToJson();

            try
            {
                var inlineMessage = new InlineMessage();
                var maxRowLength  = 2;
                if (teamsToShow.Any())
                {
                    inlineMessage.Text = $"@{username} \n" + message + _chooseTeam;
                    var inlineKeyBoardButtons = new List <List <InlineKeyboardButton> >();
                    var buttons = new List <InlineKeyboardButton>();
                    foreach (var team in teamsToShow)
                    {
                        if (buttons.Count == maxRowLength)
                        {
                            inlineKeyBoardButtons.Add(buttons.Select(x => x).ToList());
                            buttons.RemoveRange(0, maxRowLength);
                        }

                        buttons.Add(InlineKeyboardButton.WithCallbackData(team.Name, team.Id.ToString()));
                    }
                    inlineKeyBoardButtons.Add(buttons);

                    var keyboardMarkup = new InlineKeyboardMarkup(inlineKeyBoardButtons);

                    inlineMessage.ReplyMarkup = keyboardMarkup;
                }
                return(inlineMessage);
            }
            catch (Exception ex)
            {
                _log.WriteError("TelegramBotActions.TeamListToSend", data, ex);
                throw;
            }
        }
        private async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
        {
            try
            {
                if (!CheckTotalTimeLimit(messageEventArgs.Message.Date))
                {
                    return;
                }

#if DEBUG
                Console.WriteLine("BotOnMessageReceived - " + messageEventArgs.Message.Text);
#endif

                var message = messageEventArgs.Message;

                if (message.Chat.Id != _settings.CommandChatId)
                {
                    _log.WriteWarning("BotOnMessageReceived", $"chat id: {message.Chat.Id}, chat name: {message.Chat.Title}, sender: {message.From.Username}",
                                      "Receive message from wronf chat");
                    return;
                }


                if (message == null || message.Type != MessageType.Text)
                {
                    return;
                }

                var firstWord = message.Text.Split(' ').First();
                var command   = firstWord.IndexOf('@') == -1 ? firstWord : firstWord.Substring(0, firstWord.IndexOf('@'));

                if (command == "/create" || command == "/resetMyTeam" || command == "/groupId")
                {
                    _history.Clear();
                }

                if (command == "/groupId")
                {
                    await SendTextToUser(message.Chat.Id, $"Group Id: {message.Chat.Id}");

                    return;
                }

                var result = await CheckForGroupAccess(message.Chat.Id, message.Chat.Id);

                if (!result)
                {
                    return;
                }

                // get repository name and checking user
                if (message.ReplyToMessage?.Text == $"@{message.From.Username} \n" + _questionEnterName && TimeoutTimer.Working && CurrentUser.User.Id == message.From.Id)
                {
                    TimeoutTimer.Stop();
                    // var prevQuestion = await _telegramBotHistoryRepository.GetLatestAsync(x => x.ChatId == message.Chat.Id && x.UserId == message.From.Id);
                    var prevQuestion = _history.LastOrDefault();

                    if (prevQuestion != null && prevQuestion.Question == _questionEnterName)
                    {
                        if (!Regex.IsMatch(message.Text, @"^[a-zA-Z0-9._-]+$"))
                        {
                            await SendTextToUser(message.Chat.Id, $"@{message.From.Username} \n" + "Incorrect format.");

                            await _bot.SendTextMessageAsync(message.Chat.Id, $"@{message.From.Username} \n" + _questionEnterName, replyMarkup : new ForceReplyMarkup {
                                Selective = true
                            });
                        }
                        else
                        {
                            var repoIsAlreadyExists = await _actions.IsRepositoryExist(message.Text);

                            if (repoIsAlreadyExists)
                            {
                                await SendTextToUser(message.Chat.Id, $"@{message.From.Username} \n" + "Repository with this name already exists.");

                                await _bot.SendTextMessageAsync(message.Chat.Id, $"@{message.From.Username} \n" + _questionEnterName, replyMarkup : new ForceReplyMarkup {
                                    Selective = true
                                });
                            }
                            else
                            {
                                await _bot.SendTextMessageAsync(message.Chat.Id, $"@{message.From.Username} \n" + _questionEnterDesc, replyMarkup : new ForceReplyMarkup {
                                    Selective = true
                                });
                                await CreateBotHistory(message.Chat.Id, message.From.Id, message.From.Username, _questionEnterDesc, message.Text);
                            }
                        }

                        TimeoutTimer.Start();
                    }
                    else
                    {
                        await SendTextToUser(message.Chat.Id);
                    }
                }

                // get repository description and checking user
                else if (message.ReplyToMessage?.Text == $"@{message.From.Username} \n" + _questionEnterDesc && TimeoutTimer.Working && CurrentUser.User.Id == message.From.Id)
                {
                    TimeoutTimer.Stop();
                    //var prevQuestion = await _telegramBotHistoryRepository.GetLatestAsync(x => x.ChatId == message.Chat.Id && x.UserId == message.From.Id);
                    var prevQuestion = _history.LastOrDefault();

                    var checkMenuType = await GetMenuAction(message.Chat.Id, message.From.Id);

                    if (checkMenuType == _createLibraryRepo)
                    {
                        await CreateBotHistory(message.Chat.Id, message.From.Id, message.From.Username, "Creating library repo", message.Text);
                        await SendTextToUser(message.Chat.Id, $"@{message.From.Username} \n" + "Creating library repository. Please wait...");

                        var question = await CreateRepoAsync(message.Chat.Id, message.From.Id);
                        await CreateBotHistory(message.Chat.Id, message.From.Id, message.From.Username, question, message.Text);
                        await SaveHistory();
                    }
                    else if (checkMenuType == _createGithubRepo)
                    {
                        await CreateBotHistory(message.Chat.Id, message.From.Id, message.From.Username, "Creating repository");

                        await SendTextToUser(message.Chat.Id, $"@{message.From.Username} \n" + "Creating repository. Please wait...");

                        var question = await CreateRepoAsync(message.Chat.Id, message.From.Id);

                        await CreateBotHistory(message.Chat.Id, message.From.Id, message.From.Username, question, message.Text);
                        await SaveHistory();
                    }
                    else
                    {
                        await SendTextToUser(message.Chat.Id);
                    }
                }

                else if (message.ReplyToMessage?.Text == $"@{message.From.Username} \n" + _questionEnterGitAcc && TimeoutTimer.Working && CurrentUser.User.Id == message.From.Id)
                {
                    TimeoutTimer.Stop();
                    // var prevQuestion = await _telegramBotHistoryRepository.GetLatestAsync(x => x.ChatId == message.Chat.Id && x.UserId == message.From.Id);
                    var prevQuestion = _history.LastOrDefault();
                    if (prevQuestion != null && prevQuestion.Question == _questionEnterGitAcc)
                    {
                        if (!Regex.IsMatch(message.Text, @"^[a-zA-Z0-9._-]+$"))
                        {
                            await SendTextToUser(message.Chat.Id, $"@{message.From.Username} \n" + "Incorrect format.");

                            await _bot.SendTextMessageAsync(message.Chat.Id, $"@{message.From.Username} \n" + _questionEnterGitAcc, replyMarkup : new ForceReplyMarkup {
                                Selective = true
                            });
                        }
                        else
                        {
                            await SendTextToUser(message.Chat.Id, $"@{message.From.Username} \n" + "Please, wait a second...");
                            await CreateBotHistory(message.Chat.Id, message.From.Id, message.From.Username, _chooseTeam, message.Text);

                            var gitUserTeamList = await _actions.UserHasTeamCheckAsync(message.Text);

                            Console.WriteLine(gitUserTeamList.Count);

                            if (gitUserTeamList.Count == 0)
                            {
                                var inlineMessage = TeamListToSend(message.From.Username, new List <Team>(), $"You not assign to any team in organisation {_settings.OrganizationName}\n");
                                await _bot.SendTextMessageAsync(message.Chat.Id, inlineMessage.Text, replyMarkup : inlineMessage.ReplyMarkup);
                            }
                            else if (gitUserTeamList.Count == 1)
                            {
                                var userTeam = gitUserTeamList.FirstOrDefault();
                                await SendTextToUser(message.Chat.Id, $"@{message.From.Username} \n" + $"Your team is \"{userTeam.Name}\".");
                                await TeamSelected(message.Chat.Id, message.From, userTeam.Id.ToString());
                            }
                            else
                            {
                                var inlineMessage = TeamListToSend(message.From.Username, gitUserTeamList, "You are assigned to multiple teams.\n");
                                await _bot.SendTextMessageAsync(message.Chat.Id, inlineMessage.Text, replyMarkup : inlineMessage.ReplyMarkup);
                            }
                        }

                        TimeoutTimer.Start();
                    }
                    else
                    {
                        await SendTextToUser(message.Chat.Id);
                    }
                }
                else if (TimeoutTimer.Working && CurrentUser.User.Id != message.From.Id)
                {
                    await SendTextToUser(message.Chat.Id, $"@{message.From.Username} Please, wait for  user @{CurrentUser.User.Username} finishes creating repository");
                }

                // read commands
                else
                {
                    if (TimeoutTimer.Working && CurrentUser.User.Id == message.From.Id)
                    {
                        TimeoutTimer.Stop();
                    }
                    switch (command)
                    {
                    // send inline menu keyboard
                    case "/create":

                        var TeamId = await GetUserTeamId(message.Chat.Id, message.From.Id);

                        string addTeam       = "";
                        var    inlineMessage = new InlineMessage();

                        var inlineKeyboard = new List <IEnumerable <InlineKeyboardButton> >()
                        {
                            new []     // first row
                            {
                                InlineKeyboardButton.WithCallbackData("Create Repo", _createGithubRepo),
                                InlineKeyboardButton.WithCallbackData("Create Library Repo", _createLibraryRepo)
                            }
                        };

                        //if (TeamId != 0)
                        //{
                        //    var teamName = await _actions.GetTeamById(TeamId);
                        //    addTeam = $"\nYour team is \"{teamName}\"";
                        //    inlineKeyboard.Add(
                        //    new[] // first row
                        //    {
                        //    InlineKeyboardButton.WithCallbackData("Reset my team", _resetTeam)
                        //    });
                        //    await CreateBotHistory(message.Chat.Id, message.From.Id, message.From.Username, _chooseTeam, message.Text);
                        //}

                        inlineMessage.ReplyMarkup = new InlineKeyboardMarkup(inlineKeyboard);

                        inlineMessage.Text = $"@{message.From.Username} \n" + _mainMenu + addTeam;

                        await _bot.SendTextMessageAsync(message.Chat.Id, inlineMessage.Text, replyMarkup : inlineMessage.ReplyMarkup);

                        await CreateBotHistory(message.Chat.Id, message.From.Id, message.From.Username, inlineMessage.Text, (TeamId != 0)?TeamId.ToString() : "");

                        break;

                    case "/resetMyTeam":
                        await ClearTeam(message.Chat.Id, message.From);

                        break;

                    default:
                        //send default answer
                        await SendTextToUser(message.Chat.Id);

                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                await _log.WriteErrorAsync("TelegramBotService.BotOnMessageReceived", messageEventArgs.Message.ReplyToMessage?.Text, ex);

                return;
            }
        }
        private async Task SendResponseMarkup(CallbackQuery callbackQuery, Message message)
        {
            try
            {
                var historyCreateSkipped = false;
                if (TimeoutTimer.Working && CurrentUser.User.Id != callbackQuery.From.Id)
                {
                    await SendTextToUser(callbackQuery.Message.Chat.Id, $"@{callbackQuery.From.Username} Please, wait for  user @{CurrentUser.User.Username} finishes creating repository");
                }
                else
                {
                    var inlineMessage = new InlineMessage();
                    var question      = String.Empty;
                    var saveHistory   = false;

                    switch (callbackQuery.Data)
                    {
                    case _createGithubRepo:
                    case _createLibraryRepo:

                        TimeoutTimer.Stop();
                        //Getting userTeamId
                        var userTeamId = await GetUserTeamId(message.Chat.Id, callbackQuery.From.Id);

                        if (userTeamId != 0)
                        {
                            question = _questionEnterName;
                            await _bot.SendTextMessageAsync(message.Chat.Id, $"@{callbackQuery.From.Username} \n" + _questionEnterName, replyMarkup : new ForceReplyMarkup {
                                Selective = true
                            });
                        }
                        else
                        {
                            question = _questionEnterGitAcc;
                            await _bot.SendTextMessageAsync(message.Chat.Id, $"@{callbackQuery.From.Username} \n" + _questionEnterGitAcc, replyMarkup : new ForceReplyMarkup {
                                Selective = true
                            });
                        }

                        CurrentUser.User   = callbackQuery.From;
                        CurrentUser.ChatId = message.Chat.Id;
                        TimeoutTimer.Start();

                        break;

                    case _questionEnterName:
                        TimeoutTimer.Stop();
                        question = _questionEnterName;
                        await _bot.SendTextMessageAsync(message.Chat.Id, $"@{callbackQuery.From.Username} \n" + _questionEnterName, replyMarkup : new ForceReplyMarkup {
                            Selective = true
                        });

                        TimeoutTimer.Start();
                        break;

                    case "Security":
                    case "NoSecurity":
                        TimeoutTimer.Stop();
                        // var prevQuestion = await _telegramBotHistoryRepository.GetLatestAsync(x => x.ChatId == message.Chat.Id && x.UserId == callbackQuery.From.Id);
                        var prevQuestion = _history.LastOrDefault();

                        if (prevQuestion == null || prevQuestion.Question != _questionSecurity)
                        {
                            await SendTextToUser(message.Chat.Id);

                            break;
                        }


                        await CreateBotHistory(message.Chat.Id, callbackQuery.From.Id, callbackQuery.From.Username, question, callbackQuery.Data);

                        historyCreateSkipped = true;
                        callbackQuery.Data   = "NoCore";
                        await SendResponseMarkup(callbackQuery, message);

                        break;

                    case "Core":
                    case "NoCore":
                        TimeoutTimer.Stop();
                        // prevQuestion = await _telegramBotHistoryRepository.GetLatestAsync(x => x.ChatId == message.Chat.Id && x.UserId == callbackQuery.From.Id);
                        prevQuestion = _history.LastOrDefault();
                        if (prevQuestion == null || prevQuestion.Question != _questionMultipleTeams)
                        {
                            await SendTextToUser(message.Chat.Id);

                            break;
                        }
                        prevQuestion.Answer = callbackQuery.Data;
                        await CreateBotHistory(message.Chat.Id, callbackQuery.From.Id, callbackQuery.From.Username, "Creating repository");

                        await SendTextToUser(message.Chat.Id, $"@{callbackQuery.From.Username} \n" + "Creating repository. Please wait...");

                        question = await CreateRepoAsync(message.Chat.Id, callbackQuery.From.Id);

                        saveHistory = true;

                        break;

                    case _resetTeam:
                        TimeoutTimer.Stop();
                        await ClearTeam(message.Chat.Id, callbackQuery.From);

                        break;
                    }

                    if (!historyCreateSkipped)
                    {
                        await CreateBotHistory(message.Chat.Id, callbackQuery.From.Id, callbackQuery.From.Username, question, callbackQuery.Data);
                    }

                    if (saveHistory)
                    {
                        await SaveHistory();
                    }
                }
            }
            catch (Exception ex)
            {
                await _log.WriteErrorAsync("TelegramBotService.SendResponseMarkup", callbackQuery.Data, ex);

                return;
            }
        }