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 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
tg = TelegramClient("your token"; chat_id = "your default chat_id") sendMessage(text = "Calculation complete")
sendMessage(tg; text = "Calculation complete")
Package is registered so you can install it in a usual way
julia> using Pkg julia> Pkg.add("Telegram")
In addition to API Reference methods, there is a number of methods which add some julian functionality like bots and logging facilities.
TelegramClient(token; chat_id, parse_mode, ep, use_globally = true)
Creates telegram client, which can be used to run telegram commands.
token::String: telegram token, which can be obtained in telegram from @BotFather.
chat_id: if set, used as default
chat_idargument for all chat related commands. To get specific
chat_id, send message to the bot in telegram application and use
parse_mode: if set, used as default for text messaging commands.
true. If set to
truethen it current client can be used as default in all telegram commands.
ep: endpoint for telegram api. By default
https://api.telegram.org/botis used, but you may change it, if you are using proxy or for testing purposes.
TelegramLogger(tg::TelegramClient; fmt = tg_formatter, min_level = Logging.Info, async = true)
Creates logger, which output log messages to Telegram channel.
tg: telegram client, should have valid
chat_idwhich will be used for outcome messages.
fmt: function which accepts (
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 in
asyncmode. Since telegram messages can take some time to be send, it make sense to set this parameter to
true, 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 to
silent_errors: by default, if there was an error during sending a message, it will be printed in
stderrchannel. If you want to silence this output, set
apiquery(method, client; kwargs...)
method request to the telegram. It is recommended to use only if some of Telegram API function is not wrapped already.
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.
run_bot(f::Function, [tg::TelegramClient]; timeout = 10, brute_force_alive = false, offset = -1)
Run telegram bot, which executes function
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 interrupted
brute_force_alive: despite all measures, messages sometimes "hangs". You may use this argument to wake up telegram server regularly. When this option is set to
trueit 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.
tg as default client in all
tg = TelegramClient(ENV["TG_TOKEN"]) useglobally!(tg) getMe() # new client is used in this command by default.