ATrade-QUIK connector
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1 KiB

9 years ago
{-# LANGUAGE OverloadedStrings #-}
module Network.Telegram
(
mkTelegramContext,
9 years ago
sendMessage,
Manager
9 years ago
) where
import Control.Monad
import Network.Connection
import Network.HTTP.Client
import Network.HTTP.Client.TLS
import qualified Data.Text as T
import qualified Data.ByteString.UTF8 as BU8
import Data.Aeson
import Data.Aeson.Types
type TelegramApiToken = T.Text
type TelegramChatId = T.Text
9 years ago
data TelegramContext = TelegramContext {
tgToken :: TelegramApiToken,
9 years ago
httpMan :: Manager
}
mkTelegramContext :: Manager -> TelegramApiToken -> IO TelegramContext
9 years ago
mkTelegramContext man token = do
9 years ago
return TelegramContext { httpMan = man, tgToken = token }
sendMessage :: TelegramContext -> TelegramChatId -> T.Text -> IO ()
9 years ago
sendMessage ctx chatId text = do
req <- parseUrl $ "https://api.telegram.org/bot" ++ (T.unpack $ tgToken ctx) ++ "/sendMessage"
void $ withResponse (req { method = "POST", requestHeaders = [("Content-Type", BU8.fromString "application/json")], requestBody = (RequestBodyLBS . encode) (object ["chat_id" .= chatId, "text" .= text]) }) (httpMan ctx) (\resp -> brConsume (responseBody resp))