-
Notifications
You must be signed in to change notification settings - Fork 243
Examples
Willian Falbo edited this page Jun 2, 2021
·
12 revisions
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
- Goto https://api.slack.com/apps
- Click "Create New App"
- Give the app a name and assign it to a workspace
- Click "OAuth & Permissions" (In the left menu)
- Under "Scopes" click "Add an OAuth Scope" and select your desired permissions (for example chat:write:bot)
- At the top of the page, click "Install App to Workspace"
- Click Allow
- Copy the generated "OAuth Access Token". This is the token you need to pass into the Slack Client.
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");
}
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:
- https://github.com/Inumedia/SlackAPI/blob/master/SlackAPI.Tests/UserUIInteraction.cs (the redirect Url is configured to http://localhost)
- https://github.com/gpailler/Luxa4Slack/blob/master/Luxa4Slack/OAuthHelper.cs https://github.com/gpailler/Luxa4Slack/blob/master/Luxa4Slack.OAuth.AzureFunctions/RedirectFunction.cs
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();
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");
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!");
An async version of the client is available from SlackAPI.SlackTaskClient