From 30b7a52c890626312bebcf7382c894af91db09fd Mon Sep 17 00:00:00 2001 From: Andrey Prokopenko Date: Sun, 26 May 2024 14:32:50 +0200 Subject: [PATCH] Parse Maybe fields optionally --- .../Bot/API/InlineMode/InlineQueryResult.hs | 52 +++++++++---------- .../Telegram/Bot/API/Types/BackgroundType.hs | 10 ++-- .../Telegram/Bot/API/Types/ChatBoostSource.hs | 7 +-- .../Telegram/Bot/API/Types/MessageOrigin.hs | 6 +-- 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/telegram-bot-api/src/Telegram/Bot/API/InlineMode/InlineQueryResult.hs b/telegram-bot-api/src/Telegram/Bot/API/InlineMode/InlineQueryResult.hs index 7d2476e..8a5296d 100644 --- a/telegram-bot-api/src/Telegram/Bot/API/InlineMode/InlineQueryResult.hs +++ b/telegram-bot-api/src/Telegram/Bot/API/InlineMode/InlineQueryResult.hs @@ -321,30 +321,30 @@ instance FromJSON InlineQueryResult where (o .: "type" :: Parser Text) >>= \case "article" -> InlineQueryResultArticle <$> parseJSON (Object o) - <*> o .: "url" - <*> o .: "hide_url" + <*> o .:? "url" + <*> o .:? "hide_url" "photo" -> parseFileId o "photo_file_id" >>= \case Nothing -> InlineQueryResultPhoto <$> parseJSON (Object o) -- generic thumbnail <*> o .: "photo_url" - <*> o .: "photo_width" - <*> o .: "photo_height" + <*> o .:? "photo_width" + <*> o .:? "photo_height" Just fileId -> InlineQueryResultCachedPhoto <$> parseJSON (Object o) <*> pure fileId "gif" -> parseFileId o "gif_file_id" >>= \case Nothing -> InlineQueryResultGif <$> parseJSON (Object o) -- generic thumbnail <*> o .: "gif_url" - <*> o .: "gif_width" - <*> o .: "gif_height" - <*> o .: "gif_duration" + <*> o .:? "gif_width" + <*> o .:? "gif_height" + <*> o .:? "gif_duration" Just fileId -> InlineQueryResultCachedGif <$> parseJSON (Object o) <*> pure fileId "mpeg4_gif" -> parseFileId o "mpeg4_file_id" >>= \case Nothing -> InlineQueryResultMpeg4Gif <$> parseJSON (Object o) -- generic thumbnail <*> o .: "mpeg4_url" - <*> o .: "mpeg4_width" - <*> o .: "mpeg4_height" - <*> o .: "mpeg4_duration" + <*> o .:? "mpeg4_width" + <*> o .:? "mpeg4_height" + <*> o .:? "mpeg4_duration" Just fileId -> InlineQueryResultCachedMpeg4Gif <$> parseJSON (Object o) <*> pure fileId @@ -353,9 +353,9 @@ instance FromJSON InlineQueryResult where <$> parseJSON (Object o) <*> o .: "video_url" <*> o .: "mime_type" - <*> o .: "video_width" - <*> o .: "video_height" - <*> o .: "video_duration" + <*> o .:? "video_width" + <*> o .:? "video_height" + <*> o .:? "video_duration" Just fileId -> InlineQueryResultCachedVideo <$> parseJSON (Object o) <*> pure fileId @@ -363,14 +363,14 @@ instance FromJSON InlineQueryResult where Nothing -> InlineQueryResultAudio <$> parseJSON (Object o) <*> o .: "audio_url" - <*> o .: "performer" - <*> o .: "duration" + <*> o .:? "performer" + <*> o .:? "duration" Just fileId -> InlineQueryResultCachedAudio <$> parseJSON (Object o) <*> pure fileId "voice" -> parseFileId o "voice_file_id" >>= \case Nothing -> InlineQueryResultVoice <$> parseJSON (Object o) <*> o .: "voice_url" - <*> o .: "voice_duration" + <*> o .:? "voice_duration" Just fileId -> InlineQueryResultCachedVoice <$> parseJSON (Object o) <*> pure fileId "document" -> parseFileId o "document_file_id" >>= \case Nothing -> InlineQueryResultDocument @@ -382,25 +382,25 @@ instance FromJSON InlineQueryResult where <$> parseJSON (Object o) <*> o .: "latitude" <*> o .: "longitude" - <*> o .: "horizontal_accuracy" - <*> o .: "live_period" - <*> o .: "heading" - <*> o .: "proximity_alert_radius" + <*> o .:? "horizontal_accuracy" + <*> o .:? "live_period" + <*> o .:? "heading" + <*> o .:? "proximity_alert_radius" "venue" -> InlineQueryResultVenue <$> parseJSON (Object o) <*> o .: "latitude" <*> o .: "longitude" <*> o .: "address" - <*> o .: "foursquare_id" - <*> o .: "foursquare_type" - <*> o .: "google_place_id" - <*> o .: "google_place_type" + <*> o .:? "foursquare_id" + <*> o .:? "foursquare_type" + <*> o .:? "google_place_id" + <*> o .:? "google_place_type" "contact" -> InlineQueryResultContact <$> parseJSON (Object o) <*> o .: "phone_number" <*> o .: "first_name" - <*> o .: "last_name" - <*> o .: "vcard" + <*> o .:? "last_name" + <*> o .:? "vcard" "game" -> InlineQueryResultGame <$> parseJSON (Object o) <*> o .: "game_short_name" diff --git a/telegram-bot-api/src/Telegram/Bot/API/Types/BackgroundType.hs b/telegram-bot-api/src/Telegram/Bot/API/Types/BackgroundType.hs index a5b50b0..fa04069 100644 --- a/telegram-bot-api/src/Telegram/Bot/API/Types/BackgroundType.hs +++ b/telegram-bot-api/src/Telegram/Bot/API/Types/BackgroundType.hs @@ -4,7 +4,7 @@ {-# LANGUAGE OverloadedStrings #-} module Telegram.Bot.API.Types.BackgroundType where -import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), (.=), (.:), withObject) +import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), (.=), (.:), (.:?), withObject) import Data.Aeson.Types (Parser) import Data.Text (Text) import GHC.Generics (Generic) @@ -82,15 +82,15 @@ instance FromJSON BackgroundType where <$> o .: "type" <*> o .: "document" <*> o .: "dark_theme_dimming" - <*> o .: "is_blurred" - <*> o .: "is_moving" + <*> o .:? "is_blurred" + <*> o .:? "is_moving" "pattern" -> BackgroundTypePattern <$> o .: "type" <*> o .: "document" <*> o .: "fill" <*> o .: "intensity" - <*> o .: "is_inverted" - <*> o .: "is_moving" + <*> o .:? "is_inverted" + <*> o .:? "is_moving" "chat_theme" -> BackgroundTypeChatTheme <$> o .: "type" <*> o .: "theme_name" diff --git a/telegram-bot-api/src/Telegram/Bot/API/Types/ChatBoostSource.hs b/telegram-bot-api/src/Telegram/Bot/API/Types/ChatBoostSource.hs index cde0d7d..467e30c 100644 --- a/telegram-bot-api/src/Telegram/Bot/API/Types/ChatBoostSource.hs +++ b/telegram-bot-api/src/Telegram/Bot/API/Types/ChatBoostSource.hs @@ -4,7 +4,7 @@ {-# LANGUAGE OverloadedStrings #-} module Telegram.Bot.API.Types.ChatBoostSource where -import Data.Aeson (FromJSON (..), ToJSON (..), KeyValue ((.=)), Value (..), withObject, (.:)) +import Data.Aeson (FromJSON (..), ToJSON (..), KeyValue ((.=)), Value (..), withObject, (.:), (.:?)) import Data.Aeson.Types (Parser) import Data.Text (Text) import GHC.Generics (Generic) @@ -22,6 +22,7 @@ import Telegram.Bot.API.Types.User -- * ChatBoostSourcePremium -- * ChatBoostSourceGiftCode -- * ChatBoostSourceGiveaway +-- data ChatBoostSource -- | The boost was obtained by subscribing to Telegram Premium or by gifting a Telegram Premium subscription to another user. = ChatBoostSourcePremium @@ -68,6 +69,6 @@ instance FromJSON ChatBoostSource where "giveaway" -> ChatBoostSourceGiveaway <$> o .: "source" <*> o .: "giveaway_message_id" - <*> o .: "user" - <*> o .: "is_unclaimed" + <*> o .:? "user" + <*> o .:? "is_unclaimed" t -> fail $ Text.unpack ("Unknown source: " <> t) diff --git a/telegram-bot-api/src/Telegram/Bot/API/Types/MessageOrigin.hs b/telegram-bot-api/src/Telegram/Bot/API/Types/MessageOrigin.hs index 30c7069..4075792 100644 --- a/telegram-bot-api/src/Telegram/Bot/API/Types/MessageOrigin.hs +++ b/telegram-bot-api/src/Telegram/Bot/API/Types/MessageOrigin.hs @@ -4,7 +4,7 @@ {-# LANGUAGE OverloadedStrings #-} module Telegram.Bot.API.Types.MessageOrigin where -import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), (.=), (.:), withObject) +import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), (.=), (.:), (.:?), withObject) import Data.Aeson.Types (Parser) import Data.Text (Text) import Data.Time.Clock.POSIX (POSIXTime) @@ -86,12 +86,12 @@ instance FromJSON MessageOrigin where <$> o .: "type" <*> o .: "date" <*> o .: "sender_chat" - <*> o .: "author_signature" + <*> o .:? "author_signature" "channel" -> MessageOriginChannel <$> o .: "type" <*> o .: "date" <*> o .: "chat" <*> o .: "message_id" - <*> o .: "author_signature" + <*> o .:? "author_signature" t -> fail $ Text.unpack ("Unknown type: " <> t)