|
5 | 5 | [](https://travis-ci.com/Arkoniak/Telegram.jl)
|
6 | 6 | [](https://codecov.io/gh/Arkoniak/Telegram.jl)
|
7 | 7 |
|
8 |
| -Telegram api wrapper |
| 8 | +Simple [Telegram Messaging](https://telegram.org/) 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 |
| 9 | + |
| 10 | +```julia |
| 11 | +using Telegram, Telegram.API |
| 12 | +tg = TelegramClient("YOUR TOKEN", chat_id = "YOUR CHAT_ID") |
| 13 | + |
| 14 | +# Some lengthy calculation |
| 15 | +# ... |
| 16 | + |
| 17 | +sendMessage(text = "Calculation complete, result is $result") |
| 18 | +``` |
| 19 | + |
| 20 | +Please refer to [documentation](https://Arkoniak.github.io/Telegram.jl/dev) to learn how to properly setup telegram credentials and use package in general. |
9 | 21 |
|
10 | 22 | # Installation
|
11 | 23 |
|
12 |
| -Currently package is not registered, so it should be installed from source |
| 24 | +Package is registered so you can install it in a usual way |
13 | 25 |
|
14 | 26 | ```julia
|
15 | 27 | julia> using Pkg
|
16 |
| -julia> Pkg.add("https://github.com/Arkoniak/Telegram.jl.git") |
| 28 | +julia> Pkg.add("Telegram") |
17 | 29 | ```
|
18 | 30 |
|
19 | 31 | # Usage
|
20 | 32 |
|
| 33 | +## Telegram API |
| 34 | + |
21 | 35 | Usage is straightforward, [Telegram API methods](https://core.telegram.org/bots/api#available-methods) are in one to one correspondence with this Julia wrapper. You need to create connection and then simply call necessary methods
|
22 | 36 |
|
23 | 37 | ```julia
|
| 38 | +julia> using Telegram, Telegram.API |
24 | 39 | julia> token = "HERE SHOULD BE YOUR TOKEN"
|
25 |
| -julia> client = TelegramClient(token) |
| 40 | +julia> TelegramClient(token) |
26 | 41 |
|
27 |
| -julia> getMe(client) |
28 |
| -JSON3.Object{Base.CodeUnits{UInt8,String},Array{UInt64,1}} with 2 entries: |
29 |
| - :ok => true |
30 |
| - :result => {… |
| 42 | +julia> getMe() |
| 43 | +JSON3.Object{Array{UInt8,1},SubArray{UInt64,1,Array{UInt64,1},Tuple{UnitRange{Int64}},true}} with 7 entries: |
| 44 | + :id => 123456789 |
| 45 | + :is_bot => true |
| 46 | + :first_name => "Awesome Bot" |
| 47 | + :username => "AwesomeBot" |
| 48 | + :can_join_groups => true |
| 49 | + :can_read_all_group_messages => false |
| 50 | + :supports_inline_queries => false |
31 | 51 | ```
|
32 | 52 |
|
33 |
| -You can necessary arguments in methods, but some subset can be set in `client` itself |
| 53 | +Mainly you need to set arguments, but `chat_id` can be set directly in `TelegramClient` |
34 | 54 |
|
35 | 55 | ```julia
|
36 | 56 | julia> token = "HERE SHOULD BE YOUR TOKEN"
|
37 |
| -julia> client = TelegramClient(token; chat_id = "HERE SHOULD BE CHAT_ID") |
| 57 | +julia> TelegramClient(token; chat_id = "HERE SHOULD BE CHAT_ID") |
38 | 58 |
|
39 |
| -julia> sendMessage(client, text = "Hello, world!") |
| 59 | +julia> sendMessage(text = "Hello, world!") |
40 | 60 | ```
|
41 |
| -or equivalently |
| 61 | + |
| 62 | +You can send files and other `io` objects |
42 | 63 |
|
43 | 64 | ```julia
|
44 |
| -julia> token = "HERE SHOULD BE YOUR TOKEN" |
45 |
| -julia> client = TelegramClient(token) |
| 65 | +julia> sendPhoto(photo = open("picture.jpg", "r")) |
| 66 | +julia> io = IOBuffer() |
| 67 | +julia> print(io, "Hello world!") |
| 68 | +julia> sendDocument(document = "hello.txt" => io) |
| 69 | +``` |
| 70 | + |
| 71 | +## Logging |
| 72 | + |
| 73 | +You can use [Telegram.jl](https://github.com/Arkoniak/Telegram.jl) together with [LoggingExtras.jl](https://github.com/oxinabox/LoggingExtras.jl) to create powerful logging with insta messaging in case of critical situations |
| 74 | + |
| 75 | +```julia |
| 76 | +using Telegram |
| 77 | +using Logging, LoggingExtras |
| 78 | + |
| 79 | +# considering that token and chat_id variables are set as corresponding |
| 80 | +# environment variables |
| 81 | +tg = TelegramClient(ENV["TG_TOKEN"], chat_id = ENV["TG_CHAT_ID"]) |
| 82 | +tg_logger = TelegramLogger(tg; async = false) |
| 83 | +demux_logger = TeeLogger( |
| 84 | + MinLevelLogger(tg_logger, Logging.Error), |
| 85 | + ConsoleLogger() |
| 86 | +) |
| 87 | +global_logger(demux_logger) |
| 88 | + |
| 89 | +@warn "It is bad" # goes to console |
| 90 | +@info "normal stuff" # goes to console |
| 91 | +@error "THE WORSE THING" # goes to console and telegram |
| 92 | +@debug "it is chill" # goes to console |
| 93 | +``` |
| 94 | + |
| 95 | +## Bots |
| 96 | + |
| 97 | +You can create bot very easy with the `run_bot` command. Here is for example Echo bot |
| 98 | +```julia |
| 99 | +using Telegram, Telegram.API |
| 100 | + |
| 101 | +token = ENV["TG_TOKEN"] |
| 102 | +tg = TelegramClient(token) |
46 | 103 |
|
47 |
| -julia> sendMessage(client; text = "Hello, world!", chat_id = "HERE SHOULD BE CHAT_ID") |
| 104 | +# Echo bot |
| 105 | +run_bot() do msg |
| 106 | + sendMessage(text = msg.message.text, chat_id = msg.message.chat.id) |
| 107 | +end |
48 | 108 | ```
|
0 commit comments