Skip to content

Latest commit

 

History

History
131 lines (112 loc) · 3.75 KB

readme.md

File metadata and controls

131 lines (112 loc) · 3.75 KB

Slacker2

Create your own Slackbot w/ C#.

Quick Guide

Initialization

SlackBot.Configuration = new SlackBotConfiguration()
{
    AuthToken = "xoxb-AAAA-BBBBBBBB"
};
SlackBot.Run();

// `SlackBot.Run` does not block the program.
// You have to hold your bot yourself.
Console.ReadLine();

You can find your AuthToken here.

Respond to messages
Creating a new bot command is very simple. Even you don't need to register your commands manually.
Just define a method and let Slacker2 know the method is a subscriber. Slacker2 will find all your subscribers automatically.

The example below shows that how to make a basic bot command.

public class Program : BotService {

    // put a regular expr which you want to subscribe
    [Subscribe("^hello")]
    public void OnHello(SlackMessage message) {
        message.Reply("hi there");
    }
}

Accept command parameters
Since Subscribe attribute accepts a regular expressions, you can also use capturing function.
Captured values will be bound to method's parameters in order.

[Subscribe("^echo (.+)$")]
public void OnEcho(SlackMessage message, string echo) {
    message.Reply(echo);
}

[Subscribe("^sum (.+) (.+)$")]
public void OnSum(SlackMessage message, int a, int b) {
    message.Reply($"{a} + {b} = {a + b}");
}

Periodic tasks
If you need to execute a method in every specific minutes, just add a Schedule attribute on your method.
Slacker2 will invoke the method repeatedly by internally managed timers.

[Schedule(1)] // interval (seconds)
public void OnTimer()
{
    Console.WriteLine("Minsoo chunjaeya");
}

Interactive Message

Currently, Slacker2 only supports `buton` type messages.

Requirements
Slack requires a https server for interactive messages. we detour this using AWS.SQS and AWS.Labmda.

TODO

If you can manage a https server yourself, you may want to use it without AWS stuffs.
However, We don't have flexibilities for it. You have to implement by yourself.
Please edit these lines of code

Send a message that includes some buttons

var choice = await SendActionMessageAndWait(
    "channel", "choose one of the following items",
    new SlackInteractiveMessage() {
        Buttons = new SlackActionButton[] {
            new SlackActionButton() {
                Name = "orange", Text = "Give me an Orange!"
            },
            new SlackActionButton() {
                Name = "banana", Text = "Give me a Banana!"
            }
        }
    });
    
if (choice == "orange") { /* ... */ }
else if (choice == "banana") { /* .... */ }

Advanced Features

User permission
Sometimes, you may want to make dangerous functions. Such as changing machine's state or handling your private data.
Slacker2 also provides a permission management feature. You can add the required permissions on your commands and grant/remove each permissions to the right users.

[Subscribe("^!sys_shutdown$")]
[NeedsPermission("SYSTEM_SHUTDOWN")]
public void OnShutdown(SlackMessage message) {
    /* shutdown machine */
}
GrantPermission(user, "SYSTEM_SHUTDOWN");
// now `user` can invoke `!sys_shutdown` method.

User permissions can be evaluated at method's body instead NeedsPermission attribute.

var user = message.Sender;

if (user.Permissions.Contains("SYSTEM_SHUTDOWN"))
    ; /* shutdown machine */
else
    message.Reply("Sorry, but you don't have the proper permission.");    

Integrations w/ Slacker2