Пример #1
0
        static async void Bot_OnMessage(object sender, MessageEventArgs e)
        {
            if (e.Message.Text != null)
            {
                //Bot start
                if (e.Message.Text == "/start")
                {
                    logger.Info($"User with id {e.Message.Chat.Id} started using bot");
                    await botClient.SendTextMessageAsync(
                        chatId : e.Message.Chat,
                        text : "Welcome to LiveScoreBot! Select command entering / and follow bot's instructions."
                        );
                }

                //Live matches
                if (e.Message.Text == "/live")
                {
                    logger.Info($"User with id {e.Message.Chat.Id} requested live matches");
                    try
                    {
                        var competitionsInPlay = matchRequestService.GetCompetitions(isLive: true);
                        logger.Debug($"There was request for api.football-data.org");
                        var champioshipsListButtons = MarkupCreator.CreateChampioshipsListButtons(competitionsInPlay, isLive: true);
                        await botClient.SendTextMessageAsync(
                            chatId : e.Message.Chat,
                            text : "Please select the competition",
                            replyMarkup : champioshipsListButtons
                            );
                    }
                    catch (Exception)
                    {
                        logger.Error($"There has been an error getting live matches.");
                        await botClient.SendTextMessageAsync(
                            chatId : e.Message.Chat,
                            text : "There has been an error getting live matches.");
                    }
                }

                //Scheduled matches
                if (e.Message.Text == "/schedule")
                {
                    try
                    {
                        logger.Info($"User with id {e.Message.Chat.Id} requested scheduled matches");
                        var competitions = matchRequestService.GetCompetitions();
                        logger.Debug($"There was request for api.football-data.org");
                        var champioshipsListButtons = MarkupCreator.CreateChampioshipsListButtons(competitions);
                        await botClient.SendTextMessageAsync(
                            chatId : e.Message.Chat,
                            text : "Please select the competition",
                            replyMarkup : champioshipsListButtons
                            );
                    }
                    catch (Exception)
                    {
                        logger.Error($"There has been an error getting scheduled matches.");
                        await botClient.SendTextMessageAsync(
                            chatId : e.Message.Chat,
                            text : "There has been an error getting scheduled matches.");
                    }
                }

                //Favourite matches
                if (e.Message.Text == "/fav")
                {
                    try
                    {
                        logger.Info($"User with id {e.Message.Chat.Id} requested favourite matches");
                        var favouriteMatches = userFavouriteService.GetUserFavourites(e.Message.Chat.Id);
                        logger.Debug($"Favourite matches for user id {e.Message.Chat.Id} were got from Database");
                        var allMatches = matchRequestService.GetAllMatches();
                        logger.Debug($"There was request for api.football-data.org");
                        var matches = new List <Match>();

                        foreach (var match in allMatches)
                        {
                            foreach (var favouriteMatch in favouriteMatches)
                            {
                                if (favouriteMatch.MatchId == match.Id)
                                {
                                    matches.Add(match);
                                }
                            }
                        }

                        var matchListButtons = MarkupCreator.CreateMatchListButtons(matches, true);

                        await botClient.SendTextMessageAsync(
                            chatId : e.Message.Chat,
                            text : "Select match from favourite list",
                            replyMarkup : matchListButtons
                            );
                    }
                    catch (Exception)
                    {
                        logger.Error($"There has been an error getting favourite matches.");
                        await botClient.SendTextMessageAsync(
                            chatId : e.Message.Chat,
                            text : "There has been an error getting favourite matches.");
                    }
                }

                //Information
                if (e.Message.Text == "/info")
                {
                    logger.Info($"User with id {e.Message.Chat.Id} requested information");
                    var infoButtons = MarkupCreator.CreateInfoButtons();

                    await botClient.SendTextMessageAsync(
                        chatId : e.Message.Chat,
                        text : "Select what do you want to see information about",
                        replyMarkup : infoButtons
                        );
                }

                //Stadium input
                if (e.Message.Text.Contains("stadium"))
                {
                    var stadiumName = e.Message.Text.Substring(8);
                    var stadium     = stadiumService.GetStadiumByName(stadiumName);
                    logger.Debug($"Stadium with name {stadiumName} were got from Database");

                    if (stadium != null)
                    {
                        await botClient.SendPhotoAsync(
                            e.Message.Chat,
                            stadium.PictureUrl,
                            $@"Name: {stadium.Name}
HomeTeam: {stadium.HomeTeam}
Opened: {stadium.Opened}
Capacity: {stadium.Capacity}
FifaRate: {stadium.FifaRate} stars");
                    }
                    else
                    {
                        await botClient.SendTextMessageAsync(
                            chatId : e.Message.Chat,
                            text : "Sorry, stadium with this name hasn't been found. Try one more time!"
                            );
                    }
                }
            }
        }