From 7a6be91ee9da15d57e592cce33d71a054b1ef681 Mon Sep 17 00:00:00 2001 From: Jake Faulkner Date: Tue, 12 Mar 2024 11:47:33 +1300 Subject: [PATCH 1/3] Refactor UpdateParser to use monad transformers --- .../src/Telegram/Bot/Simple/UpdateParser.hs | 40 +++---------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs b/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs index f155eaf..0a725bd 100644 --- a/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs +++ b/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs @@ -3,7 +3,6 @@ {-# LANGUAGE CPP #-} module Telegram.Bot.Simple.UpdateParser where -import Control.Applicative import Control.Monad #if defined(MIN_VERSION_GLASGOW_HASKELL) #if MIN_VERSION_GLASGOW_HASKELL(8,6,2,0) @@ -15,42 +14,16 @@ import qualified Data.Char as Char import Data.Text (Text) import qualified Data.Text as Text import Text.Read (readMaybe) - +import Control.Monad.Reader import Telegram.Bot.API - -newtype UpdateParser a = UpdateParser - { runUpdateParser :: Update -> Maybe a - } deriving (Functor) - -instance Applicative UpdateParser where - pure x = UpdateParser (pure (pure x)) - UpdateParser f <*> UpdateParser x = UpdateParser (\u -> f u <*> x u) - -instance Alternative UpdateParser where - empty = UpdateParser (const Nothing) - UpdateParser f <|> UpdateParser g = UpdateParser (\u -> f u <|> g u) - -instance Monad UpdateParser where - return = pure - UpdateParser x >>= f = UpdateParser (\u -> x u >>= flip runUpdateParser u . f) -#if !MIN_VERSION_base(4,13,0) - fail _ = empty -#endif - -#if MIN_VERSION_base(4,13,0) -instance MonadFail UpdateParser where - fail _ = empty -#endif - -mkParser :: (Update -> Maybe a) -> UpdateParser a -mkParser = UpdateParser +type UpdateParser a = ReaderT Update Maybe a parseUpdate :: UpdateParser a -> Update -> Maybe a -parseUpdate = runUpdateParser +parseUpdate = runReaderT text :: UpdateParser Text -text = UpdateParser (extractUpdateMessage >=> messageText) +text = ask >>= (lift . (extractUpdateMessage >=> messageText)) plainText :: UpdateParser Text plainText = do @@ -77,10 +50,7 @@ commandWithBotName botname commandname = do -- | Obtain 'CallbackQuery' @data@ associated with the callback button in an inline keyboard if present in 'Update' message. callbackQueryDataRead :: Read a => UpdateParser a -callbackQueryDataRead = mkParser $ \update -> do - query <- updateCallbackQuery update - data_ <- callbackQueryData query - readMaybe (Text.unpack data_) +callbackQueryDataRead = ask >>= (lift . (updateCallbackQuery >=> callbackQueryData >=> (readMaybe . Text.unpack))) updateMessageText :: Update -> Maybe Text updateMessageText = extractUpdateMessage >=> messageText From 9c2685544cf97a4d7c3f05c397b9bb7a081c0da8 Mon Sep 17 00:00:00 2001 From: Jake Faulkner Date: Tue, 12 Mar 2024 11:53:56 +1300 Subject: [PATCH 2/3] Reintroduce mkParser function --- .../src/Telegram/Bot/Simple/UpdateParser.hs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs b/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs index 0a725bd..55c4278 100644 --- a/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs +++ b/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs @@ -19,11 +19,14 @@ import Telegram.Bot.API type UpdateParser a = ReaderT Update Maybe a +mkParser :: (Update -> Maybe a) -> UpdateParser a +mkParser f = ask >>= lift . f + parseUpdate :: UpdateParser a -> Update -> Maybe a parseUpdate = runReaderT text :: UpdateParser Text -text = ask >>= (lift . (extractUpdateMessage >=> messageText)) +text = mkParser (extractUpdateMessage >=> messageText) plainText :: UpdateParser Text plainText = do @@ -50,7 +53,7 @@ commandWithBotName botname commandname = do -- | Obtain 'CallbackQuery' @data@ associated with the callback button in an inline keyboard if present in 'Update' message. callbackQueryDataRead :: Read a => UpdateParser a -callbackQueryDataRead = ask >>= (lift . (updateCallbackQuery >=> callbackQueryData >=> (readMaybe . Text.unpack))) +callbackQueryDataRead = mkParser (updateCallbackQuery >=> callbackQueryData >=> (readMaybe . Text.unpack)) updateMessageText :: Update -> Maybe Text updateMessageText = extractUpdateMessage >=> messageText From 3ee4dbd578c1801ab2fab9e1282b13ca0d2b78c4 Mon Sep 17 00:00:00 2001 From: Jake Faulkner Date: Tue, 12 Mar 2024 11:57:29 +1300 Subject: [PATCH 3/3] add runUpdateParser for compatibility --- telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs b/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs index 55c4278..c6f2390 100644 --- a/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs +++ b/telegram-bot-simple/src/Telegram/Bot/Simple/UpdateParser.hs @@ -25,6 +25,9 @@ mkParser f = ask >>= lift . f parseUpdate :: UpdateParser a -> Update -> Maybe a parseUpdate = runReaderT +runUpdateParser :: UpdateParser a -> Update -> Maybe a +runUpdateParser = runReaderT + text :: UpdateParser Text text = mkParser (extractUpdateMessage >=> messageText)