2021-08-21 09:45:37 +02:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Web.Announcements
|
2021-08-21 09:49:30 +02:00
|
|
|
(sendAnnouncement,
|
2021-08-21 10:08:41 +02:00
|
|
|
formatLink,
|
|
|
|
AnnouncementHook(..),
|
|
|
|
toAnnouncementHook)
|
2021-08-21 09:45:37 +02:00
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Text
|
|
|
|
import qualified Data.Text.Encoding as DTE
|
|
|
|
import Data.Maybe
|
|
|
|
import Network.HTTP.Req as R
|
|
|
|
import Prelude
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Default
|
|
|
|
|
2021-08-21 10:08:41 +02:00
|
|
|
data AnnouncementHook = SlackWebHook Text
|
2021-08-21 09:45:37 +02:00
|
|
|
|
2021-08-21 10:08:41 +02:00
|
|
|
toAnnouncementHook :: Text -> AnnouncementHook
|
|
|
|
toAnnouncementHook url
|
|
|
|
| ".slack." `isInfixOf` url = SlackWebHook url
|
|
|
|
| otherwise = error $ "unknown hook type"
|
|
|
|
|
|
|
|
sendAnnouncement :: AnnouncementHook -> Text -> IO ()
|
|
|
|
sendAnnouncement (SlackWebHook hook) message = do
|
2021-08-21 09:45:37 +02:00
|
|
|
let (Just (hookUrl, _)) = parseUrlHttps $ DTE.encodeUtf8 hook
|
|
|
|
|
|
|
|
R.runReq def $ do
|
|
|
|
let payload = object [ "text" .= message ]
|
|
|
|
(_ :: IgnoreResponse) <- R.req R.POST
|
|
|
|
hookUrl
|
|
|
|
(R.ReqBodyJson payload)
|
|
|
|
R.ignoreResponse
|
|
|
|
mempty
|
|
|
|
return ()
|
|
|
|
|
2021-08-21 10:08:41 +02:00
|
|
|
formatLink :: Maybe AnnouncementHook -> Text -> Text -> Text
|
|
|
|
formatLink (Just (SlackWebHook _)) url title = "<" <> url <> "|" <> title <> ">"
|
|
|
|
formatLink Nothing url title = title <> "<" <> url <> ">"
|