summaryrefslogtreecommitdiff
path: root/Network/API/Telegram/Bot/Object/Update/Message/Content/Info.hs
blob: c3737889fe1e054ddd79737d852917949dc6ec42 (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
module Network.API.Telegram.Bot.Object.Update.Message.Content.Info (Info (..)) where

import "aeson" Data.Aeson (FromJSON (parseJSON), withObject, (.:), (.:?))
import "aeson" Data.Aeson.Types (Object, Parser, Value)
import "base" Control.Applicative ((<*>), (<|>))
import "base" Control.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.Location (Location)

data Info = Point Location
	| Contact Text (Maybe Text) Text (Maybe Text)
	| Venue Location Text Text (Maybe Text) (Maybe Text)
	deriving Show

instance FromJSON Info where
	parseJSON = withObject "Info" $ \v -> contact v <|> venue v <|> point v where

		contact :: Object -> Parser Info
		contact v = v .: "contact" >>= info where

			info :: Value -> Parser Info
			info = withObject "Contact" $ \i -> Contact
				<$> i .: "first_name" <*> i .:? "last_name"
				<*> i .: "phone_number" <*> i .:? "vcard"

		venue :: Object -> Parser Info
		venue v = v .: "venue" >>= info where

			info :: Value -> Parser Info
			info = withObject "Venue" $ \i -> Venue
				<$> i .: "location" <*> i .: "title" <*> i .: "address"
				<*> i .:? "foursquare_id" <*> i .:? "foursquare_type"

		point :: Object -> Parser Info
		point v = Point <$> v .: "location"