diff options
Diffstat (limited to 'src/Tomato/Post.hs')
-rw-r--r-- | src/Tomato/Post.hs | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/src/Tomato/Post.hs b/src/Tomato/Post.hs index c3bd847..4adcdbb 100644 --- a/src/Tomato/Post.hs +++ b/src/Tomato/Post.hs @@ -10,6 +10,9 @@ import Network.HTTP.Req ( jsonResponse , ignoreResponse , req + , Req + , defaultHttpConfig + , runReq , ReqBodyFile (..) , ReqBodyJson (..) , (/:) @@ -20,46 +23,47 @@ import Network.HTTP.Req , responseBody ) -import Data.Message (GMIUrl (..), OutMessage (..)) +import Tomato.Data.Except (DecodeException (..)) +import Tomato.Data.Message (GMIUrl (..), OutMessage (..)) +import Tomato.App (App (..)) import qualified Data.Aeson as Ae import qualified Data.Aeson.KeyMap as Ae +import qualified RIO.Text as T --- TODO global config reader monad -tomatoFile :: FilePath -tomatoFile = "tomato.png" - -accessToken :: ByteString -accessToken = "t2YhlxxwZmn2cWfkAomjMc6BgVPMaC5NRkqHzGQl" - -botId :: Text -botId = "713bcb4a4604006c944804552c" -- | Post the current tomato -postTomato :: Req () +postTomato :: RIO App () postTomato = do url <- uploadTomato postMessage $ OutMessage url -- | Upload the current tomato to the GroupMe image server -uploadTomato :: Req GMIUrl +uploadTomato :: RIO App GMIUrl uploadTomato = do + tomatoFile <- asks appFile + accessToken <- asks appToken let url = https "image.groupme.com" /: "pictures" - js <- req POST url (ReqBodyFile tomatoFile) jsonResponse $ + js <- rr $ req POST url (ReqBodyFile tomatoFile) jsonResponse $ header "X-Access-Token" accessToken <> header "Content-Type" "image/png" gmURL <- case Ae.fromJSON (responseBody js) of Ae.Success r -> pure r - Ae.Error _s -> error "deal with this later" -- TODO error monad + Ae.Error s -> throwM $ DecodeException $ T.pack s return gmURL -- | Post a message to the group with image as attachment -postMessage :: OutMessage -> Req () +postMessage :: OutMessage -> RIO App () postMessage outMes = do + botId <- asks appBotId + accessToken <- asks appToken let url = https "api.groupme.com" /: "v3" /: "bots" /: "post" outMes' = case Ae.toJSON outMes of Ae.Object o -> Ae.Object $ Ae.insert "bot_id" (Ae.toJSON botId) o _ -> error "exceptional" - req POST url (ReqBodyJson outMes') ignoreResponse $ + rr $ req POST url (ReqBodyJson outMes') ignoreResponse $ header "X-Access-Token" accessToken return () + +rr :: Req a -> RIO App a +rr = runReq defaultHttpConfig |