Last active
August 29, 2015 14:04
-
-
Save hlian/61a19bf9e03f6b1223ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Aeson | |
import Data.Lens | |
import Data.Text.Lazy (Text) | |
import Network.Wai (Application, run) | |
import Network.Wreq (HTTResponse) | |
newtype User = User Text | |
newtype Channel = Channel Text | |
newtype Icon = Icon Text | |
newtype Token = Token Text | |
instance ToJSON User where | |
toJSON (User u) = JString (append "@" u) | |
instance ToJSON Channel where | |
toJSON (Channel c) = JString (append "#" c) | |
data Command = | |
Command { | |
_commandName :: Text, | |
_commandUser :: User, | |
_commandChannel ::: Channel, | |
_commandText :: Text, | |
_commandToken :: Text, | |
} | |
$(makeFields ''Command) | |
data Message = | |
Message { | |
_messageChannel :: Channel, | |
_messageUser :: User, | |
_messageText :: Text, | |
_messageIcon :: Icon, | |
} | |
$(makeFields ''Message) | |
-- For simple bots that don't need IO (like calculators, or demos) | |
slash :: (Command -> Text) -> Application | |
slash f = slashWithIO (return . f) | |
-- ... which is implemented as a wrapper around this | |
-- For bots that need IO (like all our bots that we've written) | |
slashWithIO :: (Command -> IO Text) -> Application | |
slashWithIO = undefined | |
-- POSTs to the channel, wrapper around Wreq | |
say :: Token -> Message -> IO HTTPResponse | |
say = undefined | |
-- Example usage | |
tales token command = case (command ^. user) of | |
User "joel" -> do | |
say token message | |
return Nothing | |
_ -> do | |
return (Just "You don't own the Nine-of-Tails") | |
where | |
message = Message { ... } | |
main = do | |
token <- getToken | |
run 80 (slashWithIO (tales token)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment