linklater-1.0.0.0: Write bots for your Slack account, and then go to sleep (because it's so easy and late at night)

Copyright(c) The Linklaterteers
LicenseBSD-style
Stabilityexperimental
PortabilityGHC
Safe HaskellNone
LanguageHaskell2010

Network.Linklater

Description

Features:

  • Uses Text everywhere so you can send your slash commands crazy Unicode characters all day long.
  • Lovely documentation.
  • Battle-tested.

Here's a /jpgto bot! If you run this program and then tell Slack about your server (incoming hook and custom slash command) and then type /jpgto diplomatico in one of your channels, you'll get the image from http://diplomatico.jpg.to. How, you say? Screen scraping.

urlParser :: Parser B.ByteString
urlParser = p
  where
    p = garbage *> url
    garbage = string "src="" | (P.take 1 *> garbage)
    url = takeTill (== _quotedbl)

urlFor :: Text -> IO Text
urlFor search = do
  r <- get (T.unpack $ F.format "http://{}.jpg.to/" [search])
  (return . handle . parse urlParser . strictly) (r ^. responseBody)
  where
    strictly = B.concat . L.toChunks
    handle (Fail i ctxs s) = error (show (i, ctxs, s))
    handle (Partial f) = handle (f "")
    handle (Done _ r) = toText r

jpgto :: Maybe Command -> Application
jpgto (Just (Command (User user) channel text)) req respond = do
  url <- urlFor (maybe "spolsky" id text)
  say (Message channel (response url) (EmojiIcon "gift")) config
  (respond . responseLBS status200 headers) ""
  where
    response url = F.format "@{} {}" (user, url)
    config = Config token "trello.slack.com"
    headers = [(Content-Type, "text/plain")]

main :: IO ()
main = do
  let port = 80
  putStrLn (F.format "+ Listening on port {}" [port])
  run port (slash jpgto)
   return ()

For the full example (since this one is missing a ton of imports), see the examples/ directory on GitHub.

https://github.com/hlian/linklater

Synopsis

Documentation

say :: Message -> Config -> IO (Response ByteString) Source

The say function posts a Message, with a capital M, to Slack. It'll, ahem, need your token first though.

slash :: (Maybe Command -> Application) -> Application Source

A bot server! As if by magic. This acts like a WAI middleware in that you let us wrap around your application.

data Channel Source

Where slash commands can come from, and where messages can go.

Constructors

GroupChannel Text

A public or private group.

IMChannel Text

A private conversation with your best friend (or lover?).

newtype User Source

A username: no at-signs, just text!

Constructors

User Text 

Instances

data Message Source

Here's how you talk.

Constructors

Message 

Fields

_messageChannel :: Channel

You need a channel. It can be a group channel or a private IM. Alert: if it's a private IM, it'll look like it came from slackbot (as of writing).

_messageText :: Text

What you want to say. This will be parsed in full (parse=full). In the future I'll support other forms of parsing, hopefully. Poke me with a pull request if I forget.

_messageIcon :: Icon

The icon you want your message to appear as.

data Config Source

Like a curiosity about the world, you'll need one of these to say something.

Constructors

Config 

Fields

_configIncomingHookToken :: Text

This is the incoming web hook token that Slack gave you. It's usually a long alphanumberic string of garbage.

_configHostname :: Text

This is where your Slack account is hosted. For example, 'trello.slack.com'.

data Command Source

Incoming HTTP requests to the slash function get parsed into one of these babies.

Constructors

Command 

Fields

_commandUser :: User

Who ran your slash command.

_commandChannel :: Channel

Where the person ran your slash command.

_commandText :: Maybe Text

Text for the slash command, if any.

newtype Icon Source

For example, ":stars2:". Future versions should support actual images, I GUESS.

Constructors

EmojiIcon Text 

Instances