Skip to content
This repository has been archived by the owner on Oct 29, 2018. It is now read-only.

WooCode/WooCode.Slack

Repository files navigation

Hi and welcome!

This library is developed by WooCode and we will add functions & make fixes now and then.

That said, we would love to see pull requests and add your ideas, handlers, fixes and code to this project since we just add things that we need for our own integrations/automations right now.

WooCode.Slack

Library for slack in .Net

  • WebHooks(both incoming and outgoing)
  • SlashCommand parsing
  • Bot (WooCode.Slack.WooBot + Nancy project)
  • Send ASP exceptions to slack (WooCode.Slack.MVC, experimental)

Prepare you config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- host is only needed if you use WooBot with the Nancy host -->
    <add key="Host" value="http://127.0.0.1:1337"/>
    <!-- https://COMPANYNAME.slack.com/services/new and add Incoming WebHooks thats where you'll find your URL -->
    <add key="Slack.HookUrl"  value="https://COMPANYNAME.slack.com/services/hooks/incoming-webhook?token=TOKEN"/>
    <add key="Slack.Icon" value="http://www.woocode.com/head.png"/>
    <add key="Slack.UserName" value="WooBot"/>
    <add key="Slack.Channel" value="#exceptions"/>
    <add key="Slack.Text" value="Something is wrong"/>
  </appSettings>
</configuration>

Send a message

// Create a new message with properties from app.config
var message = new Message("I think something is wrong.");
// Or supply some values
message = new Message("I think something is wrong.", channel:"exceptions", userName:"MyName");
// Add attachments or exceptions (will be added as attachments)
message.AttachException(new NotSupportedException("I FAILED YOU MASTER",e));
// Then you can fire it at the moon
message.Send();

The Bot

WooBot

More information about WooCode.Slack.WooBot will come later, just check the source ;)

If you use the WooCode.Slack.Nancy host you have to direct SlashCommands AND/OR Outgoing webhooks to your Host endpoint in app.config

  • WooCode.Slack.Nancy is the host project, just build it and start your server (check the app.config).
  • WooCode.Slack.WooBot contains the Bot code & handlers that are hosted in the Nancy project.

Bot commands

slashcommands will be answered by PM and webhook by message to the same channel

# Start a timer for the user that makes the request
/woobot timer start
# Stop the timer for the user and tells the user how long the timer was active
/woobot timer stop 
# Say hello
/woobot hello
# Echoes the <text>
/woobot echo <text> 

Add custom commands/handlers

Check the source until we have time to describe it in detail ;)

Adding custom handlers is really easy. Take the following EchoHandler for example.

using WooCode.Slack.WebHooks;

namespace WooCode.Slack.WooBot.Handlers
{
    public class EchoHandler : IHookHandler
    {
        public void Handle(IncomingMessage message)
        {
            new Message
            {
                Text = string.Format("@{0} {1}", message.UserName, message.Text),
                Channel = message.ChannelName
            }.Send();
        }
    }
}

if we write "/woobot echo some random text" in a channel the above handler will be invoked by convention handlerAliasHandler since the resolver ignores "Handler" in the end of the classname

message.Text will contain "some random text" and message.UserName will contain the username of the user that invoked the command.

So to make your own handler you just create a new class in the Handlers folder with a name that ends with Handler (or not) and implement the IHookHandler interface, write your logic in Handle and you are done.

About

Library for slacking with slack.com in .Net

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages