{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Web.Telegram.Types.Internal.Update where
import Data.Aeson
import Data.Monoid
import Data.Text (Text)
import Deriving.Aeson
import qualified Web.Telegram.Types.Internal.Common as C
import qualified Web.Telegram.Types.Internal.InlineQuery as IQ
import qualified Web.Telegram.Types.Internal.Media as M
import Web.Telegram.Types.Internal.UpdateType (UpdateType)
import Web.Telegram.Types.Internal.Utils
data Update
=
Message
{ updateId :: Integer,
message :: C.Message
}
|
EditedMessage
{ updateId :: Integer,
message :: C.Message
}
|
ChannelPost
{ updateId :: Integer,
message :: C.Message
}
|
EditedChannelPost
{ updateId :: Integer,
message :: C.Message
}
|
InlineQuery
{ updateId :: Integer,
iquery :: IQ.InlineQuery
}
|
ChosenInlineResult
{ updateId :: Integer,
result :: IQ.ChosenInlineResult
}
|
CallbackQuery
{ updateId :: Integer,
cbquery :: C.CallbackQuery
}
|
ShippingQuery
{ updateId :: Integer,
squery :: C.ShippingQuery
}
|
PreCheckoutQuery
{ updateId :: Integer,
pcquery :: C.PreCheckoutQuery
}
|
PollUpdate
{ updateId :: Integer,
poll :: M.Poll
}
|
PollAnswer
{ updateId :: Integer,
answer :: M.PollAnswer
}
deriving (Show, Eq, Generic, Default)
instance FromJSON Update where
parseJSON = withObject "Update object" $ \o -> do
uid <- o .: "update_id"
let pair (k, c) = do
m <- o .:? k
return $ fmap (c uid) m
l <-
sequence
[ pair ("message", Message),
pair ("edited_message", EditedMessage),
pair ("channel_post", ChannelPost),
pair ("edited_channel_post", EditedChannelPost),
pair ("inline_query", InlineQuery),
pair ("chosen_inline_result", ChosenInlineResult),
pair ("callback_query", CallbackQuery),
pair ("shipping_query", ShippingQuery),
pair ("pre_checkout_query", PreCheckoutQuery),
pair ("poll", PollUpdate),
pair ("poll_answer", PollAnswer)
]
let r = getFirst $ foldMap First l
case r of
Nothing -> fail "Empty Message"
Just r' -> return r'
data WebhookInfo
= WebhookInfo
{
url :: Text,
hasCustomCertificate :: Bool,
pendingUpdateCount :: Integer,
lastErrorDate :: Maybe Integer,
lastErrorMessage :: Maybe Text,
maxConnections :: Maybe Integer,
allowedUpdates :: Maybe [UpdateType]
}
deriving (Show, Eq, Generic, Default)
deriving
(FromJSON, ToJSON)
via Snake WebhookInfo