This repository was archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathStartup.cs
executable file
·96 lines (84 loc) · 3.41 KB
/
Startup.cs
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 System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Telegram.Bot.Abstractions;
using Telegram.Bot.Framework;
namespace SampleEchoBot
{
public class Startup
{
private readonly IConfigurationRoot _configuration;
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
_configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
// services.AddTelegramBot<EchoBot>(_configuration.GetSection("EchoBot"))
//// .AddUpdateHandler<EchoCommand>()
// .AddUpdateHandler<>()
// .Configure();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(_configuration.GetSection("Logging"));
loggerFactory.AddDebug();
ILogger logger = loggerFactory.CreateLogger<Startup>();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
var source = new CancellationTokenSource();
Task.Factory.StartNew(() =>
{
logger.LogDebug("Press Enter to stop bot manager...");
Console.ReadLine();
source.Cancel();
});
Task.Factory.StartNew(async () =>
{
var botManager = app.ApplicationServices.GetRequiredService<IBotManager<EchoBot>>();
// make sure webhook is disabled so we can use long-polling
await botManager.SetWebhookStateAsync(false);
logger.LogDebug("Webhook is disabled. Staring update handling...");
while (!source.IsCancellationRequested)
{
await Task.Delay(3_000);
await botManager.GetAndHandleNewUpdatesAsync();
}
logger.LogDebug("Bot manager stopped.");
}).ContinueWith(t =>
{
if (t.IsFaulted) throw t.Exception;
});
}
else
{
app.UseExceptionHandler(appBuilder =>
appBuilder.Run(context =>
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
return Task.CompletedTask;
})
);
logger.LogInformation($"Setting webhook for {nameof(EchoBot)}...");
// app.UseTelegramBotWebhook<EchoBot>();
logger.LogInformation("Webhook is set for bot " + nameof(EchoBot));
}
app.Run(async context =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
}