Coder Social home page Coder Social logo

slackbot.net's Introduction

Slackbot.NET

Build

What?

An opinionated ASP.NET Core middleware to create simple Slackbots using the Slack event API.

Install

Download it from NuGet:NuGet

$ dotnet add package slackbot.net.endpoints

Supported events

  • challenge
  • app_mention
  • view_submission
  • member_joined_channel

Configuration

A slack app can be distributed either as a single-workspace application (1 single Slack token), or as a distributed Slack application where other workspaces can install it them self either via a web page, or via the Slack App Store.

Single workspace Slack app

var builder = WebApplication.CreateBuilder(args);

// Needed to verify that incoming event payloads are from Slack
builder.Services.AddAuthentication()
                .AddSlackbotEvents(c =>
                    c.SigningSecret = Environment.GetEnvironmentVariable("SIGNING_SECRET")
                );

// Setup event handlers
builder.Services.AddSlackBotEvents()
                .AddAppMentionHandler<DoStuff>()


var app = builder.Build();
app.UseSlackbot(); // event endpoint
app.Run();

class DoStuff : IHandleAppMentions
{
    public bool ShouldHandle(AppMentionEvent slackEvent) => slackEvent.Text.Contains("hi");

    public Task<EventHandledResponse> Handle(EventMetaData meta, AppMentionEvent @evt)
    {
        Console.WriteLine("Doing stuff!");
        return Task.FromResult(new EventHandledResponse("yolo"));
    }
}

Advanced: Distributed Slack app

Implement the ITokenStore to store/remote on install/uninstall flows.

var builder = WebApplication.CreateBuilder(args);

// Needed to verify that incoming event payloads are from Slack
builder.Services.AddAuthentication()
               .AddSlackbotEvents(c => c.
                   SigningSecret = Environment.GetEnvironmentVariable("SIGNING_SECRET")
               );

builder.Services.AddSlackbotDistribution(c => {
   c.CLIENT_ID = Environment.GetEnvironmentVariable("CLIENT_ID");
   c.CLIENT_SECRET = Environment.GetEnvironmentVariable("CLIENT_SECRET");
});

// Setup event handlers
builder.Services.AddSlackBotEvents<MyTokenStore>()
               .AddAppMentionHandler<DoStuff>()


var app = builder.Build();
app.Map("/authorize", a => a.UseSlackbotDistribution()); // OAuth callback endpoint
app.Map("/events", a => a.UseSlackbot()); // event endpoint
app.Run();


/// Bring-your-own-token-store:
class MyTokenStore : ITokenStore
{
   // _db is some persistance technology you choose (sql, mongo, whatever)
   public Task<Workspace Delete(string teamId) => _db.DeleteByTeamId(teamId);
   public Task Insert(Workspace slackTeam) => _db.Insert(slackTeam);
}

Samples

Check the samples.

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.