Skip to content

.NET Core package that helps with Slack interactions

License

Notifications You must be signed in to change notification settings

oleksandrmalomed/Slack.NetStandard

 
 

Repository files navigation

Slack.NetStandard

.NET Core NuGet package that helps with Slack interactions Available at https://www.nuget.org/packages/Slack.NetStandard

Create OAuth URL

using Slack.NetStandard.Auth;

var builder = new OAuthV2Builder("clientId")
{
    State = "stateGoesHere", 
    BotScope = "channels:read"
};
var redirectUri = builder.BuildUri();

Get OAuth Access Token from Code

using Slack.NetStandard.Auth;

var token = await OAuthV2Builder.Exchange(code,clientId,clientSecret);

Verify Incoming Request is from Slack

using Slack.NetStandard;

var verifier = new RequestVerifier(signingSecret);
var verified = verifier.Verify(request.Headers[RequestVerifier.SignatureHeaderName], long.Parse(request.Headers[RequestVerifier.TimestampHeaderName]), request.Body);

Receive/Respond to a slash command payload

var command = new SlashCommand(payloadText);

var message = new InteractionMessage();
message.Blocks.Add(new Section{Text = new PlainText("Only title is required")});
message.Blocks.Add(new Divider());
message.Send(command.ResponseUrl);

await command.Respond(message);

// or - if it's not from a slash command, any response url can use
await command.Response(responseUrl);

Building & sending a modal

var view = new View
{
    Type = "modal",
    Title = "Create New Story",
    Close = "Cancel",
    Submit = "Submit",
    Blocks = new IMessageBlock[]
    {
       new Section{Text = new PlainText("Only title is required")}
    }
};

var client = new SlackWebApiClient(accessToken);
var response = await client.View.Open(triggerId,view);

Sending a new message to a channel

var request = new PostMessageRequest {Channel = "C123456"};
request.Blocks.Add(new Section{Text = new PlainText("Hi There!")});

var client = new SlackWebApiClient("token");
await client.Chat.Post(request);

Parse Events API Body

using Slack.NetStandard.EventsApi;
using Slack.NetStandard.EventsApi.CallbackEvents;

var eventObject = JsonConvert.DeserializeObject<Event>(input.Body);

if (eventObject is EventCallback callback)
{
    switch(callback.Event)
    {
        case AppHomeOpened appHome:
            break;
        case GroupClose groupClose:
            break;

    }
}

Parse incoming text for entities (channels, users, links etc.)

var entities = TextParser.FindEntities("<@W123456|Steven>");
if(entities.First() is UserMention mention)
{
    var userId = mention.UserId //W123456
    var label = mention.Label //Steven
}

Socket Mode - getting to your payload

if(msg.Contains("envelope_id")) //If there's no envelope ID it's a Hello or Disconnect object
{
   var env = JsonConvert.DeserializeObject<Envelope>(msg);
   switch(env.Payload) {
     case SlashCommand command:
       //logic here
       break;
     case EventCallback evt: 
       //logic here
       break;
     case InteractionPayload payload:
       //logic here
       break;
   }
   var ack = new Acknowledge{EnvelopeId=env.EnvelopeId} //All messages must be acknowledged within a few seconds
   Send(ack);
}

For a .NET 3.1 client that helps with a lot of the Socket Mode plumbing, the SocketSample app is now available at Slack.NetStandard.AsyncEnumerable

About

.NET Core package that helps with Slack interactions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%