Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: chances of execution #16

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The usage is very simple yet powerful:
```csharp
...
using Navigator;
...
...

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -19,14 +19,13 @@ builder.Services.AddNavigator(options =>
{
options.SetWebHookBaseUrl(builder.Configuration["BASE_WEBHOOK_URL"]!);
options.SetTelegramToken(builder.Configuration["TELEGRAM_TOKEN"]!);
options.EnableTypingNotification();
});

var app = builder.Build();

var bot = app.GetBot();

// This action will be triggered if the user sends a message in the style of `/join <text>`.
// This action will be triggered if the user sends a message in the style of `/join <text> <text>`.
bot.OnCommand("join", async (INavigatorClient client, Chat chat, string[] parameters) =>
{
var result = string.Join(',', parameters);
Expand All @@ -39,6 +38,35 @@ app.MapNavigator();
app.Run();
```

# Getting Started

After installing the [package](https://www.nuget.org/packages/Navigator/) you can start using it by first configuring Navigator:

```csharp
builder.Services.AddNavigator(options =>
{
// Configuration here!
});
```

Inside the AddNavigator method you can define the following options:

- The WebHook base URL:`options.SetWebHookBaseUrl(string)`
- The Telegram token: `options.SetTelegramToken(string)`
- Optionally, the WebHook endpoint: `options.SetWebHookEndpoint(string)`
- Optionally, allow multiple actions for the same update: `options.EnableMultipleActionsUsage()`
- Optionally, enable the sending of chat actions (e.g. typing) notifications: `options.EnableChatActionNotification()`

Continue by defining the actions of your bot using the different helper methods (see source code for more details). Here is a short example:

Finally add the following line to map the endpoint to the navigator framework:

```csharp
app.MapNavigator();
```

This will start the server and will listen for incoming requests from Telegram. Your bot is good to go!

# Examples

Some examples can be found in the [samples](https://github.com/navigatorframework/navigator/src/) repository.
Expand Down
5 changes: 5 additions & 0 deletions src/Navigator/Actions/BotActionInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public record BotActionInformation
/// </summary>
public required UpdateCategory Category;

/// <summary>
/// The chance of the <see cref="BotAction" /> being executed. Optional.
/// </summary>
public required double? Chances;

/// <summary>
/// The <see cref="ChatAction" /> associtated with the <see cref="BotAction" />. Optional.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/Navigator/Actions/Builder/BotActionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public BotActionBuilder()
private ushort Priority { get; set; }
private TimeSpan? Cooldown { get; set; }
private ChatAction? ChatAction { get; set; }
private double? Chance { get; set; }

/// <summary>
/// Builds the bot action.
Expand All @@ -38,6 +39,7 @@ public BotAction Build()
{
ChatAction = ChatAction,
Category = Category,
Chances = Chance,
ConditionInputTypes = ConditionInputTypes,
HandlerInputTypes = HandlerInputTypes,
Name = Name ?? $"{_id}",
Expand Down Expand Up @@ -106,6 +108,17 @@ public BotActionBuilder SetCategory(UpdateCategory category)
return this;
}

/// <summary>
/// Sets the chance of the <see cref="BotAction" /> being executed.
/// </summary>
/// <param name="chance">The chance to be set.</param>
/// <returns>An instance of <see cref="BotActionBuilder" /> to be able to continue configuring the <see cref="BotAction" />.</returns>
public BotActionBuilder WithChances(double chance)
{
Chance = chance;
return this;
}

/// <summary>
/// Sets the priority of the <see cref="BotAction" />.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/Navigator/Strategy/NavigatorStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ private async IAsyncEnumerable<BotAction> FilterActionsThatCanHandleUpdate(IEnum
/// <param name="update">The <see cref="Update" /> object that triggered the execution of the <see cref="BotAction" />.</param>
private async Task ExecuteAction(BotAction action, Update update)
{
if (action.Information.Chances is not null && Random.Shared.NextDouble() < action.Information.Chances)
{
_logger.LogDebug("Discarding action {ActionName} because of configured chances ({Chances})", action.Information.Name,
action.Information.Chances);

return;
}

var numberOfInputs = action.Information.HandlerInputTypes.Length;
object?[] arguments = new object[numberOfInputs];

Expand Down
3 changes: 1 addition & 2 deletions src/Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Navigator.Configuration.Options;
using Telegram.Bot;
using Telegram.Bot.Types;
using Chat = Navigator.Entities.Chat;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -44,7 +43,7 @@
var text = $"message received: {message.MessageId}";

await client.SendTextMessageAsync(chat, text);
}).WithCooldown(TimeSpan.FromSeconds(30));
}).WithChances(0.5).WithCooldown(TimeSpan.FromSeconds(30));

app.MapNavigator();

Expand Down
Loading