public TelegramStorage(TelegramStorageOptions opt, ITelegramBotClient botClient)
 {
     if (opt == null)
     {
         throw new ArgumentNullException(nameof(opt));
     }
     _botClient              = botClient;
     _saveResChatId          = opt.SaveResourcesChatId;
     _storageFilePath        = opt.FilePath;
     _deletePreviousMessages = opt.DeletePreviousMessages;
     if (opt.LoadOnGet)
     {
         _loadOnGet = true;
         _saveOnSet = true;
     }
     else if (opt.SaveOnSet)
     {
         _saveOnSet = true;
     }
     else if (opt.AutoSavesDelay.HasValue)
     {
         _autoSaveEnabled = true;
         _autoSaveDelay   = opt.AutoSavesDelay.Value;
     }
     ForceLoad().Wait();
 }
Ejemplo n.º 2
0
        public void Run(BotManager botManager, bool isDebug)
        {
            botManager.ConfigureServices((servicesWrap) =>
            {
                var serv = servicesWrap.Services;
                LoggerStarter.InitLogger(servicesWrap);
                servicesWrap.AddMvc(new MvcOptions()
                {
                    CheckEqualsRouteInfo = true
                });

                serv.AddSingleton <ITelegramBotClient>((sp) =>
                {
                    return(botManager.BotContext.Bot);
                });
                serv.AddScoped <ISomeScopedService, SomeScopedService>();


                //Telegram storage test
                var opt = new TelegramStorageOptions()
                {
                    SaveResourcesChatId = BotTokenResolver.GetSaveResChatId(),
                    SaveOnSet           = true
                };
                serv.AddSingleton(opt);
                serv.AddSingleton <TelegramStorage>();
                serv.AddSingleton <IKeyValueStorage>((sp) =>
                {
                    return(sp.GetRequiredService <TelegramStorage>());
                });
                serv.AddSingleton <TelegramFilesCloud>();
            });

            botManager.ConfigureBuilder((builder) =>
            {
                if (isDebug)
                {
                    builder.UseDevEceptionMessage();
                }

                builder.UseExceptionHandler(async(ctx, ex) =>
                {
                    //Throw exception if false. False mean 'not handled'.
                    return(false);
                });

                builder.UseMvc(mvcBuilder =>
                {
                    mvcBuilder.UseDebugInfo();
                    mvcBuilder.MapRouteAction(async(actionCtx) =>
                    {
                        await actionCtx.UpdateContext.SendTextMessageAsync("Mvc works.");
                    }, template: "/mvc");
                });
            });
            botManager.Start();
        }
Ejemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //Increase file upload size.
            services.Configure <FormOptions>(x =>
            {
                x.ValueLengthLimit         = AppSettings.MaxFileSize;
                x.MultipartBodyLengthLimit = AppSettings.MaxFileSize; // In case of multipart
            });

            services.AddControllers();
            services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
            {
                builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));
            StartupInit.AddSwaggerGen_Local(services);
            services.AddMvcExceptionHandler();

            //Telegram bot part.
            var bot = new TelegramBotClient(
                AppSettings.TG_BOT_TOKEN,
                new QueuedHttpClient(TimeSpan.FromMilliseconds(50))
                );

            services.AddSingleton <ITelegramBotClient>(bot);

            //Telegram storage part.
            var opt = new TelegramStorageOptions()
            {
                SaveResourcesChatId = AppSettings.TG_SAVE_RESOURCES_CHAT
            };

            services.AddSingleton(opt);
            services.AddSingleton <IKeyValueStorage, TelegramStorage>();
            //Telegram files limit is 50 mb.
            //Note that with CacheAndNotWait you can operate with any files, but too big files will be not saved in telegram,
            //so they will be unavailable after app restart.
            services.AddSingleton(new TelegramFilesCloudOptions()
            {
                SaveResourcesChatId = AppSettings.TG_SAVE_RESOURCES_CHAT,
                CacheAndNotWait     = true,
                DeleteOlderFiles    = false
            });
            services.AddSingleton <TelegramFilesCloud <FileMetadata> >();
        }