Telegram.jl

Native Julia Telegram bot api wrapper
Popularity
93 Stars
Updated Last
1 Month Ago
Started In
October 2017

Telegram

Documentation Build Status
StableDev BuildCoverage

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")

Please refer to documentation to learn how to properly setup telegram credentials and use package in general.

Installation

Package is registered so you can install it in a usual way

julia> using Pkg
julia> Pkg.add("Telegram")

Usage

Telegram Bot API

Usage is straightforward, Telegram Bot API methods are in one to one correspondence with this Julia wrapper. You need to create connection and then simply call necessary methods

julia> using Telegram, Telegram.API
julia> token = "YOUR TELEGRAM BOT TOKEN"
julia> TelegramClient(token)

julia> getMe()
JSON3.Object{Array{UInt8,1},SubArray{UInt64,1,Array{UInt64,1},Tuple{UnitRange{Int64}},true}} with 7 entries:
  :id                          => 123456789
  :is_bot                      => true
  :first_name                  => "Awesome Bot"
  :username                    => "AwesomeBot"
  :can_join_groups             => true
  :can_read_all_group_messages => false
  :supports_inline_queries     => false

Mainly you need to set arguments, but chat_id can be set directly in TelegramClient

julia> token = "YOUR TELEGRAM BOT TOKEN"
julia> TelegramClient(token; chat_id = "YOUR TELEGRAM BOT CHAT_ID")

julia> sendMessage(text = "Hello, world!")

You can send files and other IO objects

julia> sendPhoto(photo = open("picture.jpg", "r"))
julia> io = IOBuffer()
julia> print(io, "Hello world!")
julia> sendDocument(document = "hello.txt" => io)

Logging

You can use Telegram.jl together with LoggingExtras.jl to create powerful logging with insta messaging in case of critical situations

Put your credentials in .env

# .env
TELEGRAM_BOT_TOKEN = <YOUR TELEGRAM BOT TOKEN>
TELEGRAM_BOT_CHAT_ID = <YOUR TELEGRAM CHAT ID>
using Telegram
using Logging, LoggingExtras
using ConfigEnv

dotenv() # populate ENV with the data from .env

tg = TelegramClient()
tg_logger = TelegramLogger(tg; async = false)
demux_logger = TeeLogger(
    MinLevelLogger(tg_logger, Logging.Error),
    ConsoleLogger()
)
global_logger(demux_logger)

@warn "It is bad"        # goes to console
@info "normal stuff"     # goes to console
@error "THE WORSE THING" # goes to console and telegram
@debug "it is chill"     # goes to console

Bots

You can create bot with the run_bot command. Here is for example Echo bot

# .env
TELEGRAM_BOT_TOKEN = <YOUR TELEGRAM BOT TOKEN>
using Telegram, Telegram.API
using ConfigEnv

dotenv()

# Echo bot
run_bot() do msg
    sendMessage(text = msg.message.text, chat_id = msg.message.chat.id)
end

Used By Packages

No packages found.