{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Telegram.Bot.API.Methods.SendAudio where
import Control.Monad.IO.Class (liftIO)
import Data.Aeson (ToJSON (..))
import Data.Aeson.Text (encodeToLazyText)
import Data.Bool
import Data.Maybe (catMaybes)
import Data.Functor ((<&>))
import Data.Proxy
import Data.Text
import GHC.Generics (Generic)
import Servant.API
import Servant.Multipart.API
import Servant.Multipart.Client
import Servant.Client hiding (Response)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Telegram.Bot.API.Internal.Utils
import Telegram.Bot.API.MakingRequests
import Telegram.Bot.API.Types
import Telegram.Bot.API.Types.ParseMode
import Telegram.Bot.API.Internal.TH
data SendAudioRequest = SendAudioRequest
{ SendAudioRequest -> SomeChatId
sendAudioChatId :: SomeChatId
, SendAudioRequest -> Maybe MessageThreadId
sendAudioMessageThreadId :: Maybe MessageThreadId
, SendAudioRequest -> InputFile
sendAudioAudio :: InputFile
, SendAudioRequest -> Maybe Int
sendAudioDuration :: Maybe Int
, SendAudioRequest -> Maybe Text
sendAudioPerformer :: Maybe Text
, SendAudioRequest -> Maybe Text
sendAudioTitle :: Maybe Text
, SendAudioRequest -> Maybe InputFile
sendAudioThumbnail :: Maybe InputFile
, SendAudioRequest -> Maybe Text
sendAudioCaption :: Maybe Text
, SendAudioRequest -> Maybe ParseMode
sendAudioParseMode :: Maybe ParseMode
, SendAudioRequest -> Maybe [MessageEntity]
sendAudioCaptionEntities :: Maybe [MessageEntity]
, SendAudioRequest -> Maybe Bool
sendAudioDisableNotification :: Maybe Bool
, SendAudioRequest -> Maybe Bool
sendAudioProtectContent :: Maybe Bool
, SendAudioRequest -> Maybe MessageId
sendAudioReplyToMessageId :: Maybe MessageId
, SendAudioRequest -> Maybe ReplyParameters
sendAudioReplyParameters :: Maybe ReplyParameters
, SendAudioRequest -> Maybe InlineKeyboardMarkup
sendAudioReplyMarkup :: Maybe InlineKeyboardMarkup
}
deriving (forall x. SendAudioRequest -> Rep SendAudioRequest x)
-> (forall x. Rep SendAudioRequest x -> SendAudioRequest)
-> Generic SendAudioRequest
forall x. Rep SendAudioRequest x -> SendAudioRequest
forall x. SendAudioRequest -> Rep SendAudioRequest x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SendAudioRequest -> Rep SendAudioRequest x
from :: forall x. SendAudioRequest -> Rep SendAudioRequest x
$cto :: forall x. Rep SendAudioRequest x -> SendAudioRequest
to :: forall x. Rep SendAudioRequest x -> SendAudioRequest
Generic
instance ToJSON SendAudioRequest where toJSON :: SendAudioRequest -> Value
toJSON = SendAudioRequest -> Value
forall a (d :: Meta) (f :: * -> *).
(Generic a, GToJSON Zero (Rep a), Rep a ~ D1 d f, Datatype d) =>
a -> Value
gtoJSON
instance ToMultipart Tmp SendAudioRequest where
toMultipart :: SendAudioRequest -> MultipartData Tmp
toMultipart SendAudioRequest{Maybe Bool
Maybe Int
Maybe [MessageEntity]
Maybe Text
Maybe MessageThreadId
Maybe MessageId
Maybe ParseMode
Maybe InlineKeyboardMarkup
Maybe ReplyParameters
Maybe InputFile
SomeChatId
InputFile
sendAudioChatId :: SendAudioRequest -> SomeChatId
sendAudioMessageThreadId :: SendAudioRequest -> Maybe MessageThreadId
sendAudioAudio :: SendAudioRequest -> InputFile
sendAudioDuration :: SendAudioRequest -> Maybe Int
sendAudioPerformer :: SendAudioRequest -> Maybe Text
sendAudioTitle :: SendAudioRequest -> Maybe Text
sendAudioThumbnail :: SendAudioRequest -> Maybe InputFile
sendAudioCaption :: SendAudioRequest -> Maybe Text
sendAudioParseMode :: SendAudioRequest -> Maybe ParseMode
sendAudioCaptionEntities :: SendAudioRequest -> Maybe [MessageEntity]
sendAudioDisableNotification :: SendAudioRequest -> Maybe Bool
sendAudioProtectContent :: SendAudioRequest -> Maybe Bool
sendAudioReplyToMessageId :: SendAudioRequest -> Maybe MessageId
sendAudioReplyParameters :: SendAudioRequest -> Maybe ReplyParameters
sendAudioReplyMarkup :: SendAudioRequest -> Maybe InlineKeyboardMarkup
sendAudioChatId :: SomeChatId
sendAudioMessageThreadId :: Maybe MessageThreadId
sendAudioAudio :: InputFile
sendAudioDuration :: Maybe Int
sendAudioPerformer :: Maybe Text
sendAudioTitle :: Maybe Text
sendAudioThumbnail :: Maybe InputFile
sendAudioCaption :: Maybe Text
sendAudioParseMode :: Maybe ParseMode
sendAudioCaptionEntities :: Maybe [MessageEntity]
sendAudioDisableNotification :: Maybe Bool
sendAudioProtectContent :: Maybe Bool
sendAudioReplyToMessageId :: Maybe MessageId
sendAudioReplyParameters :: Maybe ReplyParameters
sendAudioReplyMarkup :: Maybe InlineKeyboardMarkup
..} =
(MultipartData Tmp -> MultipartData Tmp)
-> (InputFile -> MultipartData Tmp -> MultipartData Tmp)
-> Maybe InputFile
-> MultipartData Tmp
-> MultipartData Tmp
forall b a. b -> (a -> b) -> Maybe a -> b
maybe MultipartData Tmp -> MultipartData Tmp
forall a. a -> a
id (Text -> InputFile -> MultipartData Tmp -> MultipartData Tmp
makeFile Text
"Thumbnail") Maybe InputFile
sendAudioThumbnail (MultipartData Tmp -> MultipartData Tmp)
-> MultipartData Tmp -> MultipartData Tmp
forall a b. (a -> b) -> a -> b
$
Text -> InputFile -> MultipartData Tmp -> MultipartData Tmp
makeFile Text
"audio" InputFile
sendAudioAudio (MultipartData Tmp -> MultipartData Tmp)
-> MultipartData Tmp -> MultipartData Tmp
forall a b. (a -> b) -> a -> b
$
[Input] -> [FileData Tmp] -> MultipartData Tmp
forall tag. [Input] -> [FileData tag] -> MultipartData tag
MultipartData [Input]
fields [] where
fields :: [Input]
fields =
[ Text -> Text -> Input
Input Text
"chat_id" (Text -> Input) -> Text -> Input
forall a b. (a -> b) -> a -> b
$ case SomeChatId
sendAudioChatId of
SomeChatId (ChatId Integer
chat_id) -> String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Integer -> String
forall a. Show a => a -> String
show Integer
chat_id
SomeChatUsername Text
txt -> Text
txt
] [Input] -> [Input] -> [Input]
forall a. Semigroup a => a -> a -> a
<> [Maybe Input] -> [Input]
forall a. [Maybe a] -> [a]
catMaybes
[ Maybe MessageThreadId
sendAudioMessageThreadId Maybe MessageThreadId -> (MessageThreadId -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\MessageThreadId
t -> Text -> Text -> Input
Input Text
"message_thread_id" (String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ MessageThreadId -> String
forall a. Show a => a -> String
show MessageThreadId
t)
, Maybe Text
sendAudioCaption Maybe Text -> (Text -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Text
t -> Text -> Text -> Input
Input Text
"caption" Text
t
, Maybe ParseMode
sendAudioParseMode Maybe ParseMode -> (ParseMode -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\ParseMode
t -> Text -> Text -> Input
Input Text
"parse_mode" (Text -> Text
TL.toStrict (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
TL.replace Text
"\"" Text
"" (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ ParseMode -> Text
forall a. ToJSON a => a -> Text
encodeToLazyText ParseMode
t)
, Maybe [MessageEntity]
sendAudioCaptionEntities Maybe [MessageEntity] -> ([MessageEntity] -> Input) -> Maybe Input
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 (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ [MessageEntity] -> Text
forall a. ToJSON a => a -> Text
encodeToLazyText [MessageEntity]
t)
, Maybe Int
sendAudioDuration Maybe Int -> (Int -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Int
t -> Text -> Text -> Input
Input Text
"duration" (Text -> Text
TL.toStrict (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Int -> Text
forall a. ToJSON a => a -> Text
encodeToLazyText Int
t)
, Maybe Text
sendAudioPerformer Maybe Text -> (Text -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Text
t -> Text -> Text -> Input
Input Text
"performer" Text
t
, Maybe Text
sendAudioTitle Maybe Text -> (Text -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Text
t -> Text -> Text -> Input
Input Text
"title" (Text -> Text
TL.toStrict (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
forall a. ToJSON a => a -> Text
encodeToLazyText Text
t)
, Maybe Bool
sendAudioDisableNotification Maybe Bool -> (Bool -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Bool
t -> Text -> Text -> Input
Input Text
"disable_notification" (Text -> Text -> Bool -> Text
forall a. a -> a -> Bool -> a
bool Text
"false" Text
"true" Bool
t)
, Maybe Bool
sendAudioProtectContent Maybe Bool -> (Bool -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Bool
t -> Text -> Text -> Input
Input Text
"protect_content" (Text -> Text -> Bool -> Text
forall a. a -> a -> Bool -> a
bool Text
"false" Text
"true" Bool
t)
, Maybe MessageId
sendAudioReplyToMessageId Maybe MessageId -> (MessageId -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\MessageId
t -> Text -> Text -> Input
Input Text
"reply_to_message_id" (Text -> Text
TL.toStrict (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ MessageId -> Text
forall a. ToJSON a => a -> Text
encodeToLazyText MessageId
t)
, Maybe ReplyParameters
sendAudioReplyParameters Maybe ReplyParameters -> (ReplyParameters -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\ReplyParameters
t -> Text -> Text -> Input
Input Text
"reply_parameters" (Text -> Text
TL.toStrict (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ ReplyParameters -> Text
forall a. ToJSON a => a -> Text
encodeToLazyText ReplyParameters
t)
, Maybe InlineKeyboardMarkup
sendAudioReplyMarkup Maybe InlineKeyboardMarkup
-> (InlineKeyboardMarkup -> Input) -> Maybe Input
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\InlineKeyboardMarkup
t -> Text -> Text -> Input
Input Text
"reply_markup" (Text -> Text
TL.toStrict (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ InlineKeyboardMarkup -> Text
forall a. ToJSON a => a -> Text
encodeToLazyText InlineKeyboardMarkup
t)
]
type SendAudioContent
= "sendAudio"
:> MultipartForm Tmp SendAudioRequest
:> Post '[JSON] (Response Message)
type SendAudioLink
= "sendAudio"
:> ReqBody '[JSON] SendAudioRequest
:> Post '[JSON] (Response Message)
sendAudio :: SendAudioRequest -> ClientM (Response Message)
sendAudio :: SendAudioRequest -> ClientM (Response Message)
sendAudio SendAudioRequest
r = case (SendAudioRequest -> InputFile
sendAudioAudio SendAudioRequest
r, SendAudioRequest -> Maybe InputFile
sendAudioThumbnail SendAudioRequest
r) of
(InputFile{}, Maybe InputFile
_) -> do
ByteString
boundary <- IO ByteString -> ClientM ByteString
forall a. IO a -> ClientM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ByteString
genBoundary
Proxy SendAudioContent -> Client ClientM SendAudioContent
forall api.
HasClient ClientM api =>
Proxy api -> Client ClientM api
client (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @SendAudioContent) (ByteString
boundary, SendAudioRequest
r)
(InputFile
_, Just InputFile{}) -> do
ByteString
boundary <- IO ByteString -> ClientM ByteString
forall a. IO a -> ClientM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ByteString
genBoundary
Proxy SendAudioContent -> Client ClientM SendAudioContent
forall api.
HasClient ClientM api =>
Proxy api -> Client ClientM api
client (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @SendAudioContent) (ByteString
boundary, SendAudioRequest
r)
(InputFile, Maybe InputFile)
_ -> Proxy SendAudioLink -> Client ClientM SendAudioLink
forall api.
HasClient ClientM api =>
Proxy api -> Client ClientM api
client (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @SendAudioLink) SendAudioRequest
r
makeDefault ''SendAudioRequest