summaryrefslogtreecommitdiff
path: root/Network/API/Telegram/Bot/Object/Sender.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Network/API/Telegram/Bot/Object/Sender.hs')
-rw-r--r--Network/API/Telegram/Bot/Object/Sender.hs50
1 files changed, 50 insertions, 0 deletions
diff --git a/Network/API/Telegram/Bot/Object/Sender.hs b/Network/API/Telegram/Bot/Object/Sender.hs
new file mode 100644
index 0000000..1a27f05
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Sender.hs
@@ -0,0 +1,50 @@
+module Network.API.Telegram.Bot.Object.Sender (Sender (..), nickname, firstname, lastname) where
+
+import "aeson" Data.Aeson (FromJSON (parseJSON), Object, withObject, (.:), (.:?))
+import "aeson" Data.Aeson.Types (Parser)
+import "base" Control.Applicative (Applicative ((<*>)))
+import "base" Control.Monad (Monad ((>>=)))
+import "base" Data.Bool (Bool (False), bool)
+import "base" Data.Eq (Eq ((==)))
+import "base" Data.Int (Int)
+import "base" Data.Function (($))
+import "base" Data.Functor ((<$>))
+import "base" Data.Maybe (Maybe)
+import "base" Text.Show (Show)
+import "lens" Control.Lens (Lens')
+import "text" Data.Text (Text)
+
+data Sender
+ = Bot Int (Maybe Text) Text (Maybe Text) (Maybe Text)
+ | User Int (Maybe Text) Text (Maybe Text) (Maybe Text)
+ deriving Show
+
+instance Eq Sender where
+ Bot i _ _ _ _ == Bot i' _ _ _ _ = i == i'
+ User i _ _ _ _ == User i' _ _ _ _ = i == i'
+ _ == _ = False
+
+type Whom = Int -> Maybe Text -> Text -> Maybe Text -> Maybe Text -> Sender
+
+instance FromJSON Sender where
+ parseJSON = withObject "Sender" $ \v -> v .: "is_bot"
+ >>= bool (sender v User) (sender v Bot) where
+
+ sender :: Object -> Whom -> Parser Sender
+ sender v f = f <$> v .: "id"
+ <*> v .:? "username"
+ <*> v .: "first_name"
+ <*> v .:? "last_name"
+ <*> v .:? "language_code"
+
+nickname :: Lens' Sender (Maybe Text)
+nickname f (Bot uid nn fn ln lng) = (\nn' -> Bot uid nn' fn ln lng) <$> f nn
+nickname f (User uid nn fn ln lng) = (\nn' -> User uid nn' fn ln lng) <$> f nn
+
+firstname :: Lens' Sender Text
+firstname f (Bot uid nn fn ln lng) = (\fn' -> Bot uid nn fn' ln lng) <$> f fn
+firstname f (User uid nn fn ln lng) = (\fn' -> User uid nn fn' ln lng) <$> f fn
+
+lastname :: Lens' Sender (Maybe Text)
+lastname f (Bot uid nn fn ln lng) = (\ln' -> Bot uid nn fn ln' lng) <$> f ln
+lastname f (User uid nn fn ln lng) = (\ln' -> User uid nn fn ln' lng) <$> f ln