Telegram
Simple Telegram Messaging SDK with logging and bot facilities. Package was built with first-class support of telegram as instant message backend for various notification and reporing systems. So, simpliest way to use this package is by doing something like this
Put your credentials in .env
file
# .env
TELEGRAM_BOT_TOKEN = <YOUR TELEGRAM BOT TOKEN>
TELEGRAM_BOT_CHAT_ID = <YOUR TELEGRAM CHAT ID>
using Telegram, Telegram.API
using ConfigEnv
dotenv()
# Some lengthy calculation
# ...
sendMessage(text = "Calculation complete, result is $result")
Of course you can manually provide secrets with the TelegramClient
constructor
tg = TelegramClient("your token"; chat_id = "your default chat_id")
sendMessage(text = "Calculation complete")
or even
sendMessage(tg; text = "Calculation complete")
Installation
Package is registered so you can install it in a usual way
julia> using Pkg
julia> Pkg.add("Telegram")
General methods
In addition to API Reference methods, there is a number of methods which add some julian functionality like bots and logging facilities.
Telegram.TelegramClient
— TypeTelegramClient(token; chat_id, parse_mode, ep, use_globally = true)
Creates telegram client, which can be used to run telegram commands.
Arguments
token::String
: telegram token, which can be obtained in telegram from @BotFather.chat_id
: if set, used as defaultchat_id
argument for all chat related commands. To get specificchat_id
, send message to the bot in telegram application and usegetUpdates
command.parse_mode
: if set, used as default for text messaging commands.use_globally::Bool
: defaulttrue
. If set totrue
then it current client can be used as default in all telegram commands.ep
: endpoint for telegram api. By defaulthttps://api.telegram.org/bot
is used, but you may change it, if you are using proxy or for testing purposes.
Telegram.TelegramLogger
— MethodTelegramLogger(tg::TelegramClient; fmt = tg_formatter,
min_level = Logging.Info, async = true)
Creates logger, which output log messages to Telegram channel.
Arguments
tg
: telegram client, should have validchat_id
which will be used for outcome messages.fmt
: function which accepts (level
,_module
,group
,id
,file
,line
) arguments and outputs message prefix. More details can be found in Logging module. By default each messages is prepended with uppercase level, e.g. "INFO: " and the like.min_level
: minimum level of log messages which is going to be processed.async
: send messages insync
orasync
mode. Since telegram messages can take some time to be send, it make sense to set this parameter totrue
, so messages is send in the background with little impact on main program. But since in one run scripts main julia process can be finished before async message is sent, it make sense to set this parameter tofalse
silent_errors
: by default, if there was an error during sending a message, it will be printed instderr
channel. If you want to silence this output, setsilent_errors
totrue
.
Telegram.apiquery
— Functionapiquery(method, client; kwargs...)
Sends method
request to the telegram. It is recommended to use only if some of Telegram API function is not wrapped already.
Arguments
method::String
: method name from the Telegram API, for example "getMe"client::TelegramClient
: telegram client, can be omitted, in this case default client is used.
Telegram.run_bot
— Functionrun_bot(f::Function, [tg::TelegramClient]; timeout = 10, brute_force_alive = false, offset = -1)
Run telegram bot, which executes function f
repeatedly.
Arguments
f
: function to run with the bot. As an input it receive Message in the form of JSON3 object.tg
: Telegram client, if not set then global telegram client is used.timeout
: timeout limit of messages, defines when longpoll messages should be interruptedbrute_force_alive
: despite all measures, messages sometimes "hangs". You may use this argument to wake up telegram server regularly. When this option is set totrue
it works, but it is not recommended, since it makes additional calls. Consider this feature experimental and use at your own risk.offset
: telegram message offset, useful if you have processed messages, but bot was interrupted and messages state was lost.
Telegram.useglobally!
— Methoduseglobally!(tg::TelegramClient)
Set tg
as default client in all Telegram.API
functions.
Example
tg = TelegramClient(ENV["TG_TOKEN"])
useglobally!(tg)
getMe() # new client is used in this command by default.