<commit_before># Simple Slack Bot

A simple library for creating [Slack bots](https://api.slack.com/bot-users) in C#.

This is being developed for my own use and may or not every be finished/fixed/maintained.

## Installation

TODO: NuGet...

## Usage

Create a [bot integration](https://my.slack.com/services/new/bot) on Slack to get an API Key to connect a bot.

Create a Command that can receive and send messages:

	class EchoCommand : Command
	{
		const string prefix = "echo ";

		public override async Task OnMessage(Channel channel, User user, string text)
		{
			if (!text.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
				return;

			await SendMessage(channel, text.Substring(prefix.Length));
		}
	}
	
Connect the bot and register your command:

	using (var bot = await Bot.Connect(token))
	{
		bot.RegisterCommand(new EchoCommand());
		bot.RegisterCommand(new CountdownCommand());

		Console.WriteLine("Press a key to disconnect...");
		Console.ReadKey();
	}
	
## Command Cancellation

It's possible to make your commands being cancelled. An optional `CancellationToken` can be used to signal cancellations when a user sends `abort`, `stop` or `cancel`:

	class CountdownCommand : Command
	{
		public override async Task OnMessage(Channel channel, User user, string text, CancellationToken cancellationToken)
		{
			if (!string.Equals(text, "countdown", StringComparison.OrdinalIgnoreCase))
				return;

			for (var i = 10; i > 0 && !cancellationToken.IsCancellationRequested; i--)
			{
				await SendMessage(channel, $"{i}...");
				await Task.Delay(1000);
			}

			if (!cancellationToken.IsCancellationRequested)
				await SendMessage(channel, "Thunderbirds are go!");
			else
				await SendMessage(channel, "Countdown was aborted. Thunderbirds are cancelled, kids :(");
		}
	}<commit_msg>Add csharp language to code samples.<commit_after># Simple Slack Bot

A simple library for creating [Slack bots](https://api.slack.com/bot-users) in C#.

This is being developed for my own use and may or not every be finished/fixed/maintained.

## Installation

TODO: NuGet...

## Usage

Create a [bot integration](https://my.slack.com/services/new/bot) on Slack to get an API Key to connect a bot.

Create a Command that can receive and send messages:

```csharp
class EchoCommand : Command
{
	const string prefix = "echo ";

	public override async Task OnMessage(Channel channel, User user, string text)
	{
		if (!text.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
			return;

		await SendMessage(channel, text.Substring(prefix.Length));
	}
}
```
	
Connect the bot and register your command:

```csharp
using (var bot = await Bot.Connect(token))
{
	bot.RegisterCommand(new EchoCommand());
	bot.RegisterCommand(new CountdownCommand());

	Console.WriteLine("Press a key to disconnect...");
	Console.ReadKey();
}
```
	
## Command Cancellation

It's possible to make your commands being cancelled. An optional `CancellationToken` can be used to signal cancellations when a user sends `abort`, `stop` or `cancel`:

```csharp
class CountdownCommand : Command
{
	public override async Task OnMessage(Channel channel, User user, string text, CancellationToken cancellationToken)
	{
		if (!string.Equals(text, "countdown", StringComparison.OrdinalIgnoreCase))
			return;

		for (var i = 10; i > 0 && !cancellationToken.IsCancellationRequested; i--)
		{
			await SendMessage(channel, $"{i}...");
			await Task.Delay(1000);
		}

		if (!cancellationToken.IsCancellationRequested)
			await SendMessage(channel, "Thunderbirds are go!");
		else
			await SendMessage(channel, "Countdown was aborted. Thunderbirds are cancelled, kids :(");
	}
}
```
{'fraction_non_alphanumeric': 0.11080711354309165, 'fraction_numerical': 0.0038303693570451436, 'mean_word_length': 4.4485842026825635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4565206', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1081, 'n_words': 366}