GithubHelp home page GithubHelp logo

dhowett / slack.netstandard Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stoiveyp/slack.netstandard

1.0 2.0 0.0 649 KB

.NET Core package that helps with Slack interactions

License: MIT License

C# 100.00%

slack.netstandard's Introduction

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

slack.netstandard's People

Contributors

stoiveyp avatar cssho avatar dhowett avatar rasmuskl avatar absshek avatar kzhen avatar

Stargazers

Media Explorer avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.