Building an App as an Agent for Slack with .NET
This is a small post, mostly a memo for myself and those who want to build something like this for themselves.
Recently, I wanted to build a small plugin for my old app, Zet Universe, capable of monitoring my Slack Channels and subscribing to messages in them.
Turns out, there are lots of issues in the process. Basically, you have to use two kinds of APIs: one for accessing data (polling from server and posting from your app), and the other one for subscribing to changes). However, over the years Slack got multiple APIs available/deprecated, so it’s a bit of a problem to figure out how to get things done. And, as ever, while many languages are supported by official Slack dev platform .NET isn’t one of them. So finding the right tools was essential.
In this short blog post you’ll learn how to make your own small app using modern Slack APIs that can be an agent working on behalf of you.
Learning Slack APIs
For polling you can use Slack Web APIs:
These can be accessed in .NET, for example, with SlackAPI library:
For subscribing to changes you can use Slack Events APIs:
If you want to build an app that works on the client (e.g., a PC or a phone), you’ll find Socket Mode especially useful.
Configuring Access for your app to Slack APIs
To use these APIs you need to:
I. Create your own Slack app

You’ll also have to pick a workspace for it.
II. Configure its Permissions (for Web APIs access)

Scroll down to identify User Token scopes for your app to act on behalf of your user:

Beware, some scopes will destroy your app’s ability to work!
III. Subscribe to Events (for Events APIs access)

IV. Pick all the scopes you need for your agent to act on behalf of users

V. Enable Socket Mode for subscribing to Events

Consuming Slack APIs from .NET App
Polling
For polling, you can work with the aforementioned SlackAPI library for .NET. First, obtain your user token (for the end-user this would have to work through OAuth2 method, but for testing you can get yours from your App settings). Use it as a param for initializing SlackTaskClient like shown below:
// initializing REST Client
var slackClient = new SlackTaskClient(_slackUserToken);
var response = await slackClient.TestAuthAsync();
if (response.ok)
{
// showing channels
RefreshAvailableChannels();
}
Where method RefreshAvailableChannels() can work with a ListBox or a ListView named slackChannels in WPF/UWP like this:
private async Task RefreshAvailableChannels()
{
try
{
var conversationsResponse = await this._taskClient.GetConversationsListAsync(string.Empty, true, 1000);
if (conversationsResponse.ok)
{
var convos = conversationsResponse.channels.Where(c => c.is_member == true).ToList();
this.slackChannels.ItemsSource = convos;
}
}
catch
{
}
}
However, with the SlackAPI there is a number of new and deprecated APIs. To figure out which are which you can take a look at the example in this PR:
Subscribing to Events
To subscribe to Events, you’ll need a library with a socket mode support. SlackNet is a good choice:
You can use this demo to work with the Socket Mode from your C# app:
While looking back I’d say the approach described is rather simple if not obvious finding the docs for doing these steps (as well as finding the right libraries for .NET) wasn’t that simple.
Enjoy!