{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Telegram.Bot.API.Types.InputMedia where
import Data.Aeson (ToJSON (..), KeyValue ((.=)))
import Data.Aeson.Text (encodeToLazyText)
import Data.Bool (bool)
import Data.Maybe (catMaybes)
import Data.Functor ((<&>))
import Data.Text (Text, pack)
import GHC.Generics (Generic)
import Servant.Multipart.API
import System.FilePath
import qualified Data.Text.Lazy as TL
import Telegram.Bot.API.Types.Common
import Telegram.Bot.API.Types.MessageEntity
import Telegram.Bot.API.Internal.Utils
data InputMediaGeneric = InputMediaGeneric
{ InputMediaGeneric -> InputFile
inputMediaGenericMedia :: InputFile
, InputMediaGeneric -> Maybe Text
inputMediaGenericCaption :: Maybe Text
, InputMediaGeneric -> Maybe Text
inputMediaGenericParseMode :: Maybe Text
, InputMediaGeneric -> Maybe [MessageEntity]
inputMediaGenericCaptionEntities :: Maybe [MessageEntity]
}
deriving forall x. Rep InputMediaGeneric x -> InputMediaGeneric
forall x. InputMediaGeneric -> Rep InputMediaGeneric x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep InputMediaGeneric x -> InputMediaGeneric
$cfrom :: forall x. InputMediaGeneric -> Rep InputMediaGeneric x
Generic
instance ToJSON InputMediaGeneric where toJSON :: InputMediaGeneric -> Value
toJSON = forall a (d :: Meta) (f :: * -> *).
(Generic a, GToJSON Zero (Rep a), Rep a ~ D1 d f, Datatype d) =>
a -> Value
gtoJSON
instance ToMultipart Tmp InputMediaGeneric where
toMultipart :: InputMediaGeneric -> MultipartData Tmp
toMultipart InputMediaGeneric{Maybe [MessageEntity]
Maybe Text
InputFile
inputMediaGenericCaptionEntities :: Maybe [MessageEntity]
inputMediaGenericParseMode :: Maybe Text
inputMediaGenericCaption :: Maybe Text
inputMediaGenericMedia :: InputFile
inputMediaGenericCaptionEntities :: InputMediaGeneric -> Maybe [MessageEntity]
inputMediaGenericParseMode :: InputMediaGeneric -> Maybe Text
inputMediaGenericCaption :: InputMediaGeneric -> Maybe Text
inputMediaGenericMedia :: InputMediaGeneric -> InputFile
..} = Text -> InputFile -> MultipartData Tmp -> MultipartData Tmp
makeFile Text
"media" InputFile
inputMediaGenericMedia (forall tag. [Input] -> [FileData tag] -> MultipartData tag
MultipartData [Input]
fields []) where
fields :: [Input]
fields = forall a. [Maybe a] -> [a]
catMaybes
[ Maybe Text
inputMediaGenericCaption forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Text
t -> Text -> Text -> Input
Input Text
"caption" Text
t
, Maybe Text
inputMediaGenericParseMode forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Text
t -> Text -> Text -> Input
Input Text
"parse_mode" Text
t
, Maybe [MessageEntity]
inputMediaGenericCaptionEntities forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\[MessageEntity]
t -> Text -> Text -> Input
Input Text
"caption_entities" (Text -> Text
TL.toStrict forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText [MessageEntity]
t)
]
data InputMediaGenericThumb = InputMediaGenericThumb
{ InputMediaGenericThumb -> InputMediaGeneric
inputMediaGenericGeneric :: InputMediaGeneric
, InputMediaGenericThumb -> Maybe InputFile
inputMediaGenericThumb :: Maybe InputFile
}
instance ToJSON InputMediaGenericThumb where
toJSON :: InputMediaGenericThumb -> Value
toJSON InputMediaGenericThumb{Maybe InputFile
InputMediaGeneric
inputMediaGenericThumb :: Maybe InputFile
inputMediaGenericGeneric :: InputMediaGeneric
inputMediaGenericThumb :: InputMediaGenericThumb -> Maybe InputFile
inputMediaGenericGeneric :: InputMediaGenericThumb -> InputMediaGeneric
..}
= Value -> [Pair] -> Value
addJsonFields (forall a. ToJSON a => a -> Value
toJSON InputMediaGeneric
inputMediaGenericGeneric)
[Key
"thumb" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe InputFile
inputMediaGenericThumb]
instance ToMultipart Tmp InputMediaGenericThumb where
toMultipart :: InputMediaGenericThumb -> MultipartData Tmp
toMultipart = \case
InputMediaGenericThumb InputMediaGeneric
generic Maybe InputFile
Nothing -> forall tag a. ToMultipart tag a => a -> MultipartData tag
toMultipart InputMediaGeneric
generic
InputMediaGenericThumb InputMediaGeneric
generic (Just InputFile
thumb) -> Text -> InputFile -> MultipartData Tmp -> MultipartData Tmp
makeFile Text
"thumb" InputFile
thumb (forall tag a. ToMultipart tag a => a -> MultipartData tag
toMultipart InputMediaGeneric
generic)
data InputMedia
= InputMediaPhoto
{ InputMedia -> InputMediaGeneric
inputMediaPhotoGeneric :: InputMediaGeneric
, InputMedia -> Maybe Bool
inputMediaPhotoHasSpoiler :: Maybe Bool
}
| InputMediaVideo
{ InputMedia -> InputMediaGenericThumb
inputMediaVideoGeneric :: InputMediaGenericThumb
, InputMedia -> Maybe Integer
inputMediaVideoWidth :: Maybe Integer
, InputMedia -> Maybe Integer
inputMediaVideoHeight :: Maybe Integer
, InputMedia -> Maybe Integer
inputMediaVideoDuration :: Maybe Integer
, InputMedia -> Maybe Bool
inputMediaVideoSupportsStreaming :: Maybe Bool
, InputMedia -> Maybe Bool
inputMediaVideoHasSpoiler :: Maybe Bool
}
| InputMediaAnimation
{ InputMedia -> InputMediaGenericThumb
inputMediaAnimationGeneric :: InputMediaGenericThumb
, InputMedia -> Maybe Integer
inputMediaAnimationWidth :: Maybe Integer
, InputMedia -> Maybe Integer
inputMediaAnimationHeight :: Maybe Integer
, InputMedia -> Maybe Integer
inputMediaAnimationDuration :: Maybe Integer
, InputMedia -> Maybe Bool
inputMediaAnimationHasSpoiler :: Maybe Bool
}
| InputMediaAudio
{ InputMedia -> InputMediaGenericThumb
inputMediaAudioGeneric :: InputMediaGenericThumb
, InputMedia -> Maybe Integer
inputMediaAudioDuration :: Maybe Integer
, InputMedia -> Maybe Text
inputMediaAudioPerformer :: Maybe Text
, InputMedia -> Maybe Text
inputMediaAudioTitle :: Maybe Text
}
| InputMediaDocument
{ InputMedia -> InputMediaGenericThumb
inputMediaDocumentGeneric :: InputMediaGenericThumb
, InputMedia -> Maybe Bool
inputMediaDocumentDisableContentTypeDetection :: Maybe Bool
}
instance ToJSON InputMedia where
toJSON :: InputMedia -> Value
toJSON = \case
InputMediaPhoto InputMediaGeneric
img Maybe Bool
spoiler ->
Value -> [Pair] -> Value
addJsonFields (forall a. ToJSON a => a -> Value
toJSON InputMediaGeneric
img) (Text -> [Pair] -> [Pair]
addType Text
"photo" [ Key
"has_spoiler" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Bool
spoiler])
InputMediaVideo InputMediaGenericThumb
imgt Maybe Integer
width Maybe Integer
height Maybe Integer
duration Maybe Bool
streaming Maybe Bool
spoiler ->
Value -> [Pair] -> Value
addJsonFields (forall a. ToJSON a => a -> Value
toJSON InputMediaGenericThumb
imgt)
(Text -> [Pair] -> [Pair]
addType Text
"video"
[ Key
"width" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Integer
width
, Key
"height" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Integer
height
, Key
"duration" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Integer
duration
, Key
"support_streaming" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Bool
streaming
, Key
"has_spoiler" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Bool
spoiler
])
InputMediaAnimation InputMediaGenericThumb
imgt Maybe Integer
width Maybe Integer
height Maybe Integer
duration Maybe Bool
spoiler ->
Value -> [Pair] -> Value
addJsonFields (forall a. ToJSON a => a -> Value
toJSON InputMediaGenericThumb
imgt)
(Text -> [Pair] -> [Pair]
addType Text
"animation"
[ Key
"width" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Integer
width
, Key
"height" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Integer
height
, Key
"duration" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Integer
duration
, Key
"has_spoiler" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Bool
spoiler
])
InputMediaAudio InputMediaGenericThumb
imgt Maybe Integer
duration Maybe Text
performer Maybe Text
title ->
Value -> [Pair] -> Value
addJsonFields (forall a. ToJSON a => a -> Value
toJSON InputMediaGenericThumb
imgt)
(Text -> [Pair] -> [Pair]
addType Text
"audio"
[ Key
"duration" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Integer
duration
, Key
"performer" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
performer
, Key
"title" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
title
])
InputMediaDocument InputMediaGenericThumb
imgt Maybe Bool
dctd ->
Value -> [Pair] -> Value
addJsonFields (forall a. ToJSON a => a -> Value
toJSON InputMediaGenericThumb
imgt)
(Text -> [Pair] -> [Pair]
addType Text
"document" [Key
"disable_content_type_detection" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Bool
dctd])
instance ToMultipart Tmp InputMedia where
toMultipart :: InputMedia -> MultipartData Tmp
toMultipart = let
in \case
InputMediaPhoto InputMediaGeneric
img Maybe Bool
spoiler ->
forall tag. [Input] -> MultipartData tag -> MultipartData tag
addMultipartFields
(Text -> Text -> Input
Input Text
"type" Text
"photo"
forall a. a -> [a] -> [a]
: forall a. [Maybe a] -> [a]
catMaybes
[ Maybe Bool
spoiler forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Bool
t -> Text -> Text -> Input
Input Text
"has_spoiler" (forall a. a -> a -> Bool -> a
bool Text
"false" Text
"true" Bool
t)
]) (forall tag a. ToMultipart tag a => a -> MultipartData tag
toMultipart InputMediaGeneric
img)
InputMediaVideo InputMediaGenericThumb
imgt Maybe Integer
width Maybe Integer
height Maybe Integer
duration Maybe Bool
streaming Maybe Bool
spoiler ->
forall tag. [Input] -> MultipartData tag -> MultipartData tag
addMultipartFields
(Text -> Text -> Input
Input Text
"type" Text
"video"
forall a. a -> [a] -> [a]
: forall a. [Maybe a] -> [a]
catMaybes
[ Maybe Integer
width forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Integer
t -> Text -> Text -> Input
Input Text
"width" (Text -> Text
TL.toStrict forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText Integer
t)
, Maybe Integer
height forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Integer
t -> Text -> Text -> Input
Input Text
"height" (Text -> Text
TL.toStrict forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText Integer
t)
, Maybe Integer
duration forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Integer
t -> Text -> Text -> Input
Input Text
"duration" (Text -> Text
TL.toStrict forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText Integer
t)
, Maybe Bool
streaming forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Bool
t -> Text -> Text -> Input
Input Text
"support_streaming" (forall a. a -> a -> Bool -> a
bool Text
"false" Text
"true" Bool
t)
, Maybe Bool
spoiler forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Bool
t -> Text -> Text -> Input
Input Text
"has_spoiler" (forall a. a -> a -> Bool -> a
bool Text
"false" Text
"true" Bool
t)
]) (forall tag a. ToMultipart tag a => a -> MultipartData tag
toMultipart InputMediaGenericThumb
imgt)
InputMediaAnimation InputMediaGenericThumb
imgt Maybe Integer
width Maybe Integer
height Maybe Integer
duration Maybe Bool
spoiler ->
forall tag. [Input] -> MultipartData tag -> MultipartData tag
addMultipartFields
(Text -> Text -> Input
Input Text
"type" Text
"animation"
forall a. a -> [a] -> [a]
: forall a. [Maybe a] -> [a]
catMaybes
[ Maybe Integer
width forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Integer
t -> Text -> Text -> Input
Input Text
"width" (Text -> Text
TL.toStrict forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText Integer
t)
, Maybe Integer
height forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Integer
t -> Text -> Text -> Input
Input Text
"height" (Text -> Text
TL.toStrict forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText Integer
t)
, Maybe Integer
duration forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Integer
t -> Text -> Text -> Input
Input Text
"duration" (Text -> Text
TL.toStrict forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText Integer
t)
, Maybe Bool
spoiler forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Bool
t -> Text -> Text -> Input
Input Text
"has_spoiler" (forall a. a -> a -> Bool -> a
bool Text
"false" Text
"true" Bool
t)
]) (forall tag a. ToMultipart tag a => a -> MultipartData tag
toMultipart InputMediaGenericThumb
imgt)
InputMediaAudio InputMediaGenericThumb
imgt Maybe Integer
duration Maybe Text
performer Maybe Text
title ->
forall tag. [Input] -> MultipartData tag -> MultipartData tag
addMultipartFields
(Text -> Text -> Input
Input Text
"type" Text
"audio"
forall a. a -> [a] -> [a]
: forall a. [Maybe a] -> [a]
catMaybes
[ Maybe Integer
duration forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Integer
t -> Text -> Text -> Input
Input Text
"duration" (Text -> Text
TL.toStrict forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText Integer
t)
, Maybe Text
performer forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Text
t -> Text -> Text -> Input
Input Text
"performer" Text
t
, Maybe Text
title forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Text
t -> Text -> Text -> Input
Input Text
"title" Text
t
]) (forall tag a. ToMultipart tag a => a -> MultipartData tag
toMultipart InputMediaGenericThumb
imgt)
InputMediaDocument InputMediaGenericThumb
imgt Maybe Bool
dctd ->
forall tag. [Input] -> MultipartData tag -> MultipartData tag
addMultipartFields
(Text -> Text -> Input
Input Text
"type" Text
"document"
forall a. a -> [a] -> [a]
: forall a. [Maybe a] -> [a]
catMaybes
[ Maybe Bool
dctd forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Bool
t -> Text -> Text -> Input
Input Text
"disable_content_type_detection" (forall a. a -> a -> Bool -> a
bool Text
"false" Text
"true" Bool
t)
]) (forall tag a. ToMultipart tag a => a -> MultipartData tag
toMultipart InputMediaGenericThumb
imgt)
type ContentType = Text
data InputFile
= InputFileId FileId
| FileUrl Text
| InputFile FilePath ContentType
instance ToJSON InputFile where
toJSON :: InputFile -> Value
toJSON (InputFileId FileId
i) = forall a. ToJSON a => a -> Value
toJSON FileId
i
toJSON (FileUrl Text
t) = forall a. ToJSON a => a -> Value
toJSON Text
t
toJSON (InputFile FilePath
f Text
_) = forall a. ToJSON a => a -> Value
toJSON (Text
"attach://" forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
pack (FilePath -> FilePath
takeFileName FilePath
f))
makeFile :: Text -> InputFile -> MultipartData Tmp -> MultipartData Tmp
makeFile :: Text -> InputFile -> MultipartData Tmp -> MultipartData Tmp
makeFile Text
name (InputFile FilePath
path Text
ct) (MultipartData [Input]
fields [FileData Tmp]
files) =
forall tag. [Input] -> [FileData tag] -> MultipartData tag
MultipartData
(Text -> Text -> Input
Input Text
name (Text
"attach://" forall a. Semigroup a => a -> a -> a
<> Text
name) forall a. a -> [a] -> [a]
: [Input]
fields)
(forall tag.
Text -> Text -> Text -> MultipartResult tag -> FileData tag
FileData Text
name (FilePath -> Text
pack forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath
takeFileName FilePath
path) Text
ct FilePath
path forall a. a -> [a] -> [a]
: [FileData Tmp]
files)
makeFile Text
name InputFile
file (MultipartData [Input]
fields [FileData Tmp]
files) =
forall tag. [Input] -> [FileData tag] -> MultipartData tag
MultipartData
(Text -> Text -> Input
Input Text
name (Text -> Text
TL.toStrict forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText InputFile
file) forall a. a -> [a] -> [a]
: [Input]
fields)
[FileData Tmp]
files