Skip to content

Commit

Permalink
Move API to telegram-bot-api
Browse files Browse the repository at this point in the history
  • Loading branch information
swamp-agr committed Feb 11, 2023
1 parent 6655ccc commit 11601af
Show file tree
Hide file tree
Showing 186 changed files with 306 additions and 228 deletions.
3 changes: 3 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
telegram-bot-api/
telegram-bot-simple/
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@

module Telegram.Bot.API.Webhook
( setUpWebhook,
webhookApp,
deleteWebhook,
SetWebhookRequest (..),
)
where

import Control.Concurrent (forkIO)
import Control.Concurrent.STM
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Aeson (ToJSON (toJSON))
import Data.Bool (bool)
Expand All @@ -32,31 +29,11 @@ import Servant.Client (ClientEnv, ClientError,
client, runClientM)
import Servant.Multipart.API
import Servant.Multipart.Client (genBoundary)
import Telegram.Bot.API.GettingUpdates (Update)

import Telegram.Bot.API.Internal.Utils (gtoJSON)
import Telegram.Bot.API.MakingRequests (Response)
import Telegram.Bot.API.Types (InputFile, makeFile)
import Telegram.Bot.Simple.BotApp.Internal

type WebhookAPI = ReqBody '[JSON] Update :> Post '[JSON] ()

server :: BotApp model action -> BotEnv model action -> Server WebhookAPI
server BotApp {..} botEnv@BotEnv {..} =
updateHandler
where
updateHandler :: Update -> Handler ()
updateHandler update = liftIO $ handleUpdate update
handleUpdate update = liftIO . void . forkIO $ do
maction <- botAction update <$> readTVarIO botModelVar
case maction of
Nothing -> return ()
Just action -> issueAction botEnv (Just update) (Just action)

webhookAPI :: Proxy WebhookAPI
webhookAPI = Proxy

app :: BotApp model action -> BotEnv model action -> Application
app botApp botEnv = serve webhookAPI $ server botApp botEnv

