line-2.1.0.0: Haskell SDK for the LINE API

Safe HaskellNone
LanguageHaskell2010

Line.Messaging.Webhook

Contents

Description

This module provides webhook handlers both general and WAI-specific.

Synopsis

Types

Re-exported for convenience.

Basic webhook

webhook Source #

Arguments

:: Monad m 
=> ChannelSecret 
-> ByteString

Request body

-> Signature

Auth signature in X-Line-Signature header

-> 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

webhookApp Source #

Arguments

:: ChannelSecret

Channel secret

-> ([Event] -> IO ())

Event handler

-> (WebhookFailure -> Application)

Error handler. Just to return 400 for failures, use defaultOnFailure.

-> 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

webhookAction Source #

Arguments

:: ChannelSecret

Channel secret

-> ([Event] -> IO ())

Event handler

-> (WebhookFailure -> ActionM ())

Error handler. Just to return 400 for failures, use defaultOnFailure'.

-> 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
  get "/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.