Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (75 loc) · 3.89 KB
/
Copy pathProgram.cs
File metadata and controls
96 lines (75 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Telegram.Bot;
using TelegramMediaGrabberBot;
using TelegramMediaGrabberBot.Config;
using TelegramMediaGrabberBot.Services;
using TelegramMediaGrabberBot.TelegramHandler;
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
ILogger logger = services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
IConfiguration configuration = hostContext.Configuration;
TelegramBotConfig? telegramBotConfig = configuration
.GetSection("Telegram.Bot.Config")
.Get<TelegramBotConfig>();
List<long?>? whiteListedGroups = hostContext.Configuration.GetSection("WhitelistedGroupIds").Get<List<long?>>();
logger.LogInformation("whiteListedGroups {whiteListedGroups}", whiteListedGroups);
List<string>? nitterInstances = hostContext.Configuration.GetSection("NitterInstances").Get<List<string>>();
logger.LogInformation("nitterInstances {nitterInstances}", nitterInstances);
List<string>? instagramProxies = hostContext.Configuration.GetSection("InstagramProxies").Get<List<string>>();
logger.LogInformation("instagramProxies {instagramProxies}", instagramProxies);
List<string>? supportedWebSites = hostContext.Configuration.GetSection("SupportedWebSites").Get<List<string>>();
logger.LogInformation("supportedWebSites {supportedWebSites}", supportedWebSites);
int? hoursBetweenBackgroundTask = hostContext.Configuration.GetValue<int?>("HoursBetweenBackgroundTask");
logger.LogInformation("HoursBetweenBackgroundTask {hoursBetweenBackgroundTask}", hoursBetweenBackgroundTask);
BlueSkyAuth? blueSkyAuth = configuration
.GetSection("BlueSkyAuth")
.Get<BlueSkyAuth>();
InstagramAuth? instagramAuth = configuration
.GetSection("InstagramAuth")
.Get<InstagramAuth>();
AppSettings appSettings = new()
{
TelegramBotConfig = telegramBotConfig,
WhitelistedGroups = whiteListedGroups,
NitterInstances = nitterInstances,
InstagramProxies = instagramProxies,
SupportedWebSites = supportedWebSites,
HoursBetweenBackgroundTask = hoursBetweenBackgroundTask,
BlueSkyAuth = blueSkyAuth,
InstagramAuth = instagramAuth
};
_ = services.AddSingleton<AppSettings>(appSettings);
_ = services.AddHostedService<ClearTempBackgroundService>();
_ = services.AddHttpClient("telegram_bot_client")
.AddTypedClient<ITelegramBotClient>((httpClient, sp) =>
{
if (telegramBotConfig != null && telegramBotConfig.BotToken != null)
{
TelegramBotClientOptions options = new(telegramBotConfig.BotToken);
return new TelegramBotClient(options, httpClient);
}
throw new NullReferenceException(nameof(telegramBotConfig));
});
// .AddPolicyHandler(GetRetryPolicy())
// .AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMessage>(20));
_ = services.AddHttpClient("default",
client =>
{
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35");
}
);
//.AddPolicyHandler(GetRetryPolicy())
// .AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMessage>(20)); ;
_ = services.AddSingleton<TelegramUpdateHandler>();
_ = services.AddSingleton<TelegramReceiverService>();
_ = services.AddHostedService<TelegramPollingService>();
})
.Build();
try
{
await host.RunAsync();
}
catch (Exception ex)
{
ApplicationLogging.CreateLogger("Program.cs").LogError(ex, "program.cs error");
}