telegram-api: Telegram Bot API bindings

[ bsd3, library, web ] [ Propose Tags ]

High-level bindings to the Telegram Bot API


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.1.0.1, 0.2.0.0, 0.2.1.0, 0.2.1.1, 0.3.0.0, 0.3.1.0, 0.4.0.0, 0.4.0.1, 0.4.1.0, 0.4.2.0, 0.4.3.0, 0.4.3.1, 0.5.0.0, 0.5.0.1, 0.5.1.1, 0.5.1.2, 0.5.2.0, 0.6.0.0, 0.6.0.1, 0.6.0.2, 0.6.1.0, 0.6.1.1, 0.6.2.0, 0.6.3.0, 0.7.0.0, 0.7.1.0, 0.7.2.0 (info)
Change log CHANGELOG.md
Dependencies aeson (>=1.0 && <1.2), base (>=4.7 && <5), bytestring, http-api-data, http-client (>=0.5 && <0.6), http-media, http-types, mime-types, mtl (>=2.2 && <2.3), servant (>=0.9 && <0.10), servant-client (>=0.9 && <0.10), string-conversions, text, transformers [details]
License BSD-3-Clause
Copyright Alexey Rodiontsev (c) 2016
Author Alexey Rodiontsev
Maintainer alex.rodiontsev@gmail.com
Revised Revision 1 made by Bodigrim at 2021-10-15T19:37:17Z
Category Web
Home page http://github.com/klappvisor/haskell-telegram-api#readme
Source repo head: git clone https://github.com/klappvisor/haskell-telegram-api
Uploaded by klappvisor at 2017-03-06T21:13:45Z
Distributions
Reverse Dependencies 2 direct, 0 indirect [details]
Downloads 17873 total (60 in the last 30 days)
Rating 2.0 (votes: 3) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2017-03-06 [all 1 reports]

Readme for telegram-api-0.6.1.0

[back to package description]

telegram-api

Join the chat at https://gitter.im/klappvisor/haskell-telegram-api

Build Status Hackage Hackage Dependencies Haskell Programming Language BSD3 License

High-level bindings to the Telegram Bot API based on servant library. Both getUpdates request or webhook can be used to receive updates for your bot. Inline mode is supported. Uploading stickers, documents, video, etc is not supported yet, but you can send items which are already uploaded on the Telegram servers.

Support of Bot-2.0 API

Usage

There are two ways of using Telegram Bot API. First and original way is to run IO directly for every Telegram servers request, another one is based on TelegramClient which is just ReaderT.

Use TelegramClient

{-# LANGUAGE OverloadedStrings #-}

import           Network.HTTP.Client      (newManager)
import           Network.HTTP.Client.TLS  (tlsManagerSettings)
import           Web.Telegram.API.Bot

main :: IO ()
main = do
  let token = Token "bot<token>" -- entire Token should be bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
  manager <- newManager tlsManagerSettings
  result <- runClient ( do
    info <- getWebhookInfoM
    let request = setWebhookRequest' "https://example.com/hook"
    isSet <- setWebhookM request
    getMeM) token manager
  print result
  print "done!"

Runing IO directly

getMe example

{-# LANGUAGE OverloadedStrings #-}

import           Network.HTTP.Client      (newManager)
import           Network.HTTP.Client.TLS  (tlsManagerSettings)
import           Web.Telegram.API.Bot

main :: IO ()
main = do
  manager <- newManager tlsManagerSettings
  res <- getMe token manager
  case res of
    Left e -> do
      putStrLn "Request failed"
      print e
    Right Response { result = u } -> do
      putStrLn "Request succeded"
      print $ user_first_name u
  where token = Token "bot<token>" -- entire Token should be bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11

sendMessage example

{-# LANGUAGE OverloadedStrings #-}

import           Network.HTTP.Client      (newManager)
import           Network.HTTP.Client.TLS  (tlsManagerSettings)
import           Web.Telegram.API.Bot

main :: IO ()
main = do
  manager <- newManager tlsManagerSettings
  let request = sendMessageRequest chatId message
  res <- sendMessage token request manager
  case res of
    Left e -> do
      putStrLn "Request failed"
      print e
    Right Response { result = m } -> do
      putStrLn "Request succeded"
      print $ message_id m
      print $ text m
  where token = Token "bot<token>" -- entire Token should be bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
        chatId = "<chat_id> or <@channelusername>"
        message = "text *bold* _italic_ [github](github.com/klappvisor/haskell-telegram-api)"

Note on requests:

Many request data records have a lot of optional parameters which are usually redundant. There are two ways to create requests:

With data type constructor:

let request = SendMessageRequest "chatId" "text" Nothing (Just True) Nothing Nothing Nothing

Using default instance:

let request = sendMessageRequest "chatId" "text" -- only with required fields
let request = (sendMessageRequest "chatId" "text") {
  message_disable_notification = Just True -- with optional fields
}

Contribution

Contributions are welcome!

  1. Fork repository
  2. Do some changes
  3. Create pull request
  4. Wait for CI build and review
  5. ??????
  6. PROFIT

Bear in mind that the CI build won't run integration test suite against your pull request since the necessary environment variables ($BOT_TOKEN, $CHAT_ID and $BOT_NAME) aren't exported when a fork starts the build (for security reasons). If you do want to run them before creating RP, you can setup integration of your fork with CircleCI.

You can use stack to build project

stack build

To run test you have to create your own bot. Go to BotFather and create the bot. As the result you will have private bot's access token. Keep it safe!

stack test --test-arguments "--integration -t BOT_TOKEN -c CHAT_ID -b BOT_NAME -- HSPEC_ARGS"

where

  • BOT_TOKEN is the token obtained from BotFather
  • CHAT_ID can be id of your chat with your bot. Send some messages to this chat in Telegram and do curl "https://api.telegram.org/bot<replace_with_token>/getUpdates", you'll have to parse some JSON with your brain ;-) or any other suitable tool and you will find chat id there.
  • BOT_NAME is the name of your bot
  • HSPEC_ARGS are the normal hspec arguments you can find here

The help option is available for the tests and for hspec:

stack test --test-arguments "-h"
stack test --test-arguments "--integration -t BOT_TOKEN -c CHAT_ID -b BOT_NAME -- -h"

Note: Inline Spec is disabled for now...

If everything is fine after running the tests you will receive a few new messages from your bot.