telegram-bot-simple 0.12 → 0.13
raw patch · 8 files changed
+43/−19 lines, 8 filesdep +async
Dependencies added: async
Files
- CHANGELOG.md +9/−1
- README.md +2/−1
- src/Telegram/Bot/Simple/BotApp.hs +1/−2
- src/Telegram/Bot/Simple/BotApp/Internal.hs +19/−4
- src/Telegram/Bot/Simple/Reply.hs +8/−7
- src/Telegram/Bot/Simple/UpdateParser.hs +1/−1
- src/Telegram/Bot/Simple/Webhook.hs +1/−2
- telegram-bot-simple.cabal +2/−1
CHANGELOG.md view
@@ -1,10 +1,18 @@+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`.+- Fix bot hanging via rethrowing exceptions (see [#170](https://github.com/fizruk/telegram-bot-simple/pull/170)).++ 0.12 -- 2023-04-29 --- - 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.7` (breaking changes included).+- Support `telegram-bot-api == 0.6.7` (breaking changes included). - **Migration guide**:
README.md view
@@ -13,7 +13,7 @@ ## Examples -See bot examples here: https://github.com/fizruk/telegram-bot-simple/tree/master/examples+See bot examples here: https://github.com/fizruk/telegram-bot-simple/tree/master/telegram-bot-simple/examples Use `cabal build all -fexamples` to build it. If you are building with stack then use `stack build --flag telegram-bot-simple:examples`.@@ -30,5 +30,6 @@ | 0.10 | 6.5 | | 0.11.1 | 6.5.1 | | 0.12 | 6.7 |+| 0.13 | 7.0 | _Nick_
src/Telegram/Bot/Simple/BotApp.hs view
@@ -16,7 +16,6 @@ getEnvToken, ) where -import Control.Concurrent (forkIO) import Control.Monad (void) import Data.String (fromString) import Servant.Client@@ -42,7 +41,7 @@ fork_ $ startBotPolling bot botEnv return (issueAction botEnv Nothing . Just) where- fork_ = void . forkIO . void . flip runClientM env+ fork_ = void . asyncLink . void . flip runClientM env -- | Like 'startBotAsync', but ignores result. startBotAsync_ :: BotApp model action -> ClientEnv -> IO ()
src/Telegram/Bot/Simple/BotApp/Internal.hs view
@@ -3,7 +3,8 @@ {-# LANGUAGE ScopedTypeVariables #-} module Telegram.Bot.Simple.BotApp.Internal where -import Control.Concurrent (ThreadId, forkIO, threadDelay)+import Control.Concurrent (ThreadId, threadDelay)+import Control.Concurrent.Async (Async, async, asyncThreadId, link) import Control.Concurrent.STM import Control.Monad (forever, void, (<=<)) import Control.Monad.Except (catchError)@@ -117,14 +118,16 @@ -- | Process incoming actions indefinitely. processActionsIndefinitely :: BotApp model action -> BotEnv model action -> IO ThreadId-processActionsIndefinitely botApp botEnv = forkIO . forever $ do- runClientM (processActionJob botApp botEnv) (botClientEnv botEnv)+processActionsIndefinitely botApp botEnv = do+ a <- asyncLink $ forever $ do+ runClientM (processActionJob botApp botEnv) (botClientEnv botEnv)+ return (asyncThreadId a) -- | Start 'Telegram.Update' polling for a bot. startBotPolling :: BotApp model action -> BotEnv model action -> ClientM () startBotPolling BotApp{..} botEnv@BotEnv{..} = startPolling handleUpdate where- handleUpdate update = liftIO . void . forkIO $ do+ handleUpdate update = liftIO . void . asyncLink $ do maction <- botAction update <$> readTVarIO botModelVar case maction of Nothing -> return ()@@ -154,3 +157,15 @@ pure maxUpdateId liftIO $ threadDelay 1000000 go nextUpdateId++-- ** 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+ a <- async action+ link a+ return a
src/Telegram/Bot/Simple/Reply.hs view
@@ -3,6 +3,7 @@ module Telegram.Bot.Simple.Reply where import Control.Applicative ((<|>))+import Control.Monad import Control.Monad.Reader import Data.String import Data.Text (Text)@@ -40,11 +41,11 @@ , replyMessageMessageThreadId :: Maybe MessageThreadId -- ^ Unique identifier for the target message thread (topic) of the forum; for forum supergroups only. , replyMessageParseMode :: Maybe ParseMode -- ^ Send 'MarkdownV2', 'HTML' or 'Markdown' (legacy), if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message. , replyMessageEntities :: Maybe [MessageEntity] -- ^ A JSON-serialized list of special entities that appear in message text, which can be specified instead of /parse_mode/.- , replyMessageDisableWebPagePreview :: Maybe Bool -- ^ Disables link previews for links in this message.+ , replyMessageLinkPreviewOptions :: Maybe LinkPreviewOptions -- ^ Link preview generation options for the message. , replyMessageDisableNotification :: Maybe Bool -- ^ Sends the message silently. Users will receive a notification with no sound. , replyMessageProtectContent :: Maybe Bool -- ^ Protects the contents of the sent message from forwarding and saving. , replyMessageReplyToMessageId :: Maybe MessageId -- ^ If the message is a reply, ID of the original message.- , replyMessageAllowSendingWithoutReply :: Maybe Bool -- ^ Pass 'True', if the message should be sent even if the specified replied-to message is not found.+ , replyMessageReplyParameters :: Maybe ReplyParameters -- ^ Description of the message to reply to. , replyMessageReplyMarkup :: Maybe SomeReplyMarkup -- ^ Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user. } deriving (Generic) @@ -63,12 +64,12 @@ , sendMessageText = replyMessageText , sendMessageParseMode = replyMessageParseMode , sendMessageEntities = replyMessageEntities- , sendMessageDisableWebPagePreview = replyMessageDisableWebPagePreview+ , sendMessageLinkPreviewOptions = replyMessageLinkPreviewOptions , sendMessageDisableNotification = replyMessageDisableNotification , sendMessageProtectContent = replyMessageProtectContent , sendMessageReplyToMessageId = replyMessageReplyToMessageId , sendMessageReplyMarkup = replyMessageReplyMarkup- , sendMessageAllowSendingWithoutReply = replyMessageAllowSendingWithoutReply+ , sendMessageReplyParameters = replyMessageReplyParameters } -- | Reply in a chat with a given 'SomeChatId'.@@ -92,7 +93,7 @@ data EditMessage = EditMessage { editMessageText :: Text , editMessageParseMode :: Maybe ParseMode- , editMessageDisableWebPagePreview :: Maybe Bool+ , editMessageLinkPreviewOptions :: Maybe LinkPreviewOptions , editMessageReplyMarkup :: Maybe SomeReplyMarkup } @@ -112,7 +113,7 @@ = EditMessageTextRequest { editMessageTextText = editMessageText , editMessageTextParseMode = editMessageParseMode- , editMessageTextDisableWebPagePreview = editMessageDisableWebPagePreview+ , editMessageTextLinkPreviewOptions = editMessageLinkPreviewOptions , editMessageTextReplyMarkup = editMessageReplyMarkup , editMessageEntities = Nothing , ..@@ -130,7 +131,7 @@ editMessageToReplyMessage :: EditMessage -> ReplyMessage editMessageToReplyMessage EditMessage{..} = (toReplyMessage editMessageText) { replyMessageParseMode = editMessageParseMode- , replyMessageDisableWebPagePreview = editMessageDisableWebPagePreview+ , replyMessageLinkPreviewOptions = editMessageLinkPreviewOptions , replyMessageReplyMarkup = editMessageReplyMarkup }
src/Telegram/Bot/Simple/UpdateParser.hs view
@@ -4,7 +4,7 @@ module Telegram.Bot.Simple.UpdateParser where import Control.Applicative-import Control.Monad.Reader+import Control.Monad #if defined(MIN_VERSION_GLASGOW_HASKELL) #if MIN_VERSION_GLASGOW_HASKELL(8,6,2,0) #else
src/Telegram/Bot/Simple/Webhook.hs view
@@ -3,7 +3,6 @@ {-# LANGUAGE TypeOperators #-} module Telegram.Bot.Simple.Webhook (webhookApp) where -import Control.Concurrent (forkIO) import Control.Concurrent.STM import Control.Monad.IO.Class (MonadIO (liftIO)) import Data.Functor (void)@@ -20,7 +19,7 @@ where updateHandler :: Update -> Handler () updateHandler update = liftIO $ handleUpdate update- handleUpdate update = liftIO . void . forkIO $ do+ handleUpdate update = liftIO . void . asyncLink $ do maction <- botAction update <$> readTVarIO botModelVar case maction of Nothing -> return ()
telegram-bot-simple.cabal view
@@ -1,7 +1,7 @@ cabal-version: 1.12 name: telegram-bot-simple-version: 0.12+version: 0.13 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@@ -49,6 +49,7 @@ build-depends: aeson , aeson-pretty+ , async , base >=4.9 && <5 , bytestring , cron >=0.7.0