Skip to content
Pat Hermens edited this page Jun 16, 2017 · 12 revisions

Pragmatically getting your auth token from your email and password credentials

SlackClient.StartAuth((authStartResponse) =>{
    // Here authStartResponse has a field ('users') containing a list of teams you have access to.
    SlackClient.AuthSignin(
        (authSigninResponse) =>{
            //Here, authSigninResponse contains a field 'token' which is your valid authentication token.
        }, 
        authStartResponse.users[0].user_id, 
        authStartResponse.users[1].team_id, 
        YOUR_PASSWORD_HERE
    );
}, YOUR_EMAIL_HERE);

Alternately, see https://github.com/Inumedia/SlackAPI/tree/master/SlackAPI.Console for an example project using OAuth.

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.SendMessage((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.SendMessage((mr) => Console.WriteLine("sent! to " + dmchannel.id), dmchannel.id, "I don't know you yet!");
Clone this wiki locally