Skip to content

Commit 0472429

Browse files
author
Igor Evdokimov
committed
- timeout for telegram
1 parent 0598310 commit 0472429

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

Botticelli.Framework.Telegram/Extensions/ServiceCollectionExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ public static IServiceCollection AddTelegramBot(this IServiceCollection services
6666

6767
var sp = services.BuildServiceProvider();
6868

69-
var bot = new TelegramBot(new TelegramBotClient(token),
69+
var telegramClient = new TelegramBotClient(token)
70+
{
71+
Timeout = TimeSpan.FromMilliseconds(settings.Timeout)
72+
};
73+
74+
var bot = new TelegramBot(telegramClient,
7075
sp.GetRequiredService<IBotUpdateHandler>(),
7176
sp.GetRequiredService<ILogger<TelegramBot>>(),
7277
sp.GetRequiredService<MetricsProcessor>(),

Botticelli.Framework.Telegram/Options/TelegramBotSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ namespace Botticelli.Framework.Telegram.Options;
55
/// <inheritdoc />
66
public class TelegramBotSettings : BotSettings
77
{
8+
/// <summary>
9+
/// Timeout in ms
10+
/// </summary>
11+
public int Timeout { get; set; } = 60000;
812
}

Botticelli.Framework.Telegram/TelegramBot.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ protected override async Task<SendMessageResponse> InnerSendMessageAsync<TSendOp
145145

146146
var text = new StringBuilder($"{request.Message.Subject} {request.Message.Body}");
147147
var retText = Escape(text).ToString();
148-
List<(string chatId, string innerId)> pairs = new();
148+
List<(string chatId, string innerId)> pairs = [];
149149

150150
foreach (var link in request.Message.ChatIdInnerIdLinks)
151151
pairs.AddRange(link.Value.Select(innerId => (link.Key, innerId)));
@@ -170,7 +170,7 @@ protected override async Task<SendMessageResponse> InnerSendMessageAsync<TSendOp
170170
else
171171
{
172172
Logger.LogWarning(@"Streaming output isn't supported for Telegram now!");
173-
await SendText(@"Sorry, but streaming output isn't supported for Telegram now\!");
173+
await SendText(@"Sorry, but streaming output isn't supported for Telegram now!");
174174
}
175175

176176
async Task SendText(string sendText)
@@ -403,7 +403,7 @@ private static StringBuilder Escape(StringBuilder text) =>
403403
/// <param name="request"></param>
404404
/// <param name="token"></param>
405405
/// <returns></returns>
406-
protected override async Task<StartBotResponse> InnerStartBotAsync(StartBotRequest request, CancellationToken token)
406+
protected override Task<StartBotResponse> InnerStartBotAsync(StartBotRequest request, CancellationToken token)
407407
{
408408
try
409409
{
@@ -414,7 +414,7 @@ protected override async Task<StartBotResponse> InnerStartBotAsync(StartBotReque
414414
{
415415
Logger.LogInformation($"{nameof(StartBotAsync)}: already started");
416416

417-
return response;
417+
return Task.FromResult(response);
418418
}
419419

420420
BotStatusKeeper.IsStarted = true;
@@ -430,14 +430,14 @@ protected override async Task<StartBotResponse> InnerStartBotAsync(StartBotReque
430430

431431
Logger.LogInformation($"{nameof(StartBotAsync)}: started");
432432

433-
return response;
433+
return Task.FromResult(response);
434434
}
435435
catch (Exception ex)
436436
{
437437
Logger.LogError(ex, ex.Message);
438438
}
439439

440-
return StartBotResponse.GetInstance(AdminCommandStatus.Fail, "error");
440+
return Task.FromResult(StartBotResponse.GetInstance(AdminCommandStatus.Fail, "error"));
441441
}
442442

443443
/// <summary>
@@ -487,10 +487,7 @@ public override async Task SetBotKey(string key, CancellationToken token)
487487
}
488488
}
489489

490-
private void RecreateClient(string key)
491-
{
492-
_client = new TelegramBotClient(key);
493-
}
490+
private void RecreateClient(string key) => _client = new TelegramBotClient(key);
494491

495492
private async Task StartBot(CancellationToken token)
496493
{

Samples/TelegramInlineLayoutsSample/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
{
2828
ConnectionString = settings.SecureStorageConnectionString
2929
})
30+
.Set(s => s.Timeout = 20000)
3031
.Set(s => s.Name = "test_bot"))
3132
.AddLogging(cfg => cfg.AddNLog())
3233
.AddBotCommand<GetCalendarCommand, GetCalendarCommandProcessor, PassValidator<GetCalendarCommand>>()

0 commit comments

Comments
 (0)