telethon 0.4.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +36 -0
- data/README.md +34 -62
- data/lib/telethon/cli/args.rb +33 -0
- data/lib/telethon/cli/call.rb +182 -0
- data/lib/telethon/cli/commands/account.rb +357 -0
- data/lib/telethon/cli/commands/bots.rb +91 -0
- data/lib/telethon/cli/commands/chats.rb +279 -0
- data/lib/telethon/cli/commands/reading.rb +203 -0
- data/lib/telethon/cli/commands/writing.rb +278 -0
- data/lib/telethon/cli/commands.rb +158 -468
- data/lib/telethon/cli/completion.rb +100 -0
- data/lib/telethon/cli/table.rb +805 -0
- data/lib/telethon/cli.rb +108 -45
- data/lib/telethon/client/auth.rb +12 -4
- data/lib/telethon/client/files.rb +11 -5
- data/lib/telethon/client/messages.rb +82 -13
- data/lib/telethon/custom/dialog.rb +5 -0
- data/lib/telethon/network/sender.rb +22 -1
- data/lib/telethon/network/transport.rb +14 -1
- data/lib/telethon/private_files.rb +5 -1
- data/lib/telethon/version.rb +1 -1
- data/lib/telethon.rb +8 -0
- metadata +9 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2f131aedb880401c8fa7958fd0802a2e9b51595bf64f1eca1e43ea17f4046a0f
|
|
4
|
+
data.tar.gz: c593f31d90ad9297182d2483a504072988e189165ce682306b4ffcfd6cdf4102
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 58c033a56e4042b3a1278017cb9b07ddc1c3babae3a5a486c344c285ddc5ccdd0b7fcf9bc09df8c5c9c042dd4d2c81474e4ec5e2724d39fda128924fe5030cd2
|
|
7
|
+
data.tar.gz: e54ff6e8db9cc831c05c236702a40a1e03c45026d625cfc5d51624615788abc14c7acc097ed9bd6f4c005db1d90f84d5ba8f1e56c45ebf65975c8b6ff0b6a670
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,42 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.0.0] — 2026-12-02
|
|
11
|
+
|
|
12
|
+
The 1.0 release: milestones M7 and M8 of [the roadmap](docs/ROADMAP.md). The library has every public
|
|
13
|
+
Telethon client method, event and custom class (`rake parity`: 87 of 87), and its public API is now frozen
|
|
14
|
+
under Semantic Versioning (see [the library guide](docs/library.md)). `tg` covers what Telethon can do.
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- A complete `tg`: one command table (`lib/telethon/cli/table.rb`) drives option parsing, grouped
|
|
19
|
+
`tg --help`, `tg help COMMAND`, shell completion and the generated [docs/cli.md](docs/cli.md).
|
|
20
|
+
- Reading: `tg chats`, `info`, `listen`.
|
|
21
|
+
- Writing: `reply`, `pin`, `unpin`, `mark-read`, `react`, `draft`, `typing`, and `send` with
|
|
22
|
+
`--at`, `--album`, `--voice`, `--video-note` and `--media`.
|
|
23
|
+
- Chats: `members`, `admin`, `ban`, `kick`, `unban`, `permissions`, `admin-log`, `stats`, `join`,
|
|
24
|
+
`leave`, `archive`, `unarchive`, `delete-chat`, `profile-photos`.
|
|
25
|
+
- Account: `sessions` (and `--terminate`), `profile`.
|
|
26
|
+
- `tg call METHOD [JSON]` for any raw TL function, and `tg completion bash|zsh|fish`.
|
|
27
|
+
- `--json` on every command.
|
|
28
|
+
- Several accounts side by side: `tg --account NAME` (or `TG_ACCOUNT`) keeps each account in
|
|
29
|
+
`TG_HOME/accounts/NAME`; the account in `TG_HOME` itself stays the default. `--account` also takes an
|
|
30
|
+
account's `@username` or numeric id. `tg accounts` lists them; `tg accounts rename OLD NEW` renames one.
|
|
31
|
+
A new account reuses the default account's API id and hash.
|
|
32
|
+
- `TG_CONNECTION` picks `tg`'s transport (`tcp_full`, `tcp_abridged`, `tcp_intermediate`,
|
|
33
|
+
`tcp_obfuscated`, `tcp_obfuscated_intermediate`, `http`).
|
|
34
|
+
- Documentation in `docs/`: install, getting started, the CLI reference, the library guide (with
|
|
35
|
+
example tests), differences from Telethon, and troubleshooting.
|
|
36
|
+
|
|
37
|
+
### Fixed
|
|
38
|
+
|
|
39
|
+
- The HTTP transport no longer hangs waiting for a result that Telegram sends in the answer to a later
|
|
40
|
+
request: it polls with `http_wait`.
|
|
41
|
+
- `tg send --file` sends files as documents again, as before 0.4.0's CLI work; `--media` sends photos
|
|
42
|
+
and videos as media.
|
|
43
|
+
- `tg sessions --terminate` asks before logging a session out, unless `--yes`.
|
|
44
|
+
- `tg` writes an account's directory, and any missing parent, readable only by its owner.
|
|
45
|
+
|
|
10
46
|
## [0.4.0] — 2026-12-01
|
|
11
47
|
|
|
12
48
|
Milestones M3 to M6 of [the roadmap](docs/ROADMAP.md): `rake parity` now finds every public Telethon
|
data/README.md
CHANGED
|
@@ -1,90 +1,62 @@
|
|
|
1
1
|
# telethon.rb
|
|
2
2
|
|
|
3
|
-
A Ruby port of [Telethon](https://codeberg.org/Lonami/Telethon),
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
-
|
|
8
|
-
[Spinel](https://github.com/matz/spinel).
|
|
9
|
-
- 100% of Telethon v1's client methods, events, and custom classes (`rake parity`), oracle-checked
|
|
10
|
-
against Telethon itself — including bots (buttons, callbacks, inline queries, Bot API file ids),
|
|
11
|
-
transports and proxies (SOCKS5, HTTP CONNECT, MTProxy), `MemorySession`/`StringSession`, takeout,
|
|
12
|
-
auto-reconnect, and typed RPC errors.
|
|
13
|
-
- API layer 234 (Telethon v1.46).
|
|
3
|
+
A Ruby port of [Telethon](https://codeberg.org/Lonami/Telethon) (v1.46, layer 234) with a complete `tg` CLI. It runs as a Telegram **user account** (or bot), so it can read history, manage chats, and delete messages of any age — things the Bot API cannot do.
|
|
4
|
+
|
|
5
|
+
- **Zero runtime dependencies:** ships as the `telethon` Ruby gem and as a standalone native `tg` binary compiled with [Spinel](https://github.com/matz/spinel).
|
|
6
|
+
- **100% Telethon v1 API surface (`rake parity`):** 64/64 client methods, 10/10 events, 13/13 custom classes, oracle-checked against Telethon itself.
|
|
7
|
+
- **Constant-memory file transfers:** streams 2 GB uploads and downloads under 192 MB peak RSS in both CRuby and native builds.
|
|
14
8
|
|
|
15
9
|
## Install
|
|
16
10
|
|
|
17
|
-
|
|
11
|
+
Install the standalone `tg` binary (macOS arm64, Linux amd64/arm64 static musl — no Ruby required):
|
|
18
12
|
|
|
19
13
|
```sh
|
|
20
14
|
curl -fsSL https://raw.githubusercontent.com/shogi-dojo/telethon.rb/main/install.sh | sh
|
|
21
15
|
```
|
|
22
16
|
|
|
23
|
-
|
|
24
|
-
(`sh -s -- --dir DIR` for elsewhere, `--version V` for an older release). The Linux binaries are
|
|
25
|
-
static, so they run on Debian, Ubuntu, RHEL, Alpine and the like. The archives are also on the
|
|
26
|
-
[releases page](https://github.com/shogi-dojo/telethon.rb/releases).
|
|
27
|
-
|
|
28
|
-
The library is a gem:
|
|
17
|
+
Or install the Ruby gem (Ruby 3.2+):
|
|
29
18
|
|
|
30
19
|
```sh
|
|
31
20
|
gem install telethon
|
|
32
21
|
```
|
|
33
22
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
| `tg delete CHAT ID... [--yes]` | Delete for everyone |
|
|
50
|
-
| `tg send CHAT TEXT [--topic ID] [--file PATH ...]` | Post as the account |
|
|
51
|
-
| `tg edit CHAT ID TEXT` | Replace a message's text |
|
|
52
|
-
| `tg forward CHAT ID... [--from CHAT] [--delete-source]` | Forward without the "Forwarded from" header |
|
|
53
|
-
| `tg inline BOT QUERY [--pick N] [--to CHAT] [--json]` | Query an inline bot (and optionally send result N) |
|
|
54
|
-
| `tg click CHAT ID BUTTON [--json]` | Click a message's button by index or text |
|
|
55
|
-
|
|
56
|
-
`CHAT` is a numeric id, a `@username`, or `me`. Only `login` and `2fa` prompt, so everything else works over
|
|
57
|
-
`ssh`. The session lives in `$TG_HOME` (default `~/.config/tg`), owner-only: whoever can read it owns
|
|
58
|
-
the account. Set `TG_PROXY` (`socks5://[user:pass@]host:port`, `http://...`, or `mtproxy://secret@host:port`)
|
|
59
|
-
to connect through a proxy. Get an API id and hash at <https://my.telegram.org>.
|
|
60
|
-
|
|
61
|
-
Exit codes: `0` ok, `1` failure, `2` usage error, `3` not logged in, `4` Telegram refused (RPC error), `5` connection failed.
|
|
62
|
-
|
|
63
|
-
## As a library
|
|
23
|
+
See [docs/install.md](docs/install.md) for installer flags, manual SHA-256 verification, and shell completion.
|
|
24
|
+
|
|
25
|
+
## Quick start: `tg` CLI
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
tg login --qr # log in once (stored owner-only in ~/.config/tg)
|
|
29
|
+
tg chats --limit 10 # list recent chats
|
|
30
|
+
tg read me --limit 5 # read Saved Messages
|
|
31
|
+
tg send me "hello from tg" --at +10m # send (or schedule) a message
|
|
32
|
+
tg call help.getNearestDc '{}' # call any raw layer 234 TL method as JSON
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Every command supports `--json` for scripts; `tg help COMMAND` prints full usage.
|
|
36
|
+
|
|
37
|
+
## Quick start: Ruby library
|
|
64
38
|
|
|
65
39
|
```ruby
|
|
66
40
|
require "telethon"
|
|
67
41
|
|
|
68
|
-
|
|
69
|
-
client = Telethon::Client.new(session, api_id: 12345, api_hash: "...")
|
|
42
|
+
client = Telethon::Client.new(File.expand_path("~/.config/tg"), api_id: 12345, api_hash: "0123456789abcdef0123456789abcdef")
|
|
70
43
|
client.connect
|
|
71
|
-
|
|
72
|
-
client.iter_messages("@telegram", limit: 10) { |m| puts "#{m.sender_id}: #{m.text}" }
|
|
44
|
+
client.iter_messages("@example_channel", limit: 10) { |m| puts "#{m.id}: #{m.text}" }
|
|
73
45
|
client.send_message("me", "**bold** and __italic__")
|
|
74
|
-
client.
|
|
75
|
-
|
|
76
|
-
client.on(Telethon::Events::NewMessage.new(pattern: /\A\/start/)) { |e| e.reply("hello") }
|
|
46
|
+
client.on(Telethon::Events::NewMessage.new(pattern: /\A\/ping\b/)) { |e| e.reply("pong") }
|
|
77
47
|
client.run_until_disconnected
|
|
78
48
|
```
|
|
79
49
|
|
|
80
|
-
|
|
81
|
-
Where the port differs, [DEVELOPING.md](DEVELOPING.md) says so. Any layer function can be called
|
|
82
|
-
with `client.call(Telethon::TL.make("messages.getHistory", ...))`.
|
|
83
|
-
|
|
84
|
-
## Development
|
|
50
|
+
## Documentation
|
|
85
51
|
|
|
86
|
-
|
|
87
|
-
|
|
52
|
+
- [Documentation Index](docs/README.md)
|
|
53
|
+
- [Installation](docs/install.md)
|
|
54
|
+
- [Getting Started](docs/getting-started.md)
|
|
55
|
+
- [`tg` CLI Reference](docs/cli.md) (45 commands, generated from the command table)
|
|
56
|
+
- [Ruby Library Guide](docs/library.md) (with frozen 1.0 API surface)
|
|
57
|
+
- [Differences from Python Telethon](docs/telethon-differences.md)
|
|
58
|
+
- [Troubleshooting](docs/troubleshooting.md)
|
|
59
|
+
- [Roadmap](docs/ROADMAP.md) and [Developing](DEVELOPING.md)
|
|
88
60
|
|
|
89
61
|
## License
|
|
90
62
|
|
data/lib/telethon/cli/args.rb
CHANGED
|
@@ -69,6 +69,39 @@ module Telethon
|
|
|
69
69
|
|
|
70
70
|
value.to_i
|
|
71
71
|
end
|
|
72
|
+
|
|
73
|
+
# Parses a Unix timestamp, relative offset (+30s, +10m, +2h, +1d), or ISO-8601 date/time.
|
|
74
|
+
def parse_time(value, what = "time")
|
|
75
|
+
raw = value.to_s.strip
|
|
76
|
+
return raw.to_i if raw.match?(/\A\d+\z/)
|
|
77
|
+
|
|
78
|
+
if (m = raw.match(/\A\+(\d+)([smhd])\z/i))
|
|
79
|
+
mult = case m[2].downcase
|
|
80
|
+
when "s" then 1
|
|
81
|
+
when "m" then 60
|
|
82
|
+
when "h" then 3600
|
|
83
|
+
else 86_400
|
|
84
|
+
end
|
|
85
|
+
return Time.now.to_i + (m[1].to_i * mult)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
if (m = raw.match(/\A(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?(Z|[+-]\d{2}:?\d{2})?\z/))
|
|
89
|
+
offset = 0
|
|
90
|
+
tz = m[7]
|
|
91
|
+
if tz && tz != "Z"
|
|
92
|
+
sign = tz.start_with?("-") ? -1 : 1
|
|
93
|
+
digits = tz[1..].delete(":")
|
|
94
|
+
offset = sign * ((digits[0, 2].to_i * 3600) + (digits[2, 2].to_i * 60))
|
|
95
|
+
end
|
|
96
|
+
begin
|
|
97
|
+
return Time.utc(m[1].to_i, m[2].to_i, m[3].to_i, m[4].to_i, m[5].to_i, m[6].to_i).to_i - offset
|
|
98
|
+
rescue ArgumentError
|
|
99
|
+
# fall through to UsageError
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
raise UsageError, "invalid #{what}: #{value} (use ISO-8601, Unix timestamp, or +10m/+2h/+1d)"
|
|
104
|
+
end
|
|
72
105
|
end
|
|
73
106
|
end
|
|
74
107
|
end
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
module CLI
|
|
5
|
+
# Builds any TL layer function call from JSON arguments (resolving peer strings and decoding base64 bytes)
|
|
6
|
+
# and converts TL responses back into plain JSON-compatible Ruby values.
|
|
7
|
+
module Call
|
|
8
|
+
BASE64_RE = %r{\A[A-Za-z0-9+/=_-]*\z}
|
|
9
|
+
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def build_request(method_name, json_text, client)
|
|
13
|
+
ctor = TL.schema.named(method_name)
|
|
14
|
+
unless ctor && ctor.function
|
|
15
|
+
raise UsageError, "unknown TL method #{method_name.inspect} in layer #{TL::LAYER}"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
raw = parse_json_object(json_text)
|
|
19
|
+
build_object(ctor, raw, client)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def parse_json_object(json_text)
|
|
23
|
+
text = json_text.nil? || json_text.empty? ? "{}" : json_text
|
|
24
|
+
parsed = begin
|
|
25
|
+
JSON.parse(text)
|
|
26
|
+
rescue JSON::ParserError => e
|
|
27
|
+
raise UsageError, "invalid JSON arguments: #{e.message}"
|
|
28
|
+
end
|
|
29
|
+
raise UsageError, "JSON arguments must be an object ({...})" unless parsed.is_a?(Hash)
|
|
30
|
+
|
|
31
|
+
parsed
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def build_object(ctor, hash, client)
|
|
35
|
+
param_map = {}
|
|
36
|
+
ctor.params.each { |p| param_map[p.name] = p unless p.flags? }
|
|
37
|
+
|
|
38
|
+
hash.each_key do |key|
|
|
39
|
+
raise UsageError, "unknown field #{key.inspect} for #{ctor.name}" unless param_map.key?(key)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
fields = {}
|
|
43
|
+
ctor.params.each do |param|
|
|
44
|
+
next if param.flags?
|
|
45
|
+
|
|
46
|
+
where = "#{ctor.name}.#{param.name}"
|
|
47
|
+
if hash.key?(param.name) && !hash[param.name].nil?
|
|
48
|
+
fields[param.name] = coerce(param.type, hash[param.name], where, client)
|
|
49
|
+
elsif !param.optional? && param.type != "true"
|
|
50
|
+
fields[param.name] = default_value(param, where, client)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
TL.make(ctor.name, fields)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def default_value(param, where, client)
|
|
57
|
+
case param.type
|
|
58
|
+
when "int" then 0
|
|
59
|
+
when "long" then param.name == "random_id" ? client.random_id : 0
|
|
60
|
+
when "double" then 0.0
|
|
61
|
+
when "Bool" then false
|
|
62
|
+
when "string" then ""
|
|
63
|
+
when "bytes" then "".b
|
|
64
|
+
else
|
|
65
|
+
if param.type.start_with?("Vector<") || param.type.start_with?("vector<")
|
|
66
|
+
[]
|
|
67
|
+
else
|
|
68
|
+
raise UsageError, "missing required field #{where} (#{param.type})"
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def coerce(type, value, where, client)
|
|
74
|
+
case type
|
|
75
|
+
when "int", "long"
|
|
76
|
+
raise UsageError, "#{where} must be an integer" unless value.is_a?(Integer)
|
|
77
|
+
|
|
78
|
+
value
|
|
79
|
+
when "double"
|
|
80
|
+
raise UsageError, "#{where} must be a number" unless value.is_a?(Integer) || value.is_a?(Float)
|
|
81
|
+
|
|
82
|
+
value.to_f
|
|
83
|
+
when "Bool", "true"
|
|
84
|
+
value ? true : false
|
|
85
|
+
when "string"
|
|
86
|
+
value.to_s
|
|
87
|
+
when "bytes", "int128", "int256"
|
|
88
|
+
bytes = decode_base64(value, where)
|
|
89
|
+
if type == "int128" && bytes.bytesize != 16
|
|
90
|
+
raise UsageError, "#{where} must decode to 16 bytes"
|
|
91
|
+
elsif type == "int256" && bytes.bytesize != 32
|
|
92
|
+
raise UsageError, "#{where} must decode to 32 bytes"
|
|
93
|
+
end
|
|
94
|
+
bytes
|
|
95
|
+
when "InputPeer"
|
|
96
|
+
return client.get_input_entity(value) if value.is_a?(String) || value.is_a?(Integer)
|
|
97
|
+
|
|
98
|
+
coerce_object(value, type, where, client)
|
|
99
|
+
when "InputChannel"
|
|
100
|
+
if value.is_a?(String) || value.is_a?(Integer)
|
|
101
|
+
peer = client.get_input_entity(value)
|
|
102
|
+
unless Client.entity_type(peer) == :channel
|
|
103
|
+
raise UsageError, "#{where}: #{value.inspect} is not a channel"
|
|
104
|
+
end
|
|
105
|
+
return TL.make("inputChannel", "channel_id" => peer["channel_id"], "access_hash" => peer["access_hash"])
|
|
106
|
+
end
|
|
107
|
+
coerce_object(value, type, where, client)
|
|
108
|
+
when "InputUser"
|
|
109
|
+
if value.is_a?(String) || value.is_a?(Integer)
|
|
110
|
+
peer = client.get_input_entity(value)
|
|
111
|
+
begin
|
|
112
|
+
return Client.input_user(peer)
|
|
113
|
+
rescue Error => e
|
|
114
|
+
raise UsageError, "#{where}: #{e.message}"
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
coerce_object(value, type, where, client)
|
|
118
|
+
else
|
|
119
|
+
if type.start_with?("Vector<") || type.start_with?("vector<")
|
|
120
|
+
raise UsageError, "#{where} must be a JSON array" unless value.is_a?(Array)
|
|
121
|
+
|
|
122
|
+
inner = type[7, type.size - 8]
|
|
123
|
+
value.map { |item| coerce(inner, item, where, client) }
|
|
124
|
+
else
|
|
125
|
+
coerce_object(value, type, where, client)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def coerce_object(value, expected_type, where, client)
|
|
131
|
+
if value.is_a?(String)
|
|
132
|
+
ctor = TL.schema.named(value)
|
|
133
|
+
raise UsageError, "#{where}: unknown TL constructor #{value.inspect}" unless ctor && !ctor.function
|
|
134
|
+
|
|
135
|
+
return build_object(ctor, {}, client)
|
|
136
|
+
end
|
|
137
|
+
raise UsageError, "#{where} must be a JSON object with \"_\" constructor name" unless value.is_a?(Hash)
|
|
138
|
+
|
|
139
|
+
name = value["_"] || (TL.bare?(expected_type) ? expected_type : nil)
|
|
140
|
+
raise UsageError, "#{where} is missing \"_\" constructor name" unless name.is_a?(String)
|
|
141
|
+
|
|
142
|
+
ctor = TL.schema.named(name)
|
|
143
|
+
raise UsageError, "#{where}: unknown TL constructor #{name.inspect}" unless ctor
|
|
144
|
+
|
|
145
|
+
fields = {}
|
|
146
|
+
value.each { |k, v| fields[k] = v unless k == "_" }
|
|
147
|
+
build_object(ctor, fields, client)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def decode_base64(value, where)
|
|
151
|
+
raise UsageError, "#{where} must be a base64 string" unless value.is_a?(String)
|
|
152
|
+
|
|
153
|
+
cleaned = value.delete(" \t\r\n")
|
|
154
|
+
raise UsageError, "#{where} is not valid base64" unless cleaned.match?(BASE64_RE)
|
|
155
|
+
|
|
156
|
+
std = cleaned.tr("-_", "+/")
|
|
157
|
+
pad = (4 - (std.bytesize % 4)) % 4
|
|
158
|
+
std = "#{std}#{'=' * pad}" if pad.positive?
|
|
159
|
+
std.unpack1("m0")
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def to_json_value(value)
|
|
163
|
+
case value
|
|
164
|
+
when TL::Object
|
|
165
|
+
out = { "_" => value.name }
|
|
166
|
+
value.fields.each { |k, v| out[k] = to_json_value(v) }
|
|
167
|
+
out
|
|
168
|
+
when Array
|
|
169
|
+
value.map { |v| to_json_value(v) }
|
|
170
|
+
when String
|
|
171
|
+
if value.encoding == Encoding::BINARY || !value.valid_encoding?
|
|
172
|
+
[value].pack("m0")
|
|
173
|
+
else
|
|
174
|
+
value
|
|
175
|
+
end
|
|
176
|
+
else
|
|
177
|
+
value
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|