Esempio n. 1
0
 public TableTennisBot(
     TelegramBotConfiguration configuration,
     RealTimeRetriever realTimeRetriever,
     IChatsRepository chatsRepository,
     IAccessTokenRepository accessTokenRepository)
 {
     _configuration         = configuration ?? throw new ArgumentNullException(nameof(configuration));
     _realTimeRetriever     = realTimeRetriever ?? throw new ArgumentNullException(nameof(realTimeRetriever));
     _chatsRepository       = chatsRepository ?? throw new ArgumentNullException(nameof(chatsRepository));
     _accessTokenRepository =
         accessTokenRepository ?? throw new ArgumentNullException(nameof(accessTokenRepository));
     _botClient            = new TelegramBotClient(_configuration.AccessToken);
     _botClient.OnMessage += MessageHandler;
     _realTimeRetriever.OnGoodBigScorePercentageFound += GoodBigScorePercentageHandler;
     _botClient.StartReceiving();
 }
Esempio n. 2
0
        public static void Main()
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", false);

            var configuration = builder.Build();
            var tbc           = new TelegramBotConfiguration();
            var rtrc          = new RealTimeRetrieverConfiguration();

            configuration.Bind(nameof(TelegramBotConfiguration), tbc);
            configuration.Bind(nameof(RealTimeRetrieverConfiguration), rtrc);


            var serviceProvider = new ServiceCollection()
                                  .AddSingleton(tbc)
                                  .AddSingleton(rtrc)
                                  .AddSingleton <RealTimeRetriever>()
                                  .AddSingleton <IBetsApiClient, BetsApiClient>(isp => new BetsApiClient(rtrc.BetsApiUrl))
                                  .AddSingleton <IAccessTokenRepository, AccessTokenRepository>()
                                  .AddSingleton <IChatsRepository, ChatsRepository>()
                                  .AddSingleton <IEventsRepository, EventsRepository>()
                                  .AddSingleton <TableTennisBot>()
                                  .AddDbContext <PostgreSqlDbContext>(db => db.UseNpgsql(configuration.GetConnectionString("PostgreSql")))
                                  .AddDbContextPool <PostgreSqlDbContext>(db =>
                                                                          db.UseNpgsql(configuration.GetConnectionString("PostgreSql")))
                                  .AddSingleton <Func <PostgreSqlDbContext> >(isp => isp.GetService <PostgreSqlDbContext>)
                                  .BuildServiceProvider();

            var tBot = serviceProvider.GetService <TableTennisBot>();

            serviceProvider.GetService <RealTimeRetriever>().StartAsync().Wait();

            new ManualResetEvent(false).WaitOne();
            GC.KeepAlive(tBot);
        }