Skip to content

Commit

Permalink
Merge pull request #55 from MajMcCloud/development
Browse files Browse the repository at this point in the history
Integrating development branch into master
  • Loading branch information
MajMcCloud authored Dec 26, 2023
2 parents 121a788 + c1018ac commit d56b26a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 24 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ BitTorrent: `TYVZSykaVT1nKZnz9hjDgBRNB9VavU1bpW`
* [TaggedButtonGrid](#tagged-button-grid)
* [CheckedButtonList](#checked-button-list)
* [MultiToggleButton](#multi-toggle-button)
- [Localizations](#localizations)
- [Groups](#groups)
* [SplitterForm](#splitter-form)
* [GroupForm](#group-form)
Expand Down Expand Up @@ -714,6 +715,19 @@ Check the example project [TelegramBotBase.Test/Tests/Controls/CheckedButtonList

Check the example project [TelegramBotBase.Test/Tests/Controls/MultiToggleButtonForm.cs](TelegramBotBase.Test/Tests/Controls/MultiToggleButtonForm.cs)


## Localizations

The current available languages for controls are:

- English
- German
- Persian

You can add other languages easily by creating a subclass of the [TelegramBotBase/Localizations/Localization.cs](TelegramBotBase/Localizations/Localization.cs) class.

To set the default language set the *Language* property on the static [TelegramBotBase/Localizations/Default.cs](TelegramBotBase/Localizations/Default.cs) instance.

## Groups

For groups, there are multiple different tools which help to work with and allows bot also to manage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="TelegramBotBase" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions TelegramBotBase/Base/ErrorResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace TelegramBotBase.Base
{
public class ErrorResult : EventArgs
{
public ErrorResult(Exception exception)
{
Exception = exception;
}

public Exception Exception { get; }
}
}
61 changes: 48 additions & 13 deletions TelegramBotBase/Base/MessageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace TelegramBotBase.Base;
public class MessageClient
{
private static readonly object EvOnMessageLoop = new();
private static readonly object EvOnReceiveError = new();

private static object __evOnMessage = new();

Expand All @@ -27,6 +28,14 @@ public class MessageClient

private CancellationTokenSource _cancellationTokenSource;

/// <summary>
/// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before
// start polling. If set to true Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates
// should be set to not null, otherwise Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates
// will effectively be set to receive all Telegram.Bot.Types.Updates.
/// </summary>
public bool ThrowPendingUpdates { get; set; }


public MessageClient(string apiKey)
{
Expand Down Expand Up @@ -113,6 +122,8 @@ public void StartReceiving()

var receiverOptions = new ReceiverOptions();

receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;

TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions,
_cancellationTokenSource.Token);
}
Expand All @@ -128,22 +139,12 @@ public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update,
await OnMessageLoop(new UpdateResult(update, null));
}

public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
public async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
CancellationToken cancellationToken)
{
if (exception is ApiRequestException exApi)
{
Console.WriteLine($"Telegram API Error:\n[{exApi.ErrorCode}]\n{exApi.Message}");
}
else
{
Console.WriteLine(exception.ToString());
}

return Task.CompletedTask;
await OnReceiveError(new ErrorResult(exception));
}


/// <summary>
/// This will return the current list of bot commands.
/// </summary>
Expand Down Expand Up @@ -186,7 +187,41 @@ public event Async.AsyncEventHandler<UpdateResult> MessageLoop

public async Task OnMessageLoop(UpdateResult update)
{
await (Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
var eventHandlers = (Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);

if (eventHandlers != null)
{
await eventHandlers;
}
}


public event Async.AsyncEventHandler<ErrorResult> ReceiveError
{
add => Events.AddHandler(EvOnReceiveError, value);
remove => Events.RemoveHandler(EvOnReceiveError, value);
}

public async Task OnReceiveError(ErrorResult update)
{
var eventHandlers = (Events[EvOnReceiveError] as Async.AsyncEventHandler<ErrorResult>)?.Invoke(this, update);

if (eventHandlers != null)
{
await eventHandlers;
return;
}

//Fallback when no event handler is used.
if (update.Exception is ApiRequestException exApi)
{
Console.WriteLine($"Telegram API Error:\n[{exApi.ErrorCode}]\n{exApi.Message}");
}
else
{
Console.WriteLine(update.Exception.ToString());
}

}

#endregion
Expand Down
15 changes: 10 additions & 5 deletions TelegramBotBase/Builder/BotBaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory)

#region "Step 4 (Network Settings)"

public IBotCommandsStage WithProxy(string proxyAddress)
public IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false)
{
var url = new Uri(proxyAddress);
_client = new MessageClient(_apiKey, url)
Expand All @@ -217,11 +217,12 @@ public IBotCommandsStage WithProxy(string proxyAddress)
Timeout = new TimeSpan(0, 1, 0)
},
};
_client.ThrowPendingUpdates = throwPendingUpdates;
return this;
}


public IBotCommandsStage NoProxy()
public IBotCommandsStage NoProxy(bool throwPendingUpdates = false)
{
_client = new MessageClient(_apiKey)
{
Expand All @@ -230,11 +231,12 @@ public IBotCommandsStage NoProxy()
Timeout = new TimeSpan(0, 1, 0)
}
};
_client.ThrowPendingUpdates = throwPendingUpdates;
return this;
}


public IBotCommandsStage WithBotClient(TelegramBotClient tgclient)
public IBotCommandsStage WithBotClient(TelegramBotClient tgclient, bool throwPendingUpdates = false)
{
_client = new MessageClient(_apiKey, tgclient)
{
Expand All @@ -243,11 +245,12 @@ public IBotCommandsStage WithBotClient(TelegramBotClient tgclient)
Timeout = new TimeSpan(0, 1, 0)
}
};
_client.ThrowPendingUpdates = throwPendingUpdates;
return this;
}


public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort)
public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort, bool throwPendingUpdates = false)
{
_client = new MessageClient(_apiKey, proxyHost, proxyPort)
{
Expand All @@ -256,10 +259,11 @@ public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort)
Timeout = new TimeSpan(0, 1, 0)
}
};
_client.ThrowPendingUpdates = throwPendingUpdates;
return this;
}

