mirror of
https://github.com/yawaflua/Telegram.Net.git
synced 2025-12-09 20:19:28 +02:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
529378c0bb | ||
|
|
2371180bd4 | ||
|
|
a691064881 | ||
|
|
0a7e3d4b49 | ||
|
|
7b7b2effca |
67
CONTRIBUTING.md
Normal file
67
CONTRIBUTING.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# Contributing to Telegram.Net
|
||||
|
||||
Thank you for your interest in contributing to Telegram.Net! We welcome contributions from anyone in the community, regardless of experience level.
|
||||
|
||||
## How to Contribute
|
||||
|
||||
### Getting Started
|
||||
|
||||
1. Fork the repository at [https://github.com/yawaflua/Telegram.Net](https://github.com/yawaflua/Telegram.Net)
|
||||
2. Clone your fork to your local machine
|
||||
3. Set up the development environment with the required .NET SDK
|
||||
|
||||
### Making Changes
|
||||
|
||||
1. Create a new branch for your feature or bug fix:
|
||||
```
|
||||
git checkout -b feature/your-feature-name
|
||||
```
|
||||
2. Make your changes
|
||||
3. Write or update tests for the changes you made using appropriate testing frameworks (e.g., MSTest, NUnit, or xUnit)
|
||||
4. Ensure your code passes all tests and meets C# coding conventions
|
||||
|
||||
## Code Quality Requirements
|
||||
|
||||
### Test Coverage
|
||||
|
||||
- **All pull requests must include tests with at least 50% code coverage**
|
||||
- Before submitting your PR, verify coverage using tools like:
|
||||
```
|
||||
# Example using Coverlet and ReportGenerator
|
||||
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
|
||||
dotnet tool run reportgenerator -reports:"**/coverage.opencover.xml" -targetdir:"coveragereport" -reporttypes:Html
|
||||
```
|
||||
|
||||
### Coding Standards
|
||||
|
||||
- Follow C# coding conventions and the existing style in the project
|
||||
- Use meaningful names for classes, methods, and variables
|
||||
- Include XML documentation comments for public APIs
|
||||
- Keep methods focused and reasonably sized
|
||||
|
||||
## Pull Request Process
|
||||
|
||||
1. Update documentation if you're changing or adding functionality
|
||||
2. Ensure your code has adequate test coverage (minimum 50%)
|
||||
3. Submit your pull request with a clear title and description
|
||||
4. Reference any relevant issues in your PR description
|
||||
|
||||
## Review Process
|
||||
|
||||
- All submissions require review before being merged
|
||||
- Maintainers may request changes or suggest improvements
|
||||
- Be responsive to feedback on your pull request
|
||||
|
||||
## C# Specific Guidelines
|
||||
|
||||
- Target the same .NET version as the project
|
||||
- Avoid excessive dependencies unless absolutely necessary
|
||||
- Follow SOLID principles when applicable
|
||||
- Use async/await patterns appropriately for asynchronous operations
|
||||
- Be mindful of performance implications, especially for a networking library
|
||||
|
||||
## Questions?
|
||||
|
||||
If you have any questions about contributing to Telegram.Net, feel free to open an issue for discussion.
|
||||
|
||||
Thank you for helping improve Telegram.Net!
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices.JavaScript;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -15,31 +14,32 @@ using static System.Reflection.BindingFlags;
|
||||
|
||||
namespace Telegram.Net.Services;
|
||||
|
||||
[SuppressMessage("ReSharper", "ReturnValueOfPureMethodIsNotUsed")]
|
||||
public class TelegramHostedService : IHostedService
|
||||
{
|
||||
private IServiceCollection isc { get; }
|
||||
internal TelegramBotClient Client { get; set; }
|
||||
private ITelegramBotConfig Config { get; }
|
||||
internal Dictionary<string, Func<ITelegramBotClient, Message, CancellationToken, Task>> CommandHandler { get; set; } = new();
|
||||
internal List<Func<ITelegramBotClient, Message, CancellationToken, Task>> EditedMessageHandler { get; set; } = new();
|
||||
internal Dictionary<string, Func<ITelegramBotClient, CallbackQuery,CancellationToken, Task>> CallbackQueryHandler { get; set; } = new();
|
||||
internal Dictionary<string, Func<ITelegramBotClient, InlineQuery ,CancellationToken, Task>> InlineHandler { get; set; } = new();
|
||||
private IServiceCollection ServiceCollection { get; } = null!;
|
||||
internal TelegramBotClient Client { get; set; } = null!;
|
||||
private ITelegramBotConfig Config { get; } = null!;
|
||||
internal Dictionary<string, Func<ITelegramBotClient, Message, CancellationToken, Task>?> CommandHandler { get; set; } = new();
|
||||
internal List<Func<ITelegramBotClient, Message, CancellationToken, Task>?> EditedMessageHandler { get; set; } = new();
|
||||
internal Dictionary<string, Func<ITelegramBotClient, CallbackQuery, CancellationToken, Task>?> CallbackQueryHandler { get; set; } = new();
|
||||
internal Dictionary<string, Func<ITelegramBotClient, InlineQuery, CancellationToken, Task>?> InlineHandler { get; set; } = new();
|
||||
internal Func<ITelegramBotClient, PreCheckoutQuery,CancellationToken, Task>? PreCheckoutHandler { get; set; }
|
||||
internal List<Func<ITelegramBotClient, Update, CancellationToken, Task>> DefaultUpdateHandler { get; set; } = new();
|
||||
internal static ILogger<TelegramHostedService> _logger;
|
||||
internal List<Func<ITelegramBotClient, Update, CancellationToken, Task>?> DefaultUpdateHandler { get; set; } = new();
|
||||
internal static ILogger<TelegramHostedService> Logger = null!;
|
||||
|
||||
public TelegramHostedService(ITelegramBotConfig config, IServiceCollection isc, ILogger<TelegramHostedService> logger)
|
||||
public TelegramHostedService(ITelegramBotConfig config, IServiceCollection serviceCollection, ILogger<TelegramHostedService> logger)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger = logger;
|
||||
Logger = logger;
|
||||
Client = new TelegramBotClient(config.Token);
|
||||
Config = config;
|
||||
this.isc = isc;
|
||||
this.ServiceCollection = serviceCollection;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception when creating TelegramHostedService: ");
|
||||
Logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception when creating TelegramHostedService: ");
|
||||
}
|
||||
}
|
||||
internal static bool IsValidHandlerMethod(MethodInfo method, Type parameterType)
|
||||
@@ -55,12 +55,12 @@ public class TelegramHostedService : IHostedService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Catched exception in parsing and checking params.");
|
||||
Logger.LogError(ex, "Catched exception in parsing and checking params.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
internal static Func<ITelegramBotClient, T, CancellationToken, Task> CreateDelegate<T>(MethodInfo method)
|
||||
internal static Func<ITelegramBotClient, T, CancellationToken, Task>? CreateDelegate<T>(MethodInfo method)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -70,7 +70,7 @@ public class TelegramHostedService : IHostedService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception in CreateDelegate function: ");
|
||||
Logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception in CreateDelegate function: ");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -106,10 +106,10 @@ public class TelegramHostedService : IHostedService
|
||||
|
||||
if (methods.Count == 0)
|
||||
{
|
||||
_logger.LogWarning("No methods found with required attributes");
|
||||
Logger.LogWarning("No methods found with required attributes");
|
||||
}
|
||||
|
||||
var isp = isc.BuildServiceProvider();
|
||||
var isp = ServiceCollection.BuildServiceProvider();
|
||||
foreach (var method in methods)
|
||||
{
|
||||
var declaringType = method.DeclaringType!;
|
||||
@@ -156,7 +156,7 @@ public class TelegramHostedService : IHostedService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Log(LogLevel.Critical, new EventId(), ex, "Catched new exception when added methods: ");
|
||||
Logger.Log(LogLevel.Critical, new EventId(), ex, "Catched new exception when added methods: ");
|
||||
}
|
||||
}, cancellationToken);
|
||||
}
|
||||
@@ -169,31 +169,46 @@ public class TelegramHostedService : IHostedService
|
||||
switch (update)
|
||||
{
|
||||
case { Message: { } message }:
|
||||
await CommandHandler.FirstOrDefault(k => message.Text!.StartsWith(k.Key))
|
||||
.Value(client, message, ctx);
|
||||
CommandHandler.Where(k => message.Text!.StartsWith(k.Key)).Select(async k =>
|
||||
{
|
||||
await k.Value!(client, message, ctx);
|
||||
return k;
|
||||
});
|
||||
break;
|
||||
case { EditedMessage: { } message }:
|
||||
EditedMessageHandler.ForEach(async k => await k(client, message, ctx));
|
||||
// ReSharper disable once AsyncVoidLambda
|
||||
EditedMessageHandler.ForEach(async k => await k!(client, message, ctx));
|
||||
break;
|
||||
case { CallbackQuery: { } callbackQuery }:
|
||||
await CallbackQueryHandler.FirstOrDefault(k => callbackQuery.Data!.StartsWith(k.Key))
|
||||
.Value(client, callbackQuery, ctx);
|
||||
CallbackQueryHandler.Where(k => callbackQuery.Data!.StartsWith(k.Key))
|
||||
.Select(async k =>
|
||||
{
|
||||
await k.Value!(client, callbackQuery, ctx);
|
||||
return k;
|
||||
});
|
||||
break;
|
||||
case { InlineQuery: { } inlineQuery }:
|
||||
await InlineHandler.FirstOrDefault(k => inlineQuery.Id.StartsWith(k.Key))
|
||||
.Value(client, inlineQuery, ctx);
|
||||
InlineHandler.Where(k => inlineQuery.Id.StartsWith(k.Key)).Select(async k =>
|
||||
{
|
||||
await k.Value!(client, inlineQuery, ctx);
|
||||
return k;
|
||||
});
|
||||
break;
|
||||
case { PreCheckoutQuery: { } preCheckoutQuery }:
|
||||
if (PreCheckoutHandler != null) await PreCheckoutHandler(client, preCheckoutQuery, ctx);
|
||||
break;
|
||||
default:
|
||||
DefaultUpdateHandler.ForEach(async k => await k(client, update, ctx));
|
||||
// ReSharper disable once AsyncVoidLambda
|
||||
DefaultUpdateHandler.ForEach(async k => await k!(client, update, ctx));
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Log(LogLevel.Error, new EventId(), ex, "Catched exception in UpdateHandler: ");
|
||||
if (ex is KeyNotFoundException)
|
||||
Logger.Log(LogLevel.Warning, new EventId(), ex, "Key not found: ");
|
||||
else
|
||||
Logger.Log(LogLevel.Error, new EventId(), ex, "Caught exception in UpdateHandler: ");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,7 +225,7 @@ public class TelegramHostedService : IHostedService
|
||||
UpdateHandler,
|
||||
Config.errorHandler ?? ((_, ex, _) =>
|
||||
{
|
||||
_logger.LogError(ex, "Catched error in telegram bot working: ");
|
||||
Logger.LogError(ex, "Catched error in telegram bot working: ");
|
||||
return Task.CompletedTask;
|
||||
}),
|
||||
Config.ReceiverOptions,
|
||||
@@ -218,7 +233,7 @@ public class TelegramHostedService : IHostedService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Log(LogLevel.Critical, new EventId(), ex, "Failed to start. Catched exception: ");
|
||||
Logger.Log(LogLevel.Critical, new EventId(), ex, "Failed to start. Catched exception: ");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,7 +245,7 @@ public class TelegramHostedService : IHostedService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogCritical(ex, "Failed to stop. Exception: ");
|
||||
Logger.LogCritical(ex, "Failed to stop. Exception: ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Version>1.0.2</Version>
|
||||
<Version>1.0.3</Version>
|
||||
<Authors>yawaflua</Authors>
|
||||
<Title>yawaflua.Telegram.Net</Title>
|
||||
<Description>Telegram.Bots extender pack, what provides to user attributes, which can used with services</Description>
|
||||
@@ -19,8 +19,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.3" />
|
||||
<PackageReference Include="Telegram.Bot" Version="22.4.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="(6.0.0,)" />
|
||||
<PackageReference Include="Telegram.Bot" Version="22.4.*" />
|
||||
<None Include="..\README.md" Pack="true" PackagePath="\"/>
|
||||
<None Include="..\LICENSE" Pack="true" PackagePath=""/>
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user