summaryrefslogtreecommitdiff
path: root/Network/API/Telegram/Bot/Object/Update/Message/Keyboard/Button.hs
blob: b6b829508da6d9eab1cc6f16a7a951016fe70a74 (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
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"