| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Line.Messaging.Webhook
Description
This module provides webhook handlers both general and WAI-specific.
- module Line.Messaging.Webhook.Types
- webhook :: Monad m => ChannelSecret -> ByteString -> Signature -> ExceptT WebhookFailure m [Event]
- webhookApp :: ChannelSecret -> ([Event] -> IO ()) -> (WebhookFailure -> Application) -> Application
- defaultOnFailure :: WebhookFailure -> Application
- webhookAction :: ChannelSecret -> ([Event] -> IO ()) -> (WebhookFailure -> ActionM ()) -> ActionM ()
- defaultOnFailure' :: WebhookFailure -> ActionM ()
Types
Re-exported for convenience.
module Line.Messaging.Webhook.Types
Basic webhook
Arguments
| :: Monad m | |
| => ChannelSecret | |
| -> ByteString | Request body |
| -> Signature | Auth signature in |
| -> ExceptT WebhookFailure m [Event] |
A basic webhook function. It validates a request with a channel secret, signature and body, and parses the body into a list of webhook events.
To handle failures, the result is in the form of .ExceptT WebhookFailure
Webhook as a WAI application
Arguments
| :: ChannelSecret | Channel secret |
| -> ([Event] -> IO ()) | Event handler |
| -> (WebhookFailure -> Application) | Error handler. Just to return 400 for failures, use |
| -> Application |
A webhook handler for WAI. It uses webhook internally and returns a WAI
Application.
An example webhook server using WAI will be like below:
app :: Application
app req f = case pathInfo req of
"webhook" : _ -> do
secret <- getChannelSecret
webhookApp secret handler defaultOnFailure req f
_ -> undefined
handler :: [Event] -> IO ()
handler events = forM_ events handleEvent
handleEvent :: Event -> IO ()
handleEvent (MessageEvent event) = undefined -- handle a message event
handleEvent _ = return ()
defaultOnFailure :: WebhookFailure -> Application Source #
A basic error handler to be used with webhookApp. It returns 400 Bad
Request with the WebhookFailure code for its body.
Webhook as a Scotty action
Arguments
| :: ChannelSecret | Channel secret |
| -> ([Event] -> IO ()) | Event handler |
| -> (WebhookFailure -> ActionM ()) | Error handler. Just to return 400 for failures, use |
| -> ActionM () |
A webhook handler for Scotty. It uses webhook internally and returns a
Scotty action of type ActionM ()
An example webhook server using WAI will be like below:
main :: IO () main = scotty 3000 $ do post "/webhook" $ webhookAction handler defaultOnFailure' handler :: [Event] -> IO () handler events = forM_ events handleEvent handleEvent :: Event -> IO () handleEvent (MessageEvent event) = undefined -- handle a message event handleEvent _ = return ()
defaultOnFailure' :: WebhookFailure -> ActionM () Source #
A basic error handler to be used with webhookAction. It returns 400 Bad
Request with the WebhookFailure code for its body. It has the same purpose
as defaultOnFailure, except that it is for Scotty.