Skip to content
Willian Falbo edited this page Jun 2, 2021 · 12 revisions

How can I get it?

First, install NuGet. Then, install SlackAPI from the package manager console:

# dotnet cli
$ dotnet add package SlackAPI

# alternatively, use a package manager
$ Install-Package SlackAPI

Configuring an app on Slack and retrieving an OAuth token for your application

  1. Goto https://api.slack.com/apps
  2. Click "Create New App"
  3. Give the app a name and assign it to a workspace
  4. Click "OAuth & Permissions" (In the left menu)
  5. Under "Scopes" click "Add an OAuth Scope" and select your desired permissions (for example chat:write:bot)
  6. At the top of the page, click "Install App to Workspace"
  7. Click Allow
  8. Copy the generated "OAuth Access Token". This is the token you need to pass into the Slack Client.

Posting your first message

public static async Task Main(string[] args)
{
  const string TOKEN = "YOUR TOKEN HERE";  // token from last step in section above
  var slackClient = new SlackTaskClient(TOKEN);

  var response = await slackClient.PostMessageAsync("#general", "hello world");
}

Retrieving access token for a specific user

To generate access tokens for the users, your first need to create and configure an app with a proper OAuth Redirect Url, redirect the user to the URL created by the SlackClientHelpers.GetAuthorizeUri method and requesting the access token by calling SlackClientHelpers.GetAccessToken when your redirect page is called.

You can check the following links for reference:

Creating a SlackSocketClient instance

First thing's first, you're going to need one of Slack's auth tokens. Once you do that, connecting is as easy as:

ManualResetEventSlim clientReady = new ManualResetEventSlim(false);
SlackSocketClient client = new SlackSocketClient(YOUR_AUTH_TOKEN);
client.Connect((connected) =>{
    // This is called once the client has emitted the RTM start command
    clientReady.Set();
}, () =>{
    // This is called once the RTM client has connected to the end point
});
client.OnMessageReceived += (message) =>
{
    // Handle each message as you receive them
};
clientReady.Wait();

Sending a message to a channel

You need a connected client first, cfr. above.

client.GetChannelList((clr) => { Console.WriteLine("got channels");  });
var c = client.Channels.Find(x => x.name.Equals("general"));
client.PostMessage((mr) => Console.WriteLine("sent message to general!"), c.id, "Hello general world");

Sending a message to a user (direct message)

Same here, client should be connected first.

client.GetUserList((ulr) => { Console.WriteLine("got users"); });
var user = client.Users.Find(x => x.name.Equals("slackbot")); // you can replace slackbot with everyone else here
var dmchannel = client.DirectMessages.Find(x => x.user.Equals(user.id));
client.PostMessage((mr) => Console.WriteLine("sent! to " + dmchannel.id), dmchannel.id, "I don't know you yet!");

Async

An async version of the client is available from SlackAPI.SlackTaskClient