{-# 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
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
sendAudioThumb :: 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 Bool
sendAudioAllowSendingWithoutReply :: Maybe Bool
, SendAudioRequest -> Maybe InlineKeyboardMarkup
sendAudioReplyMarkup :: Maybe InlineKeyboardMarkup
}
deriving 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
$cto :: forall x. Rep SendAudioRequest x -> SendAudioRequest
$cfrom :: forall x. SendAudioRequest -> Rep SendAudioRequest x
Generic
instance ToJSON SendAudioRequest where toJSON :: SendAudioRequest -> 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 SendAudioRequest where
toMultipart :: SendAudioRequest -> MultipartData Tmp
toMultipart SendAudioRequest{Maybe Bool
Maybe Int
Maybe [MessageEntity]
Maybe Text
Maybe MessageThreadId
Maybe MessageId
Maybe InlineKeyboardMarkup
Maybe ParseMode
Maybe InputFile
SomeChatId
InputFile
sendAudioReplyMarkup :: Maybe InlineKeyboardMarkup
sendAudioAllowSendingWithoutReply :: Maybe Bool
sendAudioReplyToMessageId :: Maybe MessageId
sendAudioProtectContent :: Maybe Bool
sendAudioDisableNotification :: Maybe Bool
sendAudioCaptionEntities :: Maybe [MessageEntity]
sendAudioParseMode :: Maybe ParseMode
sendAudioCaption :: Maybe Text
sendAudioThumb :: Maybe InputFile
sendAudioTitle :: Maybe Text
sendAudioPerformer :: Maybe Text
sendAudioDuration :: Maybe Int
sendAudioAudio :: InputFile
sendAudioMessageThreadId :: Maybe MessageThreadId
sendAudioChatId :: SomeChatId
sendAudioReplyMarkup :: SendAudioRequest -> Maybe InlineKeyboardMarkup
sendAudioAllowSendingWithoutReply :: SendAudioRequest -> Maybe Bool
sendAudioReplyToMessageId :: SendAudioRequest -> Maybe MessageId
sendAudioProtectContent :: SendAudioRequest -> Maybe Bool
sendAudioDisableNotification :: SendAudioRequest -> Maybe Bool
sendAudioCaptionEntities :: SendAudioRequest -> Maybe [MessageEntity]
sendAudioParseMode :: SendAudioRequest -> Maybe ParseMode
sendAudioCaption :: SendAudioRequest -> Maybe Text
sendAudioThumb :: SendAudioRequest -> Maybe InputFile
sendAudioTitle :: SendAudioRequest -> Maybe Text
sendAudioPerformer :: SendAudioRequest -> Maybe Text
sendAudioDuration :: SendAudioRequest -> Maybe Int
sendAudioAudio :: SendAudioRequest -> InputFile
sendAudioMessageThreadId :: SendAudioRequest -> Maybe MessageThreadId
sendAudioChatId :: SendAudioRequest -> SomeChatId
..} =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (Text -> InputFile -> MultipartData Tmp -> MultipartData Tmp
makeFile Text
"thumb") Maybe InputFile
sendAudioThumb forall a b. (a -> b) -> a -> b
$
Text -> InputFile -> MultipartData Tmp -> MultipartData Tmp
makeFile Text
"audio" InputFile
sendAudioAudio forall a b. (a -> b) -> a -> b
$
forall tag. [Input] -> [FileData tag] -> MultipartData tag
MultipartData [Input]
fields [] where
fields :: [Input]
fields =
[ Text -> Text -> Input
Input Text
"chat_id" forall a b. (a -> b) -> a -> b
$ case SomeChatId
sendAudioChatId of
SomeChatId (ChatId Integer
chat_id) -> String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show Integer
chat_id
SomeChatUsername Text
txt -> Text
txt
] forall a. Semigroup a => a -> a -> a
<> forall a. [Maybe a] -> [a]
catMaybes
[ Maybe MessageThreadId
sendAudioMessageThreadId 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 forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show MessageThreadId
t)
, Maybe Text
sendAudioCaption 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 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Text -> Text
TL.replace Text
"\"" Text
"" forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText ParseMode
t)
, Maybe [MessageEntity]
sendAudioCaptionEntities 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)
, Maybe Int
sendAudioDuration forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Int
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 Int
t)
, Maybe Text
sendAudioPerformer 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 forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Text
t -> Text -> Text -> Input
Input Text
"title" (Text -> Text
TL.toStrict forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText Text
t)
, Maybe Bool
sendAudioDisableNotification forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Bool
t -> Text -> Text -> Input
Input Text
"disable_notification" (forall a. a -> a -> Bool -> a
bool Text
"false" Text
"true" Bool
t)
, Maybe Bool
sendAudioProtectContent forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Bool
t -> Text -> Text -> Input
Input Text
"protect_content" (forall a. a -> a -> Bool -> a
bool Text
"false" Text
"true" Bool
t)
, Maybe MessageId
sendAudioReplyToMessageId 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 forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Text
encodeToLazyText MessageId
t)
, Maybe Bool
sendAudioAllowSendingWithoutReply forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
\Bool
t -> Text -> Text -> Input
Input Text
"allow_sending_without_reply" (forall a. a -> a -> Bool -> a
bool Text
"false" Text
"true" Bool
t)
, Maybe InlineKeyboardMarkup
sendAudioReplyMarkup 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 forall a b. (a -> b) -> a -> b
$ 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
sendAudioThumb SendAudioRequest
r) of
(InputFile{}, Maybe InputFile
_) -> do
ByteString
boundary <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ByteString
genBoundary
forall api.
HasClient ClientM api =>
Proxy api -> Client ClientM api
client (forall {k} (t :: k). Proxy t
Proxy @SendAudioContent) (ByteString
boundary, SendAudioRequest
r)
(InputFile
_, Just InputFile{}) -> do
ByteString
boundary <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ByteString
genBoundary
forall api.
HasClient ClientM api =>
Proxy api -> Client ClientM api
client (forall {k} (t :: k). Proxy t
Proxy @SendAudioContent) (ByteString
boundary, SendAudioRequest
r)
(InputFile, Maybe InputFile)
_ -> forall api.
HasClient ClientM api =>
Proxy api -> Client ClientM api
client (forall {k} (t :: k). Proxy t
Proxy @SendAudioLink) SendAudioRequest
r