linklater: The fast and fun way to write Slack.com bots

[ bsd3, library, network ] [ Propose Tags ]

A library for writing https://slack.com/ Slack chat bots.

Tutorial: https://github.com/hlian/linklater/wiki/Tutorial

Features you could take advantage of right now, should you choose:

For example, maybe you want this little guy to show up in your channel:

Find out how by clicking on Network.Linklater!


[Skip to Readme]
Versions [faq] 1.0.0.0, 1.0.0.1, 1.0.0.2, 1.0.0.3, 2.0.0.0, 2.0.0.1, 2.0.0.2, 2.0.0.3, 3.1.0.0, 3.2.0.0, 4.0.0.0, 4.0.0.1, 4.0.0.2
Change log changelog
Dependencies aeson (>=0.7.0.6 && <0.8), base (>=4.6 && <4.8), bytestring (>=0.10.4.0 && <0.11), containers (==0.5.*), http-types (>=0.8.5 && <0.9), lens (==4.3.*), text (>=1.1.1.3 && <1.2), wai (>=3.0.0.2 && <3.1), wreq (>=0.1.0.1 && <0.2) [details]
License BSD-3-Clause
Author Hao Lian <me@haolian.org>
Maintainer me@haolian.org
Category Network
Home page https://github.com/hlian/linklater
Bug tracker https://github.com/hlian/linklater/issues
Source repo head: git clone https://github.com/hlian/linklater
Uploaded by hao at Sun Jul 27 08:05:46 UTC 2014
Distributions NixOS:4.0.0.2
Downloads 5990 total (123 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Successful builds reported [all 1 reports]

Modules

[Index]

Flags

NameDescriptionDefaultType
developerDisabledManual

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for linklater-2.0.0.1

[back to package description]

Who let you in here?

Relax! I'm here to make your life easier. Has your company ever switched to using Slack, and then you wanted to write silly Slack bots in Haskell as a way to learn Haskell?

<sup>Really?<sup>Wow<sup>That was a pretty specific question.</sup></sup>

Uh, do you want to be friends? Well let's talk about it later, because right now I have an example for you.

But you'll have to grab me first:

  • cabal sandbox init
  • cabal install linklater

If you don't have Haskell, it's quite easy: Windows, Mac, and Linux.

Show me an example!

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.

-- Remaining imports left as an exercise to the reader.
import Network.Linklater (say, slashSimple, Command(..), Config(..), Message(..), Icon(..), Format(..))

findUrl :: Text -> Maybe Text
findUrl = fmap fromStrict . maybeResult . parse (manyTill (notChar '\n') (string "src=\"") *> takeTill (== '"'))

jpgto :: Maybe Command -> IO Text
jpgto (Just (Command user channel (Just text))) = do
  message <- (fmap messageOf . findUrl . decodeUtf8 . flip (^.) responseBody) <$> get ("http://" <> (unpack subdomain) <> ".jpg.to/")
  case (debug, message) of
    (True, _) -> putStrLn ("+ Pretending to post " <> (unpack . decodeUtf8 . encode) message) >> return ""
    (False, Just m) -> config' >>= say m >> return ""
    (False, Nothing) -> return "Something went wrong!"
  where config' = (Config "trello.slack.com" . filter (/= '\n') . pack) <$> readFile "token"
        subdomain = (intercalate "." . fmap (filter isLetter . filter isAscii) . words) text
        messageOf url = FormattedMessage (EmojiIcon "gift") "jpgtobot" channel [FormatAt user, FormatLink url (subdomain <> ".jpg.to>"), FormatString "no way!: &<>"]
        debug = True
jpgto _ = return "Type more! (Did you know? jpgtobot is only 26 lines of Haskell. <https://github.com/hlian/jpgtobot/blob/master/Main.hs>)"

main :: IO ()
main = let port = 3000 in putStrLn ("+ Listening on port " <> show port) >> run port (slashSimple jpgto)

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

Now! /jpgto corgi:

jpgtobot in action

So easy. Much fast.

Features

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

Contributors

  • Hao Lian, author
  • Ian Henry, design review and future contributor???
  • Shields (the Grizzly Bear album), which I listened all the way through for the first time while I was writing this ★★★★