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

using Telegram, Telegram.API
tg = TelegramClient("YOUR TOKEN", chat_id = "YOUR CHAT_ID")

# Some lengthy calculation
# ...

sendMessage(text  = "Calculation complete, result is $result")

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.TelegramClientMethod
TelegramClient(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 default chat_id argument for all chat related commands. To get specific chat_id, send message to the bot in telegram application and use getUpdates command.
  • parse_mode: if set, used as default for text messaging commands.
  • use_globally::Bool: default true. If set to true then it current client can be used as default in all telegram commands.
  • ep: endpoint for telegram api. By default https://api.telegram.org/bot is used, but you may change it, if you are using proxy or for testing purposes.
source
Telegram.TelegramLoggerMethod
TelegramLogger(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 valid chat_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 in sync or async mode. 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 false
source
Telegram.apiqueryFunction
apiquery(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.
source
Telegram.run_botFunction
run_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 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 true 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.
source
Telegram.useglobally!Method
useglobally!(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.
source