示例#1
0
        /// <summary>
        ///
        /// </summary>
        public virtual void Run()
        {
            var options = TelegramBotConfig.GetOptions(AppConfig.OptionsName);

            if (options == null)
            {
                throw new Exception($"Telegram bot options not configured! Options name: {AppConfig.OptionsName}");
            }

            _container        = options.Container;
            _servicesProvider = options.ServicesProvider;

            options.ServicesRegistrator?.Register(_container, options);
            _container.Initialize();

            var commandFactory = _servicesProvider.GetService <ITelegramBotCommandFactory>();

            commandFactory.Initialize();
            AppConfig.Configure(this);

            var clientManager = _servicesProvider.GetService <ITelegramBotClientManager>();

            clientManager.Token = AppConfig.Token;
            clientManager.Initialize();
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="services"></param>
        public static void AddTelegramBot(this IServiceCollection services, string optionKey)
        {
            var container = new TelegramBotAspNetCoreServicesContainer(services);
            var provider  = new TelegramBotAspNetCoreServicesProvider();

            var options = TelegramBotConfig.GetOptions(optionKey);

            options.Container        = container;
            options.ServicesProvider = provider;

            options.ServicesRegistrator.Register(container, options);
            options.ServicesRegistrator = null;
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="app"></param>
        /// <param name="optionKey"></param>
        /// <param name="optionKey"></param>
        public static void UseTelegramBot(this IApplicationBuilder app, string botToken, string optionKey)
        {
            var option           = TelegramBotConfig.GetOptions(optionKey);
            var servicesProvider = (TelegramBotAspNetCoreServicesProvider)option.ServicesProvider;

            servicesProvider.ServiceProvider = app.ApplicationServices;

            var config = new TelegramBotAppConfig();

            config.OptionsName = optionKey;
            config.Token       = botToken;

            TelegramBotApplication.Run(config);
        }
示例#4
0
        public MainWorker(ILogger <MainWorker> logger)
        {
            _logger  = logger;
            _tvShows = new SonarrRepository();
            logger.LogInformation("Generating config from the appsettings config file");
            var config = new ConfigurationBuilder()
                         .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                         .AddJsonFile("appsettings.json").Build();
            var section = config.GetSection(nameof(TelegramBotConfig));

            _telegramBotConfig = section.Get <TelegramBotConfig>();

            botClient                  = new TelegramBotClient(_telegramBotConfig.ApiKey);
            botClient.OnMessage       += Bot_OnMessage;
            botClient.OnCallbackQuery += Bot_OnCallback;
        }