Skip to content

Instantly share code, notes, and snippets.

@hlian
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hlian/61a19bf9e03f6b1223ee to your computer and use it in GitHub Desktop.
Save hlian/61a19bf9e03f6b1223ee to your computer and use it in GitHub Desktop.
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