summaryrefslogtreecommitdiff
path: root/Network/API/Telegram/Bot/Object/Update
diff options
context:
space:
mode:
Diffstat (limited to 'Network/API/Telegram/Bot/Object/Update')
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Callback.hs18
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Callback/Notification.hs3
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Message.hs54
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Message/Content.hs53
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Message/Content/File.hs68
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Message/Content/Info.hs41
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Message/Content/Location.hs19
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Message/Content/Size.hs17
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Message/Keyboard.hs20
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Message/Keyboard/Button.hs35
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Message/Origin.hs31
-rw-r--r--Network/API/Telegram/Bot/Object/Update/Moving.hs30
12 files changed, 389 insertions, 0 deletions
diff --git a/Network/API/Telegram/Bot/Object/Update/Callback.hs b/Network/API/Telegram/Bot/Object/Update/Callback.hs
new file mode 100644
index 0000000..55c852b
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Callback.hs
@@ -0,0 +1,18 @@
+module Network.API.Telegram.Bot.Object.Update.Callback (Callback (..), module Exports) where
+
+import Network.API.Telegram.Bot.Object.Update.Callback.Notification as Exports
+
+import "aeson" Data.Aeson (FromJSON (parseJSON), withObject, (.:))
+import "base" Control.Applicative (Applicative ((<*>)))
+import "base" Data.Function (($))
+import "base" Data.Functor ((<$>))
+import "base" Text.Show (Show)
+import "text" Data.Text (Text)
+
+import Network.API.Telegram.Bot.Object.Update.Message (Message)
+
+data Callback = Datatext Text Message Text deriving Show
+
+instance FromJSON Callback where
+ parseJSON = withObject "Callback" $ \v ->
+ Datatext <$> v .: "id" <*> v .: "message" <*> v .: "data"
diff --git a/Network/API/Telegram/Bot/Object/Update/Callback/Notification.hs b/Network/API/Telegram/Bot/Object/Update/Callback/Notification.hs
new file mode 100644
index 0000000..2289fd6
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Callback/Notification.hs
@@ -0,0 +1,3 @@
+module Network.API.Telegram.Bot.Object.Update.Callback.Notification (Notification) where
+
+data Notification
diff --git a/Network/API/Telegram/Bot/Object/Update/Message.hs b/Network/API/Telegram/Bot/Object/Update/Message.hs
new file mode 100644
index 0000000..df28b5d
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Message.hs
@@ -0,0 +1,54 @@
+module Network.API.Telegram.Bot.Object.Update.Message (Message (..), module Exports) where
+
+import Network.API.Telegram.Bot.Object.Update.Message.Content as Exports
+import Network.API.Telegram.Bot.Object.Update.Message.Keyboard as Exports
+import Network.API.Telegram.Bot.Object.Update.Message.Origin as Exports
+
+import "aeson" Data.Aeson (FromJSON (parseJSON), Value (Object), withObject, (.:))
+import "aeson" Data.Aeson.Types (Object, Parser)
+import "base" Control.Applicative (Applicative ((<*>)), Alternative ((<|>)))
+import "base" Control.Monad (Monad ((>>=)), fail)
+import "base" Data.Function (($))
+import "base" Data.Functor ((<$>))
+import "base" Data.Int (Int)
+import "base" Text.Show (Show)
+import "text" Data.Text (Text)
+
+import Network.API.Telegram.Bot.Object.Update.Message.Content (Content)
+import Network.API.Telegram.Bot.Object.Update.Message.Origin (Origin (Private, Group, Supergroup, Channel))
+
+data Message
+ = Direct Int Origin Content
+ | Forward Int Origin Content
+ | Reply Int Origin Content Message
+ deriving Show
+
+instance FromJSON Message where
+ parseJSON = withObject "Message" $ \v ->
+ forward_channel v <|> forward_chat v <|> reply v <|> direct v where
+
+ forward_channel :: Object -> Parser Message
+ forward_channel v = Forward <$> v .: "forward_from_message_id"
+ <*> (v .: "forward_from_chat" >>= channel) <*> parseJSON (Object v) where
+
+ channel :: Value -> Parser Origin
+ channel = withObject "Channel" $ \c -> Channel <$> c .: "id" <*> c .: "title"
+
+ forward_chat :: Object -> Parser Message
+ forward_chat v = Forward <$> v .: "message_id"
+ <*> (v .: "chat" >>= chat) <*> parseJSON (Object v) where
+
+ chat :: Value -> Parser Origin
+ chat = withObject "Origin" $ \c -> c .: "type" >>= \case
+ ("private" :: Text) -> Private <$> c .: "id" <*> v .: "forward_from"
+ ("group" :: Text) -> Group <$> c .: "id" <*> c .: "title" <*> v .: "forward_from"
+ ("supergroup" :: Text) -> Supergroup <$> c .: "id" <*> c .: "title" <*> v .: "forward_from"
+ _ -> fail "Type of chat is not defined"
+
+ reply :: Object -> Parser Message
+ reply v = Reply <$> v .: "message_id" <*> parseJSON (Object v)
+ <*> parseJSON (Object v) <*> v .: "reply_to_message"
+
+ direct :: Object -> Parser Message
+ direct v = Direct <$> v .: "message_id"
+ <*> parseJSON (Object v) <*> parseJSON (Object v)
diff --git a/Network/API/Telegram/Bot/Object/Update/Message/Content.hs b/Network/API/Telegram/Bot/Object/Update/Message/Content.hs
new file mode 100644
index 0000000..cf68add
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Message/Content.hs
@@ -0,0 +1,53 @@
+module Network.API.Telegram.Bot.Object.Update.Message.Content (Content (..), module Exports) where
+
+import Network.API.Telegram.Bot.Object.Update.Message.Content.File as Exports
+import Network.API.Telegram.Bot.Object.Update.Message.Content.Info as Exports
+import Network.API.Telegram.Bot.Object.Update.Message.Content.Location as Exports
+import Network.API.Telegram.Bot.Object.Update.Message.Content.Size as Exports
+
+import "aeson" Data.Aeson (FromJSON (parseJSON), withArray, withObject, (.:), (.:?))
+import "aeson" Data.Aeson.Types (Object, Parser, Value (Object))
+import "base" Control.Applicative (Applicative ((<*>)), Alternative (empty, (<|>)))
+import "base" Control.Monad (Monad ((>>=)), fail)
+import "base" Data.Function ((.), ($))
+import "base" Data.Functor ((<$>))
+import "base" Data.Foldable (Foldable (foldr))
+import "base" Data.Int (Int)
+import "base" Data.Maybe (Maybe)
+import "base" Text.Show (Show)
+import "base" Prelude ((+))
+import "text" Data.Text (Text, drop, take)
+
+data Content
+ = Textual Text
+ | Command Text
+ | Attachment (Maybe Text) File
+ | Information Info
+ deriving Show
+
+instance FromJSON Content where
+ parseJSON = withObject "Content" $ \v -> command v <|> attachment v <|> other v <|> textual v where
+
+ command :: Object -> Parser Content
+ command v = Command <$> (v .: "entities" >>= command_entity >>= extract_command v)
+
+ command_entity :: Value -> Parser (Int, Int)
+ command_entity = withArray "Command content" $ \a ->
+ foldr ((<|>) . entity) empty a where
+
+ entity :: Value -> Parser (Int, Int)
+ entity = withObject "Command entity" $ \v -> v .: "type" >>= \case
+ ("bot_command" :: Text) -> (,) <$> v .: "offset" <*> v .: "length"
+ _ -> fail "It's not a bot command"
+
+ extract_command :: Object -> (Int, Int) -> Parser Text
+ extract_command v (ofs, len) = (take len . drop (ofs + 1)) <$> v .: "text"
+
+ attachment :: Object -> Parser Content
+ attachment v = Attachment <$> v .:? "caption" <*> parseJSON (Object v)
+
+ other :: Object -> Parser Content
+ other v = Information <$> parseJSON (Object v)
+
+ textual :: Object -> Parser Content
+ textual v = Textual <$> v .: "text"
diff --git a/Network/API/Telegram/Bot/Object/Update/Message/Content/File.hs b/Network/API/Telegram/Bot/Object/Update/Message/Content/File.hs
new file mode 100644
index 0000000..07cef6d
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Message/Content/File.hs
@@ -0,0 +1,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"
diff --git a/Network/API/Telegram/Bot/Object/Update/Message/Content/Info.hs b/Network/API/Telegram/Bot/Object/Update/Message/Content/Info.hs
new file mode 100644
index 0000000..c373788
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Message/Content/Info.hs
@@ -0,0 +1,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"
diff --git a/Network/API/Telegram/Bot/Object/Update/Message/Content/Location.hs b/Network/API/Telegram/Bot/Object/Update/Message/Content/Location.hs
new file mode 100644
index 0000000..a85ca05
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Message/Content/Location.hs
@@ -0,0 +1,19 @@
+module Network.API.Telegram.Bot.Object.Update.Message.Content.Location (Location (..)) where
+
+import "aeson" Data.Aeson (FromJSON (parseJSON), ToJSON (toJSON), object, withObject, (.:), (.=))
+import "base" Control.Applicative ((<*>))
+import "base" Data.Function (($))
+import "base" Data.Functor ((<$>))
+import "base" GHC.Float (Float)
+import "base" Text.Show (Show)
+
+data Location = Location Float Float
+ deriving Show
+
+instance FromJSON Location where
+ parseJSON = withObject "Location" $ \v -> Location
+ <$> v .: "longitude" <*> v .: "latitude"
+
+instance ToJSON Location where
+ toJSON (Location latitude longitude) = object
+ ["latitude" .= latitude, "longitude" .= longitude]
diff --git a/Network/API/Telegram/Bot/Object/Update/Message/Content/Size.hs b/Network/API/Telegram/Bot/Object/Update/Message/Content/Size.hs
new file mode 100644
index 0000000..b910a82
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Message/Content/Size.hs
@@ -0,0 +1,17 @@
+module Network.API.Telegram.Bot.Object.Update.Message.Content.Size (Size (..)) where
+
+import "aeson" Data.Aeson (FromJSON (parseJSON), withObject, (.:))
+import "base" Control.Applicative ((<*>))
+import "base" Data.Int (Int)
+import "base" Data.Function (($))
+import "base" Data.Functor ((<$>))
+import "base" Data.Maybe (Maybe)
+import "base" Text.Show (Show)
+import "text" Data.Text (Text)
+
+data Size = Size Text Int Int (Maybe Int)
+ deriving Show
+
+instance FromJSON Size where
+ parseJSON = withObject "Size" $ \v -> Size <$> v .: "file_id"
+ <*> v .: "width" <*> v .: "height" <*> v .: "file_size"
diff --git a/Network/API/Telegram/Bot/Object/Update/Message/Keyboard.hs b/Network/API/Telegram/Bot/Object/Update/Message/Keyboard.hs
new file mode 100644
index 0000000..ef15bb8
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Message/Keyboard.hs
@@ -0,0 +1,20 @@
+module Network.API.Telegram.Bot.Object.Update.Message.Keyboard (Keyboard (..), module Exports) where
+
+import Network.API.Telegram.Bot.Object.Update.Message.Keyboard.Button as Exports
+
+import "aeson" Data.Aeson (FromJSON (parseJSON), ToJSON (toJSON), object, withObject, (.:), (.=))
+import "base" Data.Function (($))
+import "base" Data.Functor ((<$>))
+import "base" Text.Show (Show)
+
+import Network.API.Telegram.Bot.Object.Update.Message.Keyboard.Button (Button)
+
+data Keyboard = Inline [[Button]] deriving Show
+
+instance FromJSON Keyboard where
+ parseJSON = withObject "Inline" $ \v ->
+ Inline <$> v .: "inline_keyboard"
+
+instance ToJSON Keyboard where
+ toJSON (Inline buttons) = object
+ ["inline_keyboard" .= buttons]
diff --git a/Network/API/Telegram/Bot/Object/Update/Message/Keyboard/Button.hs b/Network/API/Telegram/Bot/Object/Update/Message/Keyboard/Button.hs
new file mode 100644
index 0000000..b6b8295
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Message/Keyboard/Button.hs
@@ -0,0 +1,35 @@
+module Network.API.Telegram.Bot.Object.Update.Message.Keyboard.Button (Button (..), Pressed (..)) where
+
+import "aeson" Data.Aeson (FromJSON (parseJSON), ToJSON (toJSON)
+ , Object, Value (Object), object, withObject, (.:), (.:?), (.=))
+import "aeson" Data.Aeson.Types (Parser)
+import "base" Control.Applicative (Applicative (pure, (<*>)), Alternative ((<|>)))
+import "base" Control.Monad (Monad ((>>=)), fail)
+import "base" Data.Function ((.), ($))
+import "base" Data.Functor (Functor (fmap), (<$>))
+import "base" Data.Maybe (Maybe, maybe)
+import "base" Text.Show (Show)
+import "text" Data.Text (Text)
+
+data Button = Button Text Pressed deriving Show
+
+instance FromJSON Button where
+ parseJSON = withObject "Button" $ \v ->
+ Button <$> v .: "text" <*> parseJSON (Object v)
+
+instance ToJSON Button where
+ toJSON (Button text (Open url)) = object
+ ["text" .= text, "url" .= url]
+ toJSON (Button text (Callback cbd)) = object
+ ["text" .= text, "callback_data" .= cbd]
+
+data Pressed = Open Text | Callback Text deriving Show
+
+instance FromJSON Pressed where
+ parseJSON = withObject "Pressed" $ \v ->
+ (is_open v <|> is_callback v) >>= maybe
+ (fail "Action on pressing isn't set") pure where
+
+ is_open, is_callback :: Object -> Parser (Maybe Pressed)
+ is_open v = (fmap . fmap) Open $ v .:? "url"
+ is_callback v = (fmap . fmap) Callback $ v .:? "callback_data"
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"
diff --git a/Network/API/Telegram/Bot/Object/Update/Moving.hs b/Network/API/Telegram/Bot/Object/Update/Moving.hs
new file mode 100644
index 0000000..e25b9d3
--- /dev/null
+++ b/Network/API/Telegram/Bot/Object/Update/Moving.hs
@@ -0,0 +1,30 @@
+module Network.API.Telegram.Bot.Object.Update.Moving (Moving (..)) where
+
+import "aeson" Data.Aeson (FromJSON (parseJSON), withObject, (.:))
+import "aeson" Data.Aeson.Types (Object, Parser)
+import "base" Control.Applicative ((<*>), (<|>))
+import "base" Control.Monad ((>>=))
+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 Moving
+ = Gone Sender (Int64, Text)
+ | Joined [Sender] (Int64, Text)
+ deriving Show
+
+instance FromJSON Moving where
+ parseJSON = withObject "Moving" $ \v -> gone v <|> joined v where
+
+ gone :: Object -> Parser Moving
+ gone v = Gone <$> v .: "left_chat_member" <*> (v .: "chat" >>= chat)
+
+ joined :: Object -> Parser Moving
+ joined v = Joined <$> v .: "new_chat_members" <*> (v .: "chat" >>= chat)
+
+ chat :: Object -> Parser (Int64, Text)
+ chat c = (,) <$> c .: "id" <*> c .: "title"