telegram-bot-simple 0.13 → 0.14.4
raw patch · 10 files changed
Files
- CHANGELOG.md +30/−2
- README.md +4/−0
- examples/EchoBot.hs +2/−2
- examples/EchoBotWebhook.hs +2/−2
- examples/GameBot.hs +5/−2
- src/Telegram/Bot/Simple/BotApp/Internal.hs +22/−5
- src/Telegram/Bot/Simple/Eff.hs +3/−0
- src/Telegram/Bot/Simple/Reply.hs +3/−1
- src/Telegram/Bot/Simple/UpdateParser.hs +9/−33
- telegram-bot-simple.cabal +6/−6
CHANGELOG.md view
@@ -1,8 +1,36 @@+0.14.4 -- 2024-11-07+---++- Print failures from telegram-bot-api thrown in bot action handlers (see [#197](https://github.com/fizruk/telegram-bot-simple/pull/197)).+- Support `telegram-bot-api-.7.4.3`.++0.14.3 -- 2024-10-20+---++- Support `telegram-bot-api-7.4.1`.++0.14.2 -- 2024-08-26+---++- Fix `ExceptionInLinkedThread` (see [#189](https://github.com/fizruk/telegram-bot-simple/pull/189)).++0.14.1 -- 2024-05-26+---++- Support `telegram-bot-api == 7.3.1`.++0.14 -- 2024-05-25+---++- Drop support GHC 8.10, 9.0, add support GHC 9.8, 9.10 (see [#182](https://github.com/fizruk/telegram-bot-simple/pull/182)).+- Support `telegram-bot-api == 7.3`.+- Ignore updates that produces parsing errors (see [#175](https://github.com/fizruk/telegram-bot-simple/pull/175)).+ 0.13 -- 2024-02-06 --- - Support GHC 9.6 (see [#163](https://github.com/fizruk/telegram-bot-simple/pull/163)).-- Support `telegram-bot-api == 0.7`.+- Support `telegram-bot-api == 7.0`. - Fix bot hanging via rethrowing exceptions (see [#170](https://github.com/fizruk/telegram-bot-simple/pull/170)). @@ -12,7 +40,7 @@ - Add support for testing WebApps (see [#148](https://github.com/fizruk/telegram-bot-simple/pull/148)); - Add nix flake (see [#149](https://github.com/fizruk/telegram-bot-simple/pull/149)); - Improve documentation for `callbackQueryDataRead` (see [#153](https://github.com/fizruk/telegram-bot-simple/pull/153));-- Support `telegram-bot-api == 0.6.7` (breaking changes included).+- Support `telegram-bot-api == 6.7` (breaking changes included). - **Migration guide**:
README.md view
@@ -31,5 +31,9 @@ | 0.11.1 | 6.5.1 | | 0.12 | 6.7 | | 0.13 | 7.0 | +| 0.14 | 7.3 | +| 0.14.2 | 7.4 |+| 0.14.3 | 7.4.2 |+| 0.14.4 | 7.4.3 | _Nick_
examples/EchoBot.hs view
@@ -35,8 +35,8 @@ Just $ InlineEcho queryId msg | isJust $ updateMessageSticker update = do fileId <- stickerFileId <$> updateMessageSticker update- chatId <- updateChatId update- pure $ StickerEcho (InputFileId fileId) chatId+ chatOfUser <- updateChatId update+ pure $ StickerEcho (InputFileId fileId) chatOfUser | otherwise = case updateMessageText update of Just text -> Just (Echo text) Nothing -> Nothing
examples/EchoBotWebhook.hs view
@@ -43,8 +43,8 @@ Just $ InlineEcho queryId msg | isJust $ updateMessageSticker update = do fileId <- stickerFileId <$> updateMessageSticker update- chatId <- updateChatId update- pure $ StickerEcho (InputFileId fileId) chatId+ chatOfUser <- updateChatId update+ pure $ StickerEcho (InputFileId fileId) chatOfUser | otherwise = case updateMessageText update of Just text -> Just (Echo text) Nothing -> Nothing
examples/GameBot.hs view
@@ -104,7 +104,11 @@ { inlineQueryResultTitle = Just msg , inlineQueryResultInputMessageContent = Just gameMsg }- gameMsg = (defaultInputTextMessageContent gameMessageText) { inputMessageContentParseMode = Just "HTML" }+ gameMsg = InputTextMessageContent+ { inputMessageContentMessageText = gameMessageText+ , inputMessageContentParseMode = Just "HTML"+ , inputMessageContentDisableWebPagePrefiew = Nothing+ } inlineQueryResult = InlineQueryResultGame { inlineQueryResultGameGeneric = genericResult , inlineQueryResultGameGameShortName = gameName@@ -737,4 +741,3 @@ H.div ! A.class_ "position-link" $ do H.div ! A.class_ "qel-button text-button text-again-button" $ toMarkup txt-
src/Telegram/Bot/Simple/BotApp/Internal.hs view
@@ -16,6 +16,8 @@ import qualified Telegram.Bot.API as Telegram import Telegram.Bot.Simple.Eff+import Data.Either (partitionEithers)+import Data.Aeson.Types (parseEither, parseJSON) -- | A bot application. data BotApp model action = BotApp@@ -107,7 +109,9 @@ (newModel, effects) -> do writeTVar botModelVar newModel return effects- mapM_ ((liftIO . issueAction botEnv update) <=< runBotM (BotContext botUser update)) effects+ let runBotAndIssueAction+ = (liftIO . issueAction botEnv update) <=< runBotM (BotContext botUser update)+ mapM_ runBotAndIssueAction effects -- | A job to wait for the next action and process it. processActionJob :: BotApp model action -> BotEnv model action -> ClientM ()@@ -120,7 +124,10 @@ :: BotApp model action -> BotEnv model action -> IO ThreadId processActionsIndefinitely botApp botEnv = do a <- asyncLink $ forever $ do- runClientM (processActionJob botApp botEnv) (botClientEnv botEnv)+ res <- runClientM (processActionJob botApp botEnv) (botClientEnv botEnv)+ case res of+ Left err -> print err+ Right _ -> return () return (asyncThreadId a) -- | Start 'Telegram.Update' polling for a bot.@@ -141,7 +148,7 @@ let inc (Telegram.UpdateId n) = Telegram.UpdateId (n + 1) offset = fmap inc lastUpdateId res <-- (Right <$> Telegram.getUpdates+ (Right <$> Telegram.getUpdatesAsValue (Telegram.GetUpdatesRequest offset Nothing (Just 25) Nothing)) `catchError` (pure . Left) @@ -150,19 +157,29 @@ liftIO (print servantErr) pure lastUpdateId Right result -> do- let updates = Telegram.responseResult result+ let updateValues = Telegram.responseResult result+ (errors, updates) = parseUpdates updateValues updateIds = map Telegram.updateUpdateId updates maxUpdateId = maximum (Nothing : map Just updateIds)+ mapM_ reportParseError errors mapM_ handleUpdate updates pure maxUpdateId liftIO $ threadDelay 1000000 go nextUpdateId + parseUpdates updates =+ partitionEithers (map (parseEither parseJSON) updates)++ reportParseError err =+ liftIO $ putStrLn $+ "Failed to parse an update! Please, make sure you have the latest version of `telegram-bot-api`\+ \ library and consider opening an issue if so. Error message: " <> err+ -- ** Helpers -- | Instead of 'forkIO' which hides exceptions, -- allow users to handle those exceptions separately.--- +-- -- See <https://github.com/fizruk/telegram-bot-simple/issues/159>. asyncLink :: IO a -> IO (Async a) asyncLink action = do
src/Telegram/Bot/Simple/Eff.hs view
@@ -24,6 +24,9 @@ , botContextUpdate :: Maybe Telegram.Update } +-- | To retain control over errors coming from Telegram API in `BotM`,+-- consider using `liftIO . flip runClientM clientEnv` instead.+-- Issues generated by servant-client will be logged, response failure errors will be ignored. liftClientM :: ClientM a -> BotM a liftClientM = BotM . lift
src/Telegram/Bot/Simple/Reply.hs view
@@ -60,6 +60,8 @@ replyMessageToSendMessageRequest :: SomeChatId -> ReplyMessage -> SendMessageRequest replyMessageToSendMessageRequest someChatId ReplyMessage{..} = SendMessageRequest { sendMessageChatId = someChatId+ , sendMessageBusinessConnectionId = Nothing+ , sendMessageMessageEffectId = Nothing , sendMessageMessageThreadId = replyMessageMessageThreadId , sendMessageText = replyMessageText , sendMessageParseMode = replyMessageParseMode@@ -102,7 +104,7 @@ data EditMessageId = EditChatMessageId SomeChatId MessageId- | EditInlineMessageId MessageId+ | EditInlineMessageId InlineMessageId toEditMessage :: Text -> EditMessage toEditMessage msg = EditMessage msg Nothing Nothing Nothing
src/Telegram/Bot/Simple/UpdateParser.hs view
@@ -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,22 @@ 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+type UpdateParser a = ReaderT Update Maybe a mkParser :: (Update -> Maybe a) -> UpdateParser a-mkParser = UpdateParser+mkParser f = ask >>= lift . f parseUpdate :: UpdateParser a -> Update -> Maybe a-parseUpdate = runUpdateParser+parseUpdate = runReaderT +runUpdateParser :: UpdateParser a -> Update -> Maybe a+runUpdateParser = runReaderT+ text :: UpdateParser Text-text = UpdateParser (extractUpdateMessage >=> messageText)+text = mkParser (extractUpdateMessage >=> messageText) plainText :: UpdateParser Text plainText = do@@ -77,10 +56,7 @@ -- | 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 = mkParser (updateCallbackQuery >=> callbackQueryData >=> (readMaybe . Text.unpack)) updateMessageText :: Update -> Maybe Text updateMessageText = extractUpdateMessage >=> messageText
telegram-bot-simple.cabal view
@@ -1,7 +1,7 @@ cabal-version: 1.12 name: telegram-bot-simple-version: 0.13+version: 0.14.4 synopsis: Easy to use library for building Telegram bots. description: Please see the README on Github at <https://github.com/fizruk/telegram-bot-simple#readme> category: Web@@ -70,7 +70,7 @@ , split , stm , template-haskell- , telegram-bot-api+ , telegram-bot-api >= 7.4.3 , text , time , transformers@@ -117,7 +117,7 @@ if flag(examples) build-depends: telegram-bot-simple- , telegram-bot-api+ , telegram-bot-api >= 7.3.1 else buildable: False @@ -159,7 +159,7 @@ if flag(examples) build-depends: telegram-bot-simple- , telegram-bot-api+ , telegram-bot-api >= 7.3.1 else buildable: False @@ -212,7 +212,7 @@ , servant-server , signal , telegram-bot-simple- , telegram-bot-api+ , telegram-bot-api >= 7.3.1 , uuid , warp else@@ -256,6 +256,6 @@ if flag(examples) build-depends: telegram-bot-simple- , telegram-bot-api+ , telegram-bot-api >= 7.3.1 else buildable: False