Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jcorrado/slackbot-router

A Clojure Slack bot request routing framework
https://github.com/jcorrado/slackbot-router

clojure library slack

Last synced: 26 days ago
JSON representation

A Clojure Slack bot request routing framework

Awesome Lists containing this project

README

        

# slackbot-router
[![Clojars Project](https://img.shields.io/clojars/v/jcorrado/slackbot-router.svg)](https://clojars.org/jcorrado/slackbot-router)

slackbot-router is a small routing library for Slack bots, organizing incoming message tests and reply-generators into tables. Incoming messages are handled asynchronously.

## Installation
Add the following dependency to your `project.clj` file:

[jcorrado/slackbot-router "0.1.1"]

## Usage

### Bot Setup
A Slack bot is a web application designed to handle events surfaced by a configured Slack workspace. Slack offers two patterns: the [Real Time Messaging API](https://api.slack.com/rtm) and the simpler [Events API](https://api.slack.com/events-api); I have only used the latter.

Here is a fragment of a Compojure app that shows the basic idea. Our message routing rules are in `message-table`, detailed below.

You will need to configure Ring, via `project.clj` to use your `init` and `handler` fns.

```clojure
(defproject mybot "0.1.0"
:dependencies [[org.clojure/clojure "1.9.0"]
[jcorrado/slackbot-router "0.1.1"]
;; ...]
:ring {:init mybot.handler/init
:handler mybot.handler/app})
```

A simple `mybot.handler` fragment with the basic idea.

```clojure
(def events-c (chan 100))

(defn slack-event-handler
[req]
(let [event (:body req)]
(try
(response (slackbot-router.core/route-event event events-c))
(catch java.lang.Exception e
(println "Unknown Slack message type:" (.getMessage e))
;; We always reply 200 to incoming Slack events
(response "OK")))))

(defroutes app
(POST "/slack-event" [body] slack-event-handler))

(defn init
[]
(go-loop []
(let [msg (