data SetWebhookRequest = SetWebhookRequest
{ setWebhookUrl :: String,
Expand Down Expand Up @@ -118,5 +95,3 @@ deleteWebhook = (void <$>) <$> runClientM deleteWebhookRequest
requestData = DeleteWebhookRequest {deleteWebhookDropPendingUpdates = Nothing}
deleteWebhookRequest = client (Proxy @DeleteWebhook) requestData

webhookApp :: BotApp model action -> BotEnv model action -> Application
webhookApp = app
205 changes: 4 additions & 201 deletions telegram-bot-simple.cabal → telegram-bot-api/telegram-bot-api.cabal
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.0.
--
-- see: https://github.com/sol/hpack

name: telegram-bot-simple
version: 0.9
synopsis: Easy to use library for building Telegram bots.
name: telegram-bot-api
version: 6.5
synopsis: Easy to use library for building Telegram bots. Exports Telegram Bot API.
description: Please see the README on Github at <https://github.com/fizruk/telegram-bot-simple#readme>
category: Web
homepage: https://github.com/fizruk/telegram-bot-simple#readme
Expand All @@ -25,11 +21,6 @@ source-repository head
type: git
location: https://github.com/fizruk/telegram-bot-simple

flag examples
description: Build examples
manual: True
default: False

library
exposed-modules:
-- API
Expand Down Expand Up @@ -195,19 +186,8 @@ library
Telegram.Bot.API.UpdatingMessages
Telegram.Bot.API.WebApps
Telegram.Bot.API.Webhook
-- Telegram Bot Simple
Telegram.Bot.Simple
Telegram.Bot.Simple.BotApp
Telegram.Bot.Simple.BotApp.Internal
Telegram.Bot.Simple.Conversation
Telegram.Bot.Simple.Debug
Telegram.Bot.Simple.Eff
Telegram.Bot.Simple.InlineKeyboard
Telegram.Bot.Simple.Instances
Telegram.Bot.Simple.Reply
Telegram.Bot.Simple.UpdateParser
other-modules:
Paths_telegram_bot_simple
Paths_telegram_bot_api
hs-source-dirs:
src
ghc-options: -Wall
Expand Down Expand Up @@ -241,180 +221,3 @@ library
, warp
, warp-tls
default-language: Haskell2010

executable example-echo-bot
main-is: examples/EchoBot.hs
other-modules:
Paths_telegram_bot_simple
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends:
aeson
, aeson-pretty
, base >=4.9 && <5
, bytestring
, cron >=0.7.0
, filepath
, hashable
, http-api-data
, http-client
, http-client-tls
, monad-control
, mtl
, pretty-show
, profunctors
, servant
, servant-client
, servant-multipart-api
, servant-multipart-client
, servant-server
, split
, stm
, template-haskell
, text
, time
, transformers
, unordered-containers
, warp
, warp-tls
default-language: Haskell2010
if flag(examples)
build-depends:
telegram-bot-simple
else
buildable: False

executable example-echo-bot-webhook
main-is: examples/EchoBotWebhook.hs
other-modules:
Paths_telegram_bot_simple
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends:
aeson
, aeson-pretty
, base >=4.9 && <5
, bytestring
, cron >=0.7.0
, filepath
, hashable
, http-api-data
, http-client
, http-client-tls
, monad-control
, mtl
, pretty-show
, profunctors
, servant
, servant-client
, servant-multipart-api
, servant-multipart-client
, servant-server
, split
, stm
, template-haskell
, text
, time
, transformers
, unordered-containers
, warp
, warp-tls
default-language: Haskell2010
if flag(examples)
build-depends:
telegram-bot-simple
else
buildable: False

executable example-game-bot
main-is: examples/GameBot.hs
other-modules:
Paths_telegram_bot_simple
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends:
aeson
, aeson-pretty
, base >=4.9 && <5
, bytestring
, cron >=0.7.0
, filepath
, hashable
, http-api-data
, http-client
, http-client-tls
, monad-control
, mtl
, pretty-show
, profunctors
, servant
, servant-client
, servant-multipart-api
, servant-multipart-client
, servant-server
, split
, stm
, template-haskell
, text
, time
, transformers
, unordered-containers
, warp
, warp-tls
default-language: Haskell2010
if flag(examples)
build-depends:
QuickCheck
, blaze-html
, cookie
, dhall
, http-types
, optparse-applicative
, prettyprinter
, random
, servant-blaze
, servant-server
, signal
, telegram-bot-simple
, uuid
, warp
else
buildable: False

executable example-todo-bot
main-is: examples/TodoBot.hs
other-modules:
Paths_telegram_bot_simple
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends:
aeson
, aeson-pretty
, base >=4.9 && <5
, bytestring
, cron >=0.7.0
, filepath
, hashable
, http-api-data
, http-client
, http-client-tls
, monad-control
, mtl
, pretty-show
, profunctors
, servant
, servant-client
, servant-multipart-api
, servant-multipart-client
, servant-server
, split
, stm
, template-haskell
, text
, time
, transformers
, unordered-containers
, warp
, warp-tls
default-language: Haskell2010
if flag(examples)
build-depends:
telegram-bot-simple
else
buildable: False
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import Network.Wai.Handler.WarpTLS
import qualified Telegram.Bot.API as Telegram
import Telegram.Bot.API.Webhook (SetWebhookRequest,
deleteWebhook,
setUpWebhook, webhookApp)
setUpWebhook)
import Telegram.Bot.Simple.BotApp.Internal
import Telegram.Bot.Simple.Webhook (webhookApp)

-- | Start bot with asynchronous polling.
-- The result is a function that allows you to send actions
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
37 changes: 37 additions & 0 deletions telegram-bot-simple/src/Telegram/Bot/Simple/Webhook.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RecordWildCards #-}
{-# 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)
import Servant

import Telegram.Bot.API.GettingUpdates (Update)
import Telegram.Bot.Simple.BotApp.Internal

type WebhookAPI = ReqBody '[JSON] Update :> Post '[JSON] ()

server :: BotApp model action -> BotEnv model action -> Server WebhookAPI
server BotApp {..} botEnv@BotEnv {..} =
updateHandler
where
updateHandler :: Update -> Handler ()
updateHandler update = liftIO $ handleUpdate update
handleUpdate update = liftIO . void . forkIO $ do
maction <- botAction update <$> readTVarIO botModelVar
case maction of
Nothing -> return ()
Just action -> issueAction botEnv (Just update) (Just action)

webhookAPI :: Proxy WebhookAPI
webhookAPI = Proxy

app :: BotApp model action -> BotEnv model action -> Application
app botApp botEnv = serve webhookAPI $ server botApp botEnv

webhookApp :: BotApp model action -> BotEnv model action -> Application
webhookApp = app

Loading

0 comments on commit 11601af

Please sign in to comment.