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.
35 lines
1.0 KiB
35 lines
1.0 KiB
{-# LANGUAGE OverloadedStrings #-} |
|
|
|
module Network.Telegram |
|
( |
|
mkTelegramContext, |
|
sendMessage, |
|
Manager |
|
) 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 |
|
|
|
data TelegramContext = TelegramContext { |
|
tgToken :: T.Text, |
|
httpMan :: Manager |
|
} |
|
|
|
mkTelegramContext :: Manager -> T.Text -> IO TelegramContext |
|
mkTelegramContext man token = do |
|
return TelegramContext { httpMan = man, tgToken = token } |
|
|
|
|
|
sendMessage :: TelegramContext -> T.Text -> T.Text -> IO () |
|
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)) |
|
|
|
|