コード例 #1
0
        public override async Task Execute(Message message, TelegramBotClient botClient)
        {
            var chatId = message.Chat.Id;

            parser = new ParserWorker <Replacement[]>(
                new BcpepParser(),
                new BcpepSettings()
                );
            parser.Start();

            string group  = (message.Text.Length > Name.Length + 1) ? message.Text.Substring(Name.Length + 1).Trim() : "";
            string botMsg = "Error: Некоректні дані!";
            //Replacement[] data = null;
            Replacement result = null;

            Replacement[] data = await parser.GetGroups();

            if (group.Length < 10 && group.Length > 2)
            {
                if (data != null)
                {
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (data[i].Group.Contains(group))
                        {
                            result = data[i];
                            break;
                        }
                    }
                    if (result != null)
                    {
                        //await botClient.SendTextMessageAsync(chatId, result.ToString(), parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown);
                        botMsg = result.ToString();
                    }
                    else
                    {
                        //await botClient.SendTextMessageAsync(chatId, $"Error: Не вдалося знайти групу \"{group}\"", parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown);
                        botMsg = $"Error: Не вдалося знайти групу \"{group}\"!";
                    }
                }
                else
                {
                    //await botClient.SendTextMessageAsync(chatId, $"Error: Помилка підключення до bcpep.org.ua", parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown);
                    botMsg = $"Error: Помилка підключення до bcpep.org.ua!";
                }
            }
            await botClient.SendTextMessageAsync(chatId, botMsg, parseMode : Telegram.Bot.Types.Enums.ParseMode.Default);

            parser.Abort();
        }
コード例 #2
0
        private async void CheckReplacementUpdates(object state)
        {
            _logger.LogInformation("Timed Background Service is working.");
            parser.Start();
            //DateTime from web, because time in PaaS and here is different
            //configurate Heroku to heroku config:add TZ="Europe/Kiev"
            DateTime dd = DateTime.Now;

            if (dd.Hour < 11 && currentReplacementDate == null)
            {
                currentReplacementDate = await parser.GetDate();
            }

            //if (dd.Hour == 11 && dd.Minute == 0 && isSent == false)
            if (dd.Hour >= 11 && dd.Hour <= 16 && isSent == false)
            {
                newReplacementDate = await parser.GetDate();

                //DEBUG:
                _logger.LogInformation($"Current: {currentReplacementDate}; New: {newReplacementDate}");

                if (currentReplacementDate != null && currentReplacementDate != newReplacementDate)
                {
                    using (var connection = new NpgsqlConnection(AppSettings.ConnString))
                    {
                        connection.Open();
                        string        group  = "";
                        Replacement   result = null;
                        Replacement[] data   = null;
                        ChatId        chatId = null;
                        using (var cmd = new NpgsqlCommand())
                        {
                            cmd.Connection  = connection;
                            cmd.CommandText = @"SELECT * FROM users";
                            using (var reader = await cmd.ExecuteReaderAsync())
                            {
                                while (await reader.ReadAsync())
                                {
                                    group  = reader["groupname"].ToString();
                                    chatId = new ChatId(int.Parse(reader["chatid"].ToString()));
                                    result = null;
                                    data   = await parser.GetGroups();

                                    if (data != null)
                                    {
                                        for (int i = 0; i < data.Length; i++)
                                        {
                                            if (data[i].Group.Contains(group))
                                            {
                                                result = data[i];
                                                break;
                                            }
                                        }
                                        if (result != null)
                                        {
                                            Bot.SendMessageAsync(chatId, result.ToString());
                                            //_logger.LogInformation($"Sended to {int.Parse(reader["chatid"].ToString())}");
                                        }
                                        else
                                        {
                                            Bot.SendMessageAsync(chatId, $"На {newReplacementDate} замін немає.");
                                        }
                                        isSent = true;
                                    }
                                }
                            }
                        }
                    }
                    currentReplacementDate = null;
                }
                //else
                //_logger.LogInformation("There nothing to send! On the site non actual data");
                //Thread.Sleep(TimeSpan.FromMinutes(1));
            }
            else if (dd.Hour < 11 && dd.Hour > 16)
            {
                isSent = false;
            }
            parser.Abort();
        }