public IBotCommandsStage WithHttpClient(HttpClient tgclient)
public IBotCommandsStage WithHttpClient(HttpClient tgclient, bool throwPendingUpdates = false)
{
_client = new MessageClient(_apiKey, tgclient)
{
Expand All @@ -268,6 +272,7 @@ public IBotCommandsStage WithHttpClient(HttpClient tgclient)
Timeout = new TimeSpan(0, 1, 0)
}
};
_client.ThrowPendingUpdates = throwPendingUpdates;
return this;
}

Expand Down
15 changes: 10 additions & 5 deletions TelegramBotBase/Builder/Interfaces/INetworkingSelectionStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,41 @@ public interface INetworkingSelectionStage
/// Chooses a proxy as network configuration.
/// </summary>
/// <param name="proxyAddress"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns>
IBotCommandsStage WithProxy(string proxyAddress);
IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false);

/// <summary>
/// Do not choose a proxy as network configuration.
/// </summary>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns>
IBotCommandsStage NoProxy();
IBotCommandsStage NoProxy(bool throwPendingUpdates = false);


/// <summary>
/// Chooses a custom instance of TelegramBotClient.
/// </summary>
/// <param name="client"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns>
IBotCommandsStage WithBotClient(TelegramBotClient client);
IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false);


/// <summary>
/// Sets the custom proxy host and port.
/// </summary>
/// <param name="proxyHost"></param>
/// <param name="Port"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns>
IBotCommandsStage WithHostAndPort(string proxyHost, int Port);
IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false);

/// <summary>
/// Uses a custom http client.
/// </summary>
/// <param name="client"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns>
IBotCommandsStage WithHttpClient(HttpClient client);
IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false);
}

0 comments on commit d56b26a

Please sign in to comment.