summaryrefslogtreecommitdiff
path: root/Network/API/Telegram/Bot/Object/Update/Message/Content/File.hs
blob: 07cef6dff946ca04bfb779ca10a41084d9f0a5a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module Network.API.Telegram.Bot.Object.Update.Message.Content.File (File (..)) where

import "aeson" Data.Aeson (FromJSON (parseJSON), withObject, (.:), (.:?))
import "aeson" Data.Aeson.Types (Object, Parser, Value)
import "base" Control.Applicative (Applicative ((<*>)), Alternative ((<|>)))
import "base" Control.Monad (Monad ((>>=)))
import "base" Data.Function (($))
import "base" Data.Functor ((<$>))
import "base" Data.Int (Int)
import "base" Data.Maybe (Maybe)
import "base" Text.Show (Show)
import "text" Data.Text (Text)

import Network.API.Telegram.Bot.Object.Update.Message.Content.Size (Size)

data File
	= Animation Text Int Int Int (Maybe Size) (Maybe Text) (Maybe Text) (Maybe Int)
	| Audio Text Int (Maybe Text) (Maybe Text) (Maybe Text) (Maybe Int) (Maybe Size)
	| Document Text (Maybe Size) (Maybe Text) (Maybe Text) (Maybe Int)
	| Video Text Int Int Int (Maybe Size) (Maybe Text) (Maybe Int)
	| Voice Text Int (Maybe Text) (Maybe Int)
	| Photo [Size]
	deriving Show

instance FromJSON File where
	parseJSON = withObject "Message" $ \v -> photo v <|> document v
		<|> audio v <|> video v <|> animation v <|> voice v where

		animation :: Object -> Parser File
		animation v = v .: "animation" >>= file where

			file :: Value -> Parser File
			file = withObject "Animation" $ \f -> Animation <$> f .: "file_id"
				<*> f .: "width" <*> f .: "height" <*> f .: "duration" <*> f .:? "thumb"
				<*> f .:? "file_name" <*> f .:? "mime_type" <*> f .:? "file_size"

		audio :: Object -> Parser File
		audio v = v .: "audio" >>= file where

			file :: Value -> Parser File
			file = withObject "Audio" $ \f -> Audio <$> f .: "file_id"
				<*> f .: "duration" <*> f .:? "performer" <*> f .:? "title"
				<*> f .:? "mime_type" <*> f .:? "file_size" <*> f .:? "thumb"

		document :: Object -> Parser File
		document v = v .: "document" >>= file where

			file :: Value -> Parser File
			file = withObject "Document" $ \f -> Document <$> f .: "file_id"
				<*> f .:? "thumb" <*> f .:? "file_name" <*> f .:? "mime_type" <*> f .:? "file_size"

		photo :: Object -> Parser File
		photo v = Photo <$> v .: "photo"

		video :: Object -> Parser File
		video v = v .: "video" >>= file where

			file :: Value -> Parser File
			file = withObject "Video" $ \f -> Video <$> f .: "file_id"
				<*> f .: "width" <*> f .: "height" <*> f .: "duration"
				<*> f .:?  "thumb" <*> f .:? "mime_type" <*> f .:? "file_size"

		voice :: Object -> Parser File
		voice v = v .: "voice" >>= file where

			file :: Value -> Parser File
			file = withObject "Voice" $ \f -> Voice <$> f .: "file_id"
				<*> f .: "duration" <*> f .:? "mime_type" <*> f .:? "file_size"