예제 #1
0
        static void Main(string[] args)
        {
            SqlConnection con = new SqlConnection();

            con.ConnectionString = @"Integrated Security = SSPI; data source = DESKTOP-LT9TUC4\SQLEXPRESS; Initial Catalog = Bot";
            Console.Title        = "Telegram Bot";
            SqlCommand    command;
            SqlDataReader reader;

            var builder = new ConfigurationBuilder()
                          .AddJsonFile($"appsettings.json", true, true);

            var configuration = builder.Build();

            using (var webClient = new WebClient())
            {
                con.Open();

                var fileProvider          = new FileChatProvider();
                var appConfig             = configuration.GetSection("TelegramBot").Get <TelegramBotSettings>();
                var webContentDownloader  = new WebContentDownloader(webClient);
                var telegramUpdatesReader = new TelegramUpdatesReader(appConfig, webContentDownloader);
                var chatBotState          = new ChatBotState(fileProvider);
                chatBotState.Load();

                var pollingWatcher = new TelegramChatUpdatesPollingWatcher(telegramUpdatesReader, chatBotState);

                var cancellationTokenSource = new CancellationTokenSource();

                pollingWatcher.MessageIsArrived += (msg) =>
                {
                    try
                    {
                        command = new SqlCommand("SELECT ID_USER_TELEGRAM FROM USERS WHERE ID_USER_TELEGRAM = @val", con);
                        command.Parameters.Add("@val", SqlDbType.Int).Value = msg.from.id;
                        reader = command.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                var val = reader.GetValue(0);
                                if (msg.from.id.ToString() != val.ToString())
                                {
                                    InsertDataToDB(msg.from.id, msg.from.username);
                                    break;
                                }
                            }
                            reader.Close();
                        }
                        else if (!reader.HasRows)
                        {
                            InsertDataToDB(msg.from.id, msg.from.username);
                        }
                        reader.Close();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    Console.WriteLine($"{msg.text} from {msg.from.username}");
                };

                pollingWatcher.StartWatch(cancellationTokenSource.Token);

                Console.ReadLine();

                cancellationTokenSource.Cancel();

                con.Close();
                chatBotState.Save();
            }
        }
예제 #2
0
 public void WebContentDownloader_DownloadGpxFile_FileExists()
 {
     File.Delete(TestPaths.Route05);
     WebContentDownloader.DownloadContent(TestPaths.Route05);
     Assert.AreEqual(true, File.Exists(TestPaths.Route05), "File: route05.gpx was not downloaded");
 }