telegram-api
Telegram Bot API bindings
http://github.com/klappvisor/haskell-telegram-api#readme
| Version on this page: | 0.5.0.0 |
| LTS Haskell 8.24: | 0.6.3.0 |
| Stackage Nightly 2017-02-11: | 0.6.0.0 |
| Latest on Hackage: | 0.7.2.0@rev:1 |
telegram-api-0.5.0.0@sha256:e2f0316b8547a59ae408451a5712f693b9a485de07b8af6a3908fda2b877ca4d,3041Module documentation for 0.5.0.0
- Web
- Web.Telegram
telegram-api
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
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 GetMeResponse { user_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 MessageResponse { message_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!
- Fork repository
- Do some changes
- Create pull request
- Wait for CI build and review
- ??????
- 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_TOKENis the token obtained from BotFatherCHAT_IDcan be id of your chat with your bot. Send some messages to this chat in Telegram and docurl "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_NAMEis the name of your botHSPEC_ARGSare the normalhspecarguments 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.
Changes
0.5.0.0 [Breaking]
Features:
- [Breaking] Changed
Responsedata record to be generic - Added certificate upload to set web-hook method for self-signed certificates
Bugfixes:
- Removed
O2tag from cabal file
0.4.3.1
Bugfixes:
- Exposed
MessageEntity
0.4.3.0
Features:
- Added Inline Keyboard to messages
0.4.2.0
Features:
- Bot-2.1 support
- Added new methods:
getChat,leaveChat,getChatAdministrators,getChatMember,getChatMembersCount. - Added support for edited messages and new mentions from Telegram v.3.9. New fields:
edited_messageinUpdate,edit_dateinMessage, user inMessageEntity. New valuetext_mentionfor the type field inMessageEntity`.
- Added new methods:
0.4.1.0
Features:
- Implemented file uploading for audio, voice, sticker, video and documents
Bugfixes:
- Exposed constrictors for inline edit requests
0.4.0.1
Bugfixes:
- Parsing issue with answer callback query response
0.4.0.0 [Breaking]
Features:
- Bot-2.0 API support is almost complete. Everything from announce is on place except file upload (works for photo BTW).
- Inline keyboards
- Updating messages
- Send Location and Phone number
- Inline Bots 2.0
- Group Admins
- and many others
- Added new and more convenient way to create request data records avoiding many optional parameters.
- migrated to servant 0.7.*
0.3.1.0
Features:
- Added possibility to upload and send photo
0.3.0.0
Bugfixes:
- [Breaking] Changed User to be optional in
fromfield of theMessagedata record since sender can be empty for messages sent to channels
0.2.1.1
Bugfixes:
- Minor fix of chat action deserialization code
0.2.1.0
Features:
- Added reply keyboard
0.2.0.0
Features:
- Main functionality is on place except content upload.
- Inline mode added