module Web.Telegram.API.Bot.Requests
(
SendMessageRequest (..)
, ForwardMessageRequest (..)
, FileUpload (..)
, FileUploadContent (..)
, SendPhotoRequest (..)
, SendAudioRequest (..)
, SendDocumentRequest (..)
, SendStickerRequest (..)
, SendVideoRequest (..)
, SendVoiceRequest (..)
, SendLocationRequest (..)
, SendVenueRequest (..)
, SendContactRequest (..)
, SendChatActionRequest (..)
, ChatAction (..)
, AnswerInlineQueryRequest (..)
, AnswerCallbackQueryRequest (..)
, ReplyKeyboard (..)
, EditMessageTextRequest (..)
, EditMessageCaptionRequest (..)
, EditMessageReplyMarkupRequest (..)
, sendMessageRequest
, forwardMessageRequest
, sendPhotoRequest
, uploadPhotoRequest
, sendAudioRequest
, sendDocumentRequest
, sendStickerRequest
, sendVideoRequest
, sendVoiceRequest
, sendLocationRequest
, sendVenueRequest
, sendContactRequest
, sendChatActionRequest
, answerInlineQueryRequest
, answerCallbackQueryRequest
, replyKeyboardMarkup
, replyKeyboardHide
, forceReply
, editMessageTextRequest
, editMessageCaptionRequest
, editMessageReplyMarkupRequest
) where
import Data.Aeson
import Data.Aeson.Types
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import Data.Maybe
import Data.Proxy
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import GHC.Generics
import GHC.TypeLits
import Network.HTTP.Client.MultipartFormData
import Network.HTTP.Types.Header (hContentType)
import Network.Mime
import Servant.Client.MultipartFormData (ToMultipartFormData (..))
import Web.Telegram.API.Bot.JsonExt
import Web.Telegram.API.Bot.Data
data FileUploadContent =
FileUploadFile FilePath
| FileUploadBS BS.ByteString
| FileUploadLBS LBS.ByteString
data FileUpload = FileUpload
{
fileUpload_type :: MimeType
, fileUpload_content :: FileUploadContent
}
fileUploadToPart :: Text -> FileUpload -> Part
fileUploadToPart inputName fileUpload =
let part =
case fileUpload_content fileUpload of
FileUploadFile path -> partFileSource inputName path
FileUploadBS bs -> partBS inputName bs
FileUploadLBS lbs -> partLBS inputName lbs
in part { partContentType = Just (fileUpload_type fileUpload) }
utf8Part :: Text -> Text -> Part
utf8Part inputName = partBS inputName . T.encodeUtf8
data SendMessageRequest = SendMessageRequest
{
message_chat_id :: Text
, message_text :: Text
, message_parse_mode :: Maybe ParseMode
, message_disable_web_page_preview :: Maybe Bool
, message_disable_notification :: Maybe Bool
, message_reply_to_message_id :: Maybe Int
, message_reply_markup :: Maybe ReplyKeyboard
} deriving (Show, Generic)
instance ToJSON SendMessageRequest where
toJSON = toJsonDrop 8
instance FromJSON SendMessageRequest where
parseJSON = parseJsonDrop 8
sendMessageRequest :: Text -> Text -> SendMessageRequest
sendMessageRequest chatId text = SendMessageRequest chatId text Nothing Nothing Nothing Nothing Nothing
data ForwardMessageRequest = ForwardMessageRequest
{
forward_chat_id :: Text
, forward_from_chat_id :: Text
, forward_disable_notification :: Maybe Bool
, forward_message_id :: Int
} deriving (Show, Generic)
instance ToJSON ForwardMessageRequest where
toJSON = toJsonDrop 8
instance FromJSON ForwardMessageRequest where
parseJSON = parseJsonDrop 8
forwardMessageRequest :: Text -> Text -> Int -> ForwardMessageRequest
forwardMessageRequest chatId fromChatId forwardMessageId = ForwardMessageRequest chatId fromChatId Nothing forwardMessageId
data SendPhotoRequest payload = SendPhotoRequest
{
photo_chat_id :: Text
, photo_photo :: payload
, photo_caption :: Maybe Text
, photo_disable_notification :: Maybe Bool
, photo_reply_to_message_id :: Maybe Int
, photo_reply_markup :: Maybe ReplyKeyboard
} deriving (Show, Generic)
instance ToJSON (SendPhotoRequest Text) where
toJSON = toJsonDrop 6
instance FromJSON (SendPhotoRequest Text) where
parseJSON = parseJsonDrop 6
sendPhotoRequest :: Text -> Text -> SendPhotoRequest Text
sendPhotoRequest chatId photo = SendPhotoRequest chatId photo Nothing Nothing Nothing Nothing
uploadPhotoRequest :: Text -> FileUpload -> SendPhotoRequest FileUpload
uploadPhotoRequest chatId photo = SendPhotoRequest chatId photo Nothing Nothing Nothing Nothing
instance ToMultipartFormData (SendPhotoRequest FileUpload) where
toMultipartFormData sendPhotoReq =
[ utf8Part "chat_id" (photo_chat_id sendPhotoReq) ] ++
catMaybes
[ utf8Part "caption" <$> photo_caption sendPhotoReq
, utf8Part "reply_to_message_id" . T.pack . show <$> photo_reply_to_message_id sendPhotoReq
, partLBS "reply_markup" . encode <$> photo_reply_markup sendPhotoReq
] ++
[ fileUploadToPart "photo" (photo_photo sendPhotoReq) ]
data SendAudioRequest = SendAudioRequest
{
_audio_chat_id :: Text
, _audio_audio :: Text
, _audio_duration :: Maybe Int
, _audio_performer :: Maybe Text
, _audio_title :: Maybe Text
, _audio_disable_notification :: Maybe Bool
, _audio_reply_to_message_id :: Maybe Int
, _audio_reply_markup :: Maybe ReplyKeyboard
} deriving (Show, Generic)
instance ToJSON SendAudioRequest where
toJSON = toJsonDrop 7
instance FromJSON SendAudioRequest where
parseJSON = parseJsonDrop 7
sendAudioRequest :: Text -> Text -> SendAudioRequest
sendAudioRequest chatId audio = SendAudioRequest chatId audio Nothing Nothing Nothing Nothing Nothing Nothing
data SendStickerRequest = SendStickerRequest
{
sticker_chat_id :: Text
, sticker_sticker :: Text
, sticker_disable_notification :: Maybe Bool
, sticker_reply_to_message_id :: Maybe Int
, sticker_reply_markup :: Maybe ReplyKeyboard
} deriving (Show, Generic)
instance ToJSON SendStickerRequest where
toJSON = toJsonDrop 8
instance FromJSON SendStickerRequest where
parseJSON = parseJsonDrop 8
sendStickerRequest :: Text -> Text -> SendStickerRequest
sendStickerRequest chatId sticker = SendStickerRequest chatId sticker Nothing Nothing Nothing
data SendDocumentRequest = SendDocumentRequest
{
document_chat_id :: Text
, document_document :: Text
, document_caption :: Maybe Text
, document_disable_notification :: Maybe Bool
, document_reply_to_message_id :: Maybe Int
, document_reply_markup :: Maybe ReplyKeyboard
} deriving (Show, Generic)
instance ToJSON SendDocumentRequest where
toJSON = toJsonDrop 9
instance FromJSON SendDocumentRequest where
parseJSON = parseJsonDrop 9
sendDocumentRequest :: Text -> Text -> SendDocumentRequest
sendDocumentRequest chatId document = SendDocumentRequest chatId document Nothing Nothing Nothing Nothing
data SendVideoRequest = SendVideoRequest
{
_video_chat_id :: Text
, _video_video :: Text
, _video_duration :: Maybe Int
, _video_caption :: Maybe Text
, _video_disable_notification :: Maybe Bool
, _video_reply_to_message_id :: Maybe Int
, _video_reply_markup :: Maybe ReplyKeyboard
} deriving (Show, Generic)
instance ToJSON SendVideoRequest where
toJSON = toJsonDrop 7
instance FromJSON SendVideoRequest where
parseJSON = parseJsonDrop 7
sendVideoRequest :: Text -> Text -> SendVideoRequest
sendVideoRequest chatId video = SendVideoRequest chatId video Nothing Nothing Nothing Nothing Nothing
data SendVoiceRequest = SendVoiceRequest
{
_voice_chat_id :: Text
, _voice_voice :: Text
, _voice_duration :: Maybe Int
, _voice_disable_notification :: Maybe Bool
, _voice_reply_to_message_id :: Maybe Int
, _voice_reply_markup :: Maybe ReplyKeyboard
} deriving (Show, Generic)
instance ToJSON SendVoiceRequest where
toJSON = toJsonDrop 7
instance FromJSON SendVoiceRequest where
parseJSON = parseJsonDrop 7
sendVoiceRequest :: Text -> Text -> SendVoiceRequest
sendVoiceRequest chatId voice = SendVoiceRequest chatId voice Nothing Nothing Nothing Nothing
data SendLocationRequest = SendLocationRequest
{
location_chat_id :: Text
, location_latitude :: Float
, location_longitude :: Float
, location_disable_notification :: Maybe Bool
, location_reply_to_message_id :: Maybe Int
, location_reply_markup :: Maybe ReplyKeyboard
} deriving (Show, Generic)
instance ToJSON SendLocationRequest where
toJSON = toJsonDrop 9
instance FromJSON SendLocationRequest where
parseJSON = parseJsonDrop 9
sendLocationRequest :: Text -> Float -> Float -> SendLocationRequest
sendLocationRequest chatId latitude longitude = SendLocationRequest chatId latitude longitude Nothing Nothing Nothing
data SendVenueRequest = SendVenueRequest
{
_venue_chat_id :: Text
, _venue_latitude :: Float
, _venue_longitude :: Float
, _venue_title :: Text
, _venue_address :: Text
, _venue_foursquare_id :: Maybe Text
, _venue_disable_notification :: Maybe Bool
, _venue_reply_to_message_id :: Maybe Int
, _venue_reply_markup :: Maybe ReplyKeyboard
} deriving (Show, Generic)
instance ToJSON SendVenueRequest where
toJSON = toJsonDrop 7
instance FromJSON SendVenueRequest where
parseJSON = parseJsonDrop 7
sendVenueRequest :: Text -> Float -> Float -> Text -> Text -> SendVenueRequest
sendVenueRequest chatId latitude longitude title address = SendVenueRequest chatId latitude longitude title address Nothing Nothing Nothing Nothing
data SendContactRequest = SendContactRequest
{
_contact_chat_id :: Text
, _contact_phone_number :: Text
, _contact_first_name :: Text
, _contact_last_name :: Maybe Text
, _contact_disable_notification :: Maybe Bool
, _contact_reply_to_message_id :: Maybe Int
, _contact_reply_markup :: Maybe ReplyKeyboard
} deriving (Show, Generic)
instance ToJSON SendContactRequest where
toJSON = toJsonDrop 9
instance FromJSON SendContactRequest where
parseJSON = parseJsonDrop 9
sendContactRequest :: Text -> Text -> Text -> SendContactRequest
sendContactRequest chatId phoneNumber firstName = SendContactRequest chatId phoneNumber firstName Nothing Nothing Nothing Nothing
data ChatAction = Typing
| UploadPhoto
| RecordVideo
| UploadVideo
| RecordAudio
| UploadAudio
| UploadDocument
| FindLocation deriving (Show, Generic)
instance ToJSON ChatAction where
toJSON Typing = "typing"
toJSON UploadPhoto = "upload_photo"
toJSON RecordVideo = "record_video"
toJSON UploadVideo = "upload_video"
toJSON RecordAudio = "record_audio"
toJSON UploadAudio = "upload_audio"
toJSON UploadDocument = "upload_document"
toJSON FindLocation = "find_location"
instance FromJSON ChatAction where
parseJSON "typing" = pure Typing
parseJSON "upload_photo" = pure UploadPhoto
parseJSON "record_video" = pure RecordVideo
parseJSON "upload_video" = pure UploadVideo
parseJSON "record_audio" = pure RecordAudio
parseJSON "upload_audio" = pure UploadAudio
parseJSON "upload_document" = pure UploadDocument
parseJSON "find_location" = pure FindLocation
parseJSON _ = fail "Failed to parse ChatAction"
data SendChatActionRequest = SendChatActionRequest
{
action_chat_id :: Text
, action_action :: ChatAction
} deriving (Show, Generic)
instance ToJSON SendChatActionRequest where
toJSON = toJsonDrop 7
instance FromJSON SendChatActionRequest where
parseJSON = parseJsonDrop 7
sendChatActionRequest :: Text -> ChatAction -> SendChatActionRequest
sendChatActionRequest chatId action = SendChatActionRequest chatId action
data AnswerInlineQueryRequest = AnswerInlineQueryRequest
{
query_inline_query_id :: Text
, query_results :: [InlineQueryResult]
, query_cache_time :: Maybe Int
, query_is_personal :: Maybe Bool
, query_next_offset :: Maybe Text
, query_switch_pm_text :: Maybe Text
, query_switch_pm_parameter :: Maybe Text
} deriving (Show, Generic)
instance ToJSON AnswerInlineQueryRequest where
toJSON = toJsonDrop 6
instance FromJSON AnswerInlineQueryRequest where
parseJSON = parseJsonDrop 6
answerInlineQueryRequest :: Text -> [InlineQueryResult] -> AnswerInlineQueryRequest
answerInlineQueryRequest queryId results = AnswerInlineQueryRequest queryId results Nothing Nothing Nothing Nothing Nothing
data AnswerCallbackQueryRequest = AnswerCallbackQueryRequest
{
cq_callback_query_id :: Text
, cq_text :: Maybe Text
, cq_show_alert :: Maybe Bool
} deriving (Show, Generic)
instance ToJSON AnswerCallbackQueryRequest where
toJSON = toJsonDrop 3
instance FromJSON AnswerCallbackQueryRequest where
parseJSON = parseJsonDrop 3
answerCallbackQueryRequest :: Text -> AnswerCallbackQueryRequest
answerCallbackQueryRequest chatId = AnswerCallbackQueryRequest chatId Nothing Nothing
data ReplyKeyboard =
ReplyKeyboardMarkup
{
reply_keyboard :: [[KeyboardButton]]
, reply_resize_keyboard :: Maybe Bool
, reply_one_time_keyboard :: Maybe Bool
, reply_selective :: Maybe Bool
}
| ReplyKeyboardHide
{
reply_hide_keyboard :: Bool
, reply_selective :: Maybe Bool
}
| ForceReply
{
reply_force_reply :: Bool
, reply_selective :: Maybe Bool
} deriving (Show, Generic)
instance ToJSON ReplyKeyboard where
toJSON = toJsonDrop 6
instance FromJSON ReplyKeyboard where
parseJSON = parseJsonDrop 6
replyKeyboardMarkup :: [[KeyboardButton]] -> ReplyKeyboard
replyKeyboardMarkup keyboard = ReplyKeyboardMarkup keyboard Nothing Nothing Nothing
replyKeyboardHide :: ReplyKeyboard
replyKeyboardHide = ReplyKeyboardHide True Nothing
forceReply :: ReplyKeyboard
forceReply = ForceReply True Nothing
data EditMessageTextRequest = EditMessageTextRequest
{
emt_chat_id :: Maybe Text
, emt_message_id :: Maybe Int
, emt_inline_message_id :: Maybe Text
, emt_text :: Text
, emt_parse_mode :: Maybe ParseMode
, emt_disable_web_page_preview :: Maybe Bool
, emt_reply_markup :: Maybe InlineKeyboardMarkup
} deriving (Show, Generic)
instance ToJSON EditMessageTextRequest where
toJSON = toJsonDrop 4
instance FromJSON EditMessageTextRequest where
parseJSON = parseJsonDrop 4
editMessageTextRequest :: Text -> Int -> Text -> EditMessageTextRequest
editMessageTextRequest chatId messageId text = EditMessageTextRequest (Just chatId) (Just messageId) Nothing text Nothing Nothing Nothing
editInlineMessageTextRequest :: Text -> Text -> EditMessageTextRequest
editInlineMessageTextRequest inlineMessageId text = EditMessageTextRequest Nothing Nothing (Just inlineMessageId) text Nothing Nothing Nothing
data EditMessageCaptionRequest = EditMessageCaptionRequest
{
emc_chat_id :: Maybe Text
, emc_message_id :: Maybe Int
, emc_inline_message_id :: Maybe Text
, emc_caption :: Maybe Text
, emc_reply_markup :: Maybe InlineKeyboardMarkup
} deriving (Show, Generic)
instance ToJSON EditMessageCaptionRequest where
toJSON = toJsonDrop 4
instance FromJSON EditMessageCaptionRequest where
parseJSON = parseJsonDrop 4
editMessageCaptionRequest :: Text -> Int -> Maybe Text -> EditMessageCaptionRequest
editMessageCaptionRequest chatId messageId caption = EditMessageCaptionRequest (Just chatId) (Just messageId) Nothing caption Nothing
editInlineMessageCaptionRequest :: Text -> Maybe Text -> EditMessageCaptionRequest
editInlineMessageCaptionRequest inlineMessageId caption = EditMessageCaptionRequest Nothing Nothing (Just inlineMessageId) caption Nothing
data EditMessageReplyMarkupRequest = EditMessageReplyMarkupRequest
{
emrm_chat_id :: Maybe Text
, emrm_message_id :: Maybe Int
, emrm_inline_message_id :: Maybe Text
, emrm_reply_markup :: Maybe InlineKeyboardMarkup
} deriving (Show, Generic)
instance ToJSON EditMessageReplyMarkupRequest where
toJSON = toJsonDrop 5
instance FromJSON EditMessageReplyMarkupRequest where
parseJSON = parseJsonDrop 5
editMessageReplyMarkupRequest :: Text -> Int -> Maybe InlineKeyboardMarkup -> EditMessageReplyMarkupRequest
editMessageReplyMarkupRequest chatId messageId keyboard = EditMessageReplyMarkupRequest (Just chatId) (Just messageId) Nothing keyboard
editInlineMessageReplyMarkupRequest :: Text -> Maybe InlineKeyboardMarkup -> EditMessageReplyMarkupRequest
editInlineMessageReplyMarkupRequest inlineMessageId keyboard = EditMessageReplyMarkupRequest Nothing Nothing (Just inlineMessageId) keyboard