summaryrefslogtreecommitdiff
path: root/Network/API/Telegram/Bot/Object/Update/Message/Origin.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Network/API/Telegram/Bot/Object/Update/Message/Origin.hs')
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Message/Origin.hs31
1 files changed, 31 insertions, 0 deletions
diff --git a/Network/API/Telegram/Bot/Object/Update/Message/Origin.hs b/Network/API/Telegram/Bot/Object/Update/Message/Origin.hs
new file mode 100644
index 0000000..e1201c6
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Message/Origin.hs
@@ -0,0 +1,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"