< Summary

Information
Class: GistBackend.Handlers.TelegramBotClientHandler.TelegramBotClientHandler
Assembly: GistBackend
File(s): /home/runner/work/the-gist-of-it-sec/the-gist-of-it-sec/backend/GistBackend/Handlers/TelegramBotClientHandler/TelegramBotClientHandler.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 15
Coverable lines: 15
Total lines: 44
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
StartBotClient(...)0%620%
SendMessageAsync()0%2040%

File(s)

/home/runner/work/the-gist-of-it-sec/the-gist-of-it-sec/backend/GistBackend/Handlers/TelegramBotClientHandler/TelegramBotClientHandler.cs

#LineLine coverage
 1using GistBackend.Utils;
 2using Microsoft.Extensions.Logging;
 3using Microsoft.Extensions.Options;
 4using Telegram.Bot;
 5using Telegram.Bot.Types;
 6using Telegram.Bot.Types.Enums;
 7using static Telegram.Bot.TelegramBotClient;
 8
 9namespace GistBackend.Handlers.TelegramBotClientHandler;
 10
 11public interface ITelegramBotClientHandler
 12{
 13    public void StartBotClient(IEnumerable<BotCommand> commands, OnMessageHandler onMessage, OnErrorHandler onError,
 14        CancellationToken ct);
 15    public Task SendMessageAsync(long chatId, string text, ParseMode parseMode = ParseMode.None);
 16}
 17
 018public class TelegramBotClientHandler(
 019    IOptions<TelegramBotClientHandlerOptions> options,
 020    ILogger<TelegramBotClient>? logger) : ITelegramBotClientHandler
 21{
 22    private TelegramBotClient? _botClient;
 23    private CancellationToken? _cancellationToken;
 24
 25    public void StartBotClient(IEnumerable<BotCommand> commands, OnMessageHandler onMessage, OnErrorHandler onError,
 26        CancellationToken ct)
 27    {
 028        _cancellationToken = ct;
 029        if (string.IsNullOrWhiteSpace(options.Value.BotToken))
 030            throw new ArgumentException("Bot token is not set in the options.");
 031        _botClient = new TelegramBotClient(options.Value.BotToken, cancellationToken: ct);
 032        _botClient.SetMyCommands(commands, cancellationToken: ct).GetAwaiter().GetResult();
 033        _botClient.OnMessage += onMessage;
 034        _botClient.OnError += onError;
 035    }
 36
 37    public async Task SendMessageAsync(long chatId, string text, ParseMode parseMode = ParseMode.None)
 38    {
 039        if (_botClient is null) throw new InvalidOperationException("Bot client is not initialized.");
 40
 041        await _botClient.SendMessage(chatId, text, parseMode, cancellationToken: _cancellationToken!.Value);
 042        logger?.LogInformation(LogEvents.SentTelegramMessage, "Sent message to chat {ChatId}: {Text}", chatId, text);
 043    }
 44}