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

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

import Network.API.Telegram.Bot.Object.Sender (Sender)

data Origin
	= Private Int64 Sender
	| Group Int64 Text Sender
	| Supergroup Int64 Text Sender
	| Channel Int64 Text
	deriving Show

instance FromJSON Origin where
	parseJSON = withObject "Message" $ \v -> v .: "chat" >>= chat v where

		chat :: Object -> Value -> Parser Origin
		chat v = withObject "Origin" $ \c -> c .: "type" >>= \case
			("private" :: Text) -> Private <$> c .: "id" <*> v .: "from"
			("group" :: Text) -> Group <$> c .: "id" <*> c .: "title" <*> v .: "from"
			("supergroup" :: Text) -> Supergroup <$> c .: "id" <*> c .: "title" <*> v .: "from"
			("channel" :: Text) -> Channel <$> c .: "id" <*> c .: "title"
			_ -> fail "Type of chat is not defined"