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
data/lib/telethon/cli.rb
CHANGED
|
@@ -20,7 +20,8 @@ module Telethon
|
|
|
20
20
|
# tg forward CHAT ID [ID ...] [--from CHAT] [--delete-source]
|
|
21
21
|
#
|
|
22
22
|
# CHAT is a numeric id (-1001874396477), a @username, or "me". The session is the whole account, so
|
|
23
|
-
# it is kept readable by its owner only, in $TG_HOME (else $DOJO_TG_HOME, else ~/.config/tg)
|
|
23
|
+
# it is kept readable by its owner only, in $TG_HOME (else $DOJO_TG_HOME, else ~/.config/tg), or for
|
|
24
|
+
# `--account NAME` (or $TG_ACCOUNT) in its accounts/NAME directory. Every
|
|
24
25
|
# command but login refuses to prompt, so it can run over a non-interactive ssh.
|
|
25
26
|
module CLI
|
|
26
27
|
class Failure < StandardError
|
|
@@ -36,45 +37,7 @@ module Telethon
|
|
|
36
37
|
EXIT_RPC_ERROR = 4
|
|
37
38
|
EXIT_CONNECTION = 5
|
|
38
39
|
|
|
39
|
-
USAGE =
|
|
40
|
-
usage: tg COMMAND [ARGS]
|
|
41
|
-
|
|
42
|
-
commands:
|
|
43
|
-
login [--qr | --bot-token] log in interactively (or as a bot via TG_BOT_TOKEN/stdin)
|
|
44
|
-
logout [--json] log out and remove the saved session
|
|
45
|
-
2fa set|remove [--hint H] [--json]
|
|
46
|
-
set, change, or remove the account's 2FA password
|
|
47
|
-
import-session [FILE | --string]
|
|
48
|
-
take over a Telethon .session or StringSession (via TG_SESSION_STRING/stdin)
|
|
49
|
-
export-session print the current session as a Telethon StringSession (stdout only)
|
|
50
|
-
whoami show the logged-in account
|
|
51
|
-
topics CHAT list a forum's topics
|
|
52
|
-
read CHAT [--limit N] [--topic ID] [--json]
|
|
53
|
-
human-readable message history
|
|
54
|
-
search [CHAT] QUERY [--from USER] [--filter KIND] [--limit N] [--json]
|
|
55
|
-
search messages in a chat or globally
|
|
56
|
-
export CHAT [--topic ID] [--limit N | --all] [--before ID] [--out FILE]
|
|
57
|
-
newest messages as JSON
|
|
58
|
-
download CHAT ID... [--out DIR] save messages' attachments (owner-only files)
|
|
59
|
-
delete CHAT ID... [--yes] delete messages for everyone
|
|
60
|
-
send CHAT TEXT [--topic ID] [--file PATH]...
|
|
61
|
-
send a message; with --file, documents with TEXT as caption
|
|
62
|
-
edit CHAT ID TEXT replace the text (or caption) of a message you sent
|
|
63
|
-
forward CHAT ID... [--from CHAT] [--delete-source]
|
|
64
|
-
forward without the "Forwarded from" header (default from: me)
|
|
65
|
-
inline BOT QUERY [--pick N] [--to CHAT] [--json]
|
|
66
|
-
query an inline bot, optionally sending result N
|
|
67
|
-
click CHAT ID BUTTON [--json] click a message's button by index or text
|
|
68
|
-
--version
|
|
69
|
-
|
|
70
|
-
exit codes:
|
|
71
|
-
0 ok
|
|
72
|
-
1 failure
|
|
73
|
-
2 usage error
|
|
74
|
-
3 not logged in
|
|
75
|
-
4 Telegram refused (RPC error)
|
|
76
|
-
5 connection failed
|
|
77
|
-
TEXT
|
|
40
|
+
USAGE = Table.format_help
|
|
78
41
|
|
|
79
42
|
module_function
|
|
80
43
|
|
|
@@ -85,20 +48,120 @@ module Telethon
|
|
|
85
48
|
dir
|
|
86
49
|
end
|
|
87
50
|
|
|
51
|
+
ACCOUNT_NAME = /\A[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}\z/
|
|
52
|
+
|
|
53
|
+
# A named account lives in HOME/accounts/NAME; the default one in HOME itself, as before.
|
|
54
|
+
def account_home(base, account)
|
|
55
|
+
name = resolve_account(base, account)
|
|
56
|
+
name ? File.join(base, "accounts", name) : base
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# The saved account that --account means, as its directory name, or nil for the default account.
|
|
60
|
+
# It may be a name, "default", the @username, or the numeric user id of a saved account.
|
|
61
|
+
def resolve_account(base, account)
|
|
62
|
+
return nil if account.nil? || account.empty? || account == "default"
|
|
63
|
+
return account if account.match?(ACCOUNT_NAME) && File.directory?(File.join(base, "accounts", account))
|
|
64
|
+
|
|
65
|
+
by_identity = account.start_with?("@") || account.match?(/\A\d+\z/)
|
|
66
|
+
raise UsageError, "bad account name #{account.inspect} (letters, digits, _ . -)" unless by_identity || account.match?(ACCOUNT_NAME)
|
|
67
|
+
return account unless by_identity # a new name: `tg --account NAME login` makes it
|
|
68
|
+
|
|
69
|
+
wanted = account.delete_prefix("@").downcase
|
|
70
|
+
found = accounts(base).find do |item|
|
|
71
|
+
item["id"].to_s == account || (item["username"] && item["username"].downcase == wanted)
|
|
72
|
+
end
|
|
73
|
+
raise NotLoggedIn, "No saved account is #{account}: see `tg accounts`." unless found
|
|
74
|
+
|
|
75
|
+
found["name"] == "default" ? nil : found["name"]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# The accounts saved under base, read from disk only: the default one (if set up) and each
|
|
79
|
+
# accounts/NAME, as {"name", "dir", "username", "id", "logged_in"}.
|
|
80
|
+
def accounts(base)
|
|
81
|
+
items = []
|
|
82
|
+
items << account_entry("default", base) if File.exist?(File.join(base, "config.json"))
|
|
83
|
+
dir = File.join(base, "accounts")
|
|
84
|
+
if File.directory?(dir)
|
|
85
|
+
Dir.children(dir).sort.each do |name|
|
|
86
|
+
path = File.join(dir, name)
|
|
87
|
+
items << account_entry(name, path) if File.directory?(path) && name.match?(ACCOUNT_NAME)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
items
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def account_entry(name, dir)
|
|
94
|
+
config = read_json(File.join(dir, "config.json"))
|
|
95
|
+
session = read_json(File.join(dir, "session.json"))
|
|
96
|
+
user_id = session["user_id"]
|
|
97
|
+
# A session file alone is not a login: tg writes one as soon as it connects. Signing in records the
|
|
98
|
+
# user id in it (and login the username in config.json).
|
|
99
|
+
logged_in = File.exist?(File.join(dir, "session.json")) && (!user_id.nil? || !config["account"].nil?)
|
|
100
|
+
{ "name" => name, "dir" => dir, "username" => config["account"], "id" => user_id, "logged_in" => logged_in }
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def read_json(path)
|
|
104
|
+
File.exist?(path) ? JSON.parse(File.read(path, encoding: "UTF-8")) : {}
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Takes --account NAME (or --account=NAME) out of argv, before any `--`; else $TG_ACCOUNT.
|
|
108
|
+
def extract_account(args)
|
|
109
|
+
account = nil
|
|
110
|
+
i = 0
|
|
111
|
+
while i < args.size
|
|
112
|
+
arg = args[i]
|
|
113
|
+
break if arg == "--"
|
|
114
|
+
|
|
115
|
+
if arg == "--account"
|
|
116
|
+
raise UsageError, "--account needs a value" if i + 1 >= args.size
|
|
117
|
+
|
|
118
|
+
account = args[i + 1]
|
|
119
|
+
args.slice!(i, 2)
|
|
120
|
+
elsif arg.start_with?("--account=")
|
|
121
|
+
account = arg.split("=", 2)[1]
|
|
122
|
+
args.slice!(i, 1)
|
|
123
|
+
else
|
|
124
|
+
i += 1
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
account = ENV.fetch("TG_ACCOUNT", "").strip if account.nil?
|
|
128
|
+
account.empty? ? nil : account
|
|
129
|
+
end
|
|
130
|
+
|
|
88
131
|
# Answers the exit status. client_factory makes the connected client from the config; tests replace it.
|
|
89
132
|
def run(argv, client_factory: nil, input: $stdin, out: $stdout, err: $stderr)
|
|
90
133
|
args = argv.dup
|
|
134
|
+
account = extract_account(args)
|
|
91
135
|
command = args.shift
|
|
92
|
-
if command.nil? || %w[-h --help
|
|
93
|
-
out.print
|
|
94
|
-
return command.nil? ?
|
|
136
|
+
if command.nil? || %w[-h --help].include?(command)
|
|
137
|
+
out.print Table.format_help
|
|
138
|
+
return command.nil? ? EXIT_USAGE : EXIT_OK
|
|
139
|
+
end
|
|
140
|
+
if command == "help"
|
|
141
|
+
if args.empty?
|
|
142
|
+
out.print Table.format_help
|
|
143
|
+
return EXIT_OK
|
|
144
|
+
end
|
|
145
|
+
entry = Table.find(args[0])
|
|
146
|
+
unless entry
|
|
147
|
+
err.puts "tg: unknown command #{args[0]} (see tg --help)"
|
|
148
|
+
return EXIT_USAGE
|
|
149
|
+
end
|
|
150
|
+
out.print Table.format_command_help(entry)
|
|
151
|
+
return EXIT_OK
|
|
95
152
|
end
|
|
96
153
|
if %w[--version -v].include?(command)
|
|
97
154
|
out.puts "tg #{VERSION} (telethon.rb, a port of #{UPSTREAM}, layer #{TL::LAYER})"
|
|
98
|
-
return
|
|
155
|
+
return EXIT_OK
|
|
156
|
+
end
|
|
157
|
+
if (args == ["--help"] || args == ["-h"]) && (entry = Table.find(command))
|
|
158
|
+
out.print Table.format_command_help(entry)
|
|
159
|
+
return EXIT_OK
|
|
99
160
|
end
|
|
100
161
|
begin
|
|
101
|
-
|
|
162
|
+
account = resolve_account(home, account)
|
|
163
|
+
Commands.new(account_home(home, account), client_factory, input, out, err,
|
|
164
|
+
base_home: home, account: account).dispatch(command, args)
|
|
102
165
|
ensure
|
|
103
166
|
out.flush # what a command printed comes before its failure on stderr, even when piped
|
|
104
167
|
end
|
data/lib/telethon/client/auth.rb
CHANGED
|
@@ -110,7 +110,8 @@ module Telethon
|
|
|
110
110
|
end
|
|
111
111
|
|
|
112
112
|
# Answers auth.sentCode; phone_code_hash is in it.
|
|
113
|
-
def send_code(phone)
|
|
113
|
+
def send_code(phone, force_sms: false)
|
|
114
|
+
_ = force_sms
|
|
114
115
|
call(TL.make("auth.sendCode", "phone_number" => phone, "api_id" => @api_id, "api_hash" => @api_hash,
|
|
115
116
|
"settings" => TL.make("codeSettings")))
|
|
116
117
|
end
|
|
@@ -154,8 +155,9 @@ module Telethon
|
|
|
154
155
|
end
|
|
155
156
|
|
|
156
157
|
# Starts a QR login: show qr.url as a QR code, then qr.wait.
|
|
157
|
-
def qr_login(except_ids =
|
|
158
|
-
|
|
158
|
+
def qr_login(except_ids = nil, ignored_ids: nil)
|
|
159
|
+
ids = except_ids || ignored_ids || []
|
|
160
|
+
QRLogin.new(self, ids).recreate
|
|
159
161
|
end
|
|
160
162
|
|
|
161
163
|
# Logs out of Telegram (auth.logOut), disconnects, and removes the session files.
|
|
@@ -217,7 +219,7 @@ module Telethon
|
|
|
217
219
|
|
|
218
220
|
# The logged-in user. Remembers its id in the session when it did not have it (an imported
|
|
219
221
|
# Telethon session does not), as Telethon keeps its own: Message#out? and events need it.
|
|
220
|
-
def get_me
|
|
222
|
+
def get_me(input_peer: false)
|
|
221
223
|
user = call(TL.make("users.getUsers", "id" => [TL.make("inputUserSelf")]))[0]
|
|
222
224
|
if user && user.is?("user")
|
|
223
225
|
bot = user["bot"] ? true : false
|
|
@@ -227,6 +229,8 @@ module Telethon
|
|
|
227
229
|
@session.save
|
|
228
230
|
end
|
|
229
231
|
end
|
|
232
|
+
return TL.make("inputPeerUser", "user_id" => user["id"], "access_hash" => user["access_hash"]) if input_peer && user
|
|
233
|
+
|
|
230
234
|
user
|
|
231
235
|
end
|
|
232
236
|
|
|
@@ -268,6 +272,10 @@ module Telethon
|
|
|
268
272
|
self
|
|
269
273
|
end
|
|
270
274
|
|
|
275
|
+
def token
|
|
276
|
+
@response && @response["token"]
|
|
277
|
+
end
|
|
278
|
+
|
|
271
279
|
def url
|
|
272
280
|
Client.login_url(@response["token"])
|
|
273
281
|
end
|
|
@@ -143,8 +143,9 @@ module Telethon
|
|
|
143
143
|
# for it. file is a path, or an IO (readpartial; name or path when it has one). part_size_kb
|
|
144
144
|
# is from the size unless given (at most 512). key and iv encrypt each part with AES-IGE, as Telethon
|
|
145
145
|
# does. An inputFile given is answered as it is.
|
|
146
|
-
def upload_file(file, part_size_kb: nil, file_size: nil, file_name: nil,
|
|
147
|
-
iv: nil)
|
|
146
|
+
def upload_file(file, part_size_kb: nil, file_size: nil, file_name: nil, use_cache: nil,
|
|
147
|
+
progress_callback: nil, key: nil, iv: nil)
|
|
148
|
+
_ = use_cache
|
|
148
149
|
return file if file.is_a?(TL::Object) && (file.is?("inputFile") || file.is?("inputFileBig"))
|
|
149
150
|
|
|
150
151
|
if file.is_a?(String)
|
|
@@ -217,15 +218,20 @@ module Telethon
|
|
|
217
218
|
# attributes of the same kind. Answers the sent Message, or the Messages for a list.
|
|
218
219
|
def send_file(entity, file, caption: nil, force_document: false, mime_type: nil, file_size: nil,
|
|
219
220
|
clear_draft: false, progress_callback: nil, reply_to: nil, attributes: nil, thumb: nil,
|
|
220
|
-
parse_mode: :default, formatting_entities: nil, voice_note: false,
|
|
221
|
-
buttons: nil, silent: nil, background: nil, supports_streaming: false,
|
|
222
|
-
|
|
221
|
+
allow_cache: true, parse_mode: :default, formatting_entities: nil, voice_note: false,
|
|
222
|
+
video_note: false, buttons: nil, silent: nil, background: nil, supports_streaming: false,
|
|
223
|
+
schedule: nil, comment_to: nil, ttl: nil, nosound_video: nil, send_as: nil,
|
|
224
|
+
message_effect_id: nil)
|
|
225
|
+
_ = allow_cache
|
|
223
226
|
raise ArgumentError, "Cannot use #{file.inspect} as file" if file.nil? || (file.is_a?(Array) && file.empty?)
|
|
224
227
|
|
|
225
228
|
caption = "" if caption.nil? || (caption.is_a?(Array) && caption.empty?)
|
|
226
229
|
formatting_entities ||= []
|
|
227
230
|
peer = get_input_entity(entity)
|
|
228
231
|
reply_to = reply_to.id if reply_to.is_a?(Message)
|
|
232
|
+
if comment_to
|
|
233
|
+
peer, reply_to = get_comment_data(peer, comment_to)
|
|
234
|
+
end
|
|
229
235
|
schedule = schedule.to_i if schedule.is_a?(Time)
|
|
230
236
|
sender = send_as && get_input_entity(send_as)
|
|
231
237
|
if file.is_a?(Array)
|
|
@@ -398,15 +398,43 @@ module Telethon
|
|
|
398
398
|
# Sends text, formatted in parse_mode (:default for the client's, markdown unless changed), or
|
|
399
399
|
# with formatting_entities as they are. reply_to is a message id, or a forum topic's. Answers the
|
|
400
400
|
# sent Message.
|
|
401
|
-
def send_message(entity, text, reply_to: nil,
|
|
402
|
-
|
|
401
|
+
def send_message(entity, text = "", reply_to: nil, attributes: nil, parse_mode: :default,
|
|
402
|
+
formatting_entities: nil, link_preview: true, file: nil, thumb: nil, force_document: false,
|
|
403
|
+
clear_draft: false, buttons: nil, silent: nil, background: nil, supports_streaming: false,
|
|
404
|
+
schedule: nil, schedule_repeat_period: nil, comment_to: nil, nosound_video: nil,
|
|
405
|
+
send_as: nil, message_effect_id: nil, noforwards: false, update_stickersets_order: false,
|
|
406
|
+
invert_media: false, allow_paid_floodskip: false, quick_reply_shortcut: nil,
|
|
407
|
+
allow_paid_stars: nil, suggested_post: nil)
|
|
408
|
+
if file
|
|
409
|
+
return send_file(entity, file, caption: text, force_document: force_document, clear_draft: clear_draft,
|
|
410
|
+
reply_to: reply_to, attributes: attributes, thumb: thumb,
|
|
411
|
+
parse_mode: parse_mode, formatting_entities: formatting_entities,
|
|
412
|
+
buttons: buttons, silent: silent, background: background,
|
|
413
|
+
supports_streaming: supports_streaming, schedule: schedule,
|
|
414
|
+
nosound_video: nosound_video, send_as: send_as,
|
|
415
|
+
message_effect_id: message_effect_id, comment_to: comment_to)
|
|
416
|
+
end
|
|
417
|
+
|
|
403
418
|
peer = input_peer_for(entity)
|
|
419
|
+
reply_to = reply_to.id if reply_to.is_a?(Message)
|
|
420
|
+
if comment_to
|
|
421
|
+
peer, reply_to = get_comment_data(peer, comment_to)
|
|
422
|
+
end
|
|
404
423
|
message, entities = formatting_entities ? [text, formatting_entities] : parse_message_text(text, parse_mode)
|
|
405
424
|
raise ArgumentError, "The message cannot be empty unless a file is provided" if message.nil? || message.empty?
|
|
406
425
|
|
|
426
|
+
schedule = schedule.to_i if schedule.is_a?(Time)
|
|
427
|
+
sender = send_as && get_input_entity(send_as)
|
|
407
428
|
fields = { "peer" => peer, "message" => message, "random_id" => random_id, "entities" => entities,
|
|
408
429
|
"no_webpage" => !link_preview, "clear_draft" => clear_draft, "noforwards" => noforwards,
|
|
409
|
-
"silent" => silent ? true : false, "
|
|
430
|
+
"silent" => silent ? true : false, "background" => background ? true : false,
|
|
431
|
+
"update_stickersets_order" => update_stickersets_order ? true : false,
|
|
432
|
+
"invert_media" => invert_media ? true : false,
|
|
433
|
+
"allow_paid_floodskip" => allow_paid_floodskip ? true : false,
|
|
434
|
+
"reply_markup" => build_reply_markup(buttons), "schedule_date" => schedule,
|
|
435
|
+
"schedule_repeat_period" => schedule_repeat_period, "send_as" => sender,
|
|
436
|
+
"quick_reply_shortcut" => quick_reply_shortcut, "effect" => message_effect_id,
|
|
437
|
+
"allow_paid_stars" => allow_paid_stars, "suggested_post" => suggested_post }
|
|
410
438
|
fields["reply_to"] = TL.make("inputReplyToMessage", "reply_to_msg_id" => reply_to) if reply_to
|
|
411
439
|
request = TL.make("messages.sendMessage", fields)
|
|
412
440
|
result = call(request)
|
|
@@ -420,24 +448,57 @@ module Telethon
|
|
|
420
448
|
response_message(request, result, peer)
|
|
421
449
|
end
|
|
422
450
|
|
|
423
|
-
# Replaces a message's text (in parse_mode, as send_message). Answers the edited Message.
|
|
424
|
-
def edit_message(entity, id, text, parse_mode: :default,
|
|
425
|
-
buttons: nil
|
|
451
|
+
# Replaces a message's text or media (in parse_mode, as send_message). Answers the edited Message.
|
|
452
|
+
def edit_message(entity, id, text = nil, parse_mode: :default, attributes: nil, formatting_entities: nil,
|
|
453
|
+
link_preview: true, file: nil, thumb: nil, force_document: false, buttons: nil,
|
|
454
|
+
supports_streaming: false, schedule: nil)
|
|
426
455
|
peer = input_peer_for(entity)
|
|
427
|
-
|
|
456
|
+
id = id.id if id.is_a?(Message)
|
|
457
|
+
message, entities = if text.nil?
|
|
458
|
+
[nil, nil]
|
|
459
|
+
elsif formatting_entities
|
|
460
|
+
[text, formatting_entities]
|
|
461
|
+
else
|
|
462
|
+
parse_message_text(text, parse_mode)
|
|
463
|
+
end
|
|
464
|
+
media = if file
|
|
465
|
+
file_to_media(file, force_document, nil, nil, attributes, thumb, false, false,
|
|
466
|
+
supports_streaming, nil, nil, nil)
|
|
467
|
+
end
|
|
468
|
+
schedule = schedule.to_i if schedule.is_a?(Time)
|
|
428
469
|
request = TL.make("messages.editMessage", "peer" => peer, "id" => id, "message" => message,
|
|
429
|
-
"
|
|
430
|
-
"reply_markup" => build_reply_markup(buttons)
|
|
470
|
+
"media" => media, "no_webpage" => !link_preview,
|
|
471
|
+
"entities" => entities, "reply_markup" => build_reply_markup(buttons),
|
|
472
|
+
"schedule_date" => schedule)
|
|
431
473
|
response_message(request, call(request), peer)
|
|
432
474
|
end
|
|
433
475
|
|
|
434
476
|
# Forwards without the "Forwarded from" header when drop_author. Answers the new Messages, in order
|
|
435
477
|
# (nil for any Telegram did not forward).
|
|
436
|
-
def forward_messages(to_entity, ids, from_entity,
|
|
478
|
+
def forward_messages(to_entity, ids, from_entity, silent: nil, background: nil, with_my_score: nil,
|
|
479
|
+
drop_author: false, drop_media_captions: false, noforwards: false,
|
|
480
|
+
allow_paid_floodskip: false, top_msg_id: nil, reply_to: nil, as_album: nil,
|
|
481
|
+
schedule: nil, schedule_repeat_period: nil, send_as: nil, quick_reply_shortcut: nil,
|
|
482
|
+
message_effect_id: nil, video_timestamp: nil, allow_paid_stars: nil, suggested_post: nil)
|
|
483
|
+
_ = as_album
|
|
437
484
|
peer = input_peer_for(to_entity)
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
485
|
+
schedule = schedule.to_i if schedule.is_a?(Time)
|
|
486
|
+
sender = send_as && get_input_entity(send_as)
|
|
487
|
+
reply_id = reply_to.is_a?(Message) ? reply_to.id : reply_to
|
|
488
|
+
fields = { "from_peer" => input_peer_for(from_entity), "id" => ids, "to_peer" => peer,
|
|
489
|
+
"random_id" => Array.new(ids.size) { random_long },
|
|
490
|
+
"drop_author" => drop_author ? true : nil, "silent" => silent ? true : nil,
|
|
491
|
+
"background" => background ? true : nil, "with_my_score" => with_my_score ? true : nil,
|
|
492
|
+
"drop_media_captions" => drop_media_captions ? true : nil,
|
|
493
|
+
"noforwards" => noforwards ? true : nil,
|
|
494
|
+
"allow_paid_floodskip" => allow_paid_floodskip ? true : nil,
|
|
495
|
+
"top_msg_id" => top_msg_id, "schedule_date" => schedule,
|
|
496
|
+
"schedule_repeat_period" => schedule_repeat_period, "send_as" => sender,
|
|
497
|
+
"quick_reply_shortcut" => quick_reply_shortcut, "effect" => message_effect_id,
|
|
498
|
+
"video_timestamp" => video_timestamp, "allow_paid_stars" => allow_paid_stars,
|
|
499
|
+
"suggested_post" => suggested_post }
|
|
500
|
+
fields["reply_to"] = TL.make("inputReplyToMessage", "reply_to_msg_id" => reply_id) if reply_id
|
|
501
|
+
request = TL.make("messages.forwardMessages", fields)
|
|
441
502
|
response_messages(request, call(request), peer)
|
|
442
503
|
end
|
|
443
504
|
|
|
@@ -467,6 +528,14 @@ module Telethon
|
|
|
467
528
|
|
|
468
529
|
private
|
|
469
530
|
|
|
531
|
+
def get_comment_data(peer, comment_to)
|
|
532
|
+
msg_id = comment_to.is_a?(Message) ? comment_to.id : comment_to
|
|
533
|
+
res = call(TL.make("messages.getDiscussionMessage", "peer" => peer, "msg_id" => msg_id))
|
|
534
|
+
msg = res["messages"][0]
|
|
535
|
+
discussion = res["chats"].find { |c| Client.peer_id(c) == Client.peer_id(msg["peer_id"]) }
|
|
536
|
+
[discussion ? get_input_entity(discussion) : peer, msg["id"]]
|
|
537
|
+
end
|
|
538
|
+
|
|
470
539
|
def pin(entity, id, unpin, notify, pm_oneside)
|
|
471
540
|
peer = input_peer_for(entity)
|
|
472
541
|
if id.nil? || id <= 0
|
|
@@ -133,6 +133,11 @@ module Telethon
|
|
|
133
133
|
"folder_peers" => [TL.make("inputFolderPeer", "peer" => @input_entity, "folder_id" => folder)]))
|
|
134
134
|
end
|
|
135
135
|
|
|
136
|
+
# Leaves a channel or group and deletes the dialog.
|
|
137
|
+
def delete(revoke: false)
|
|
138
|
+
@client.delete_dialog(@input_entity, revoke: revoke)
|
|
139
|
+
end
|
|
140
|
+
|
|
136
141
|
def to_s
|
|
137
142
|
"#<Telethon::Dialog #{@id} #{@name.inspect}>"
|
|
138
143
|
end
|
|
@@ -11,6 +11,8 @@ module Telethon
|
|
|
11
11
|
class Sender
|
|
12
12
|
INIT_LAYER = "invokeWithLayer"
|
|
13
13
|
MAX_RESENDS = 5
|
|
14
|
+
# How long Telegram may hold an http_wait open, in ms, before answering with nothing.
|
|
15
|
+
HTTP_MAX_WAIT = 25_000
|
|
14
16
|
|
|
15
17
|
attr_reader :state, :transport, :updates
|
|
16
18
|
|
|
@@ -47,6 +49,7 @@ module Telethon
|
|
|
47
49
|
def poll(timeout)
|
|
48
50
|
deadline = Time.now.to_f + timeout
|
|
49
51
|
while Time.now.to_f < deadline
|
|
52
|
+
poll_http if http_transport?
|
|
50
53
|
break unless @transport.ready?([deadline - Time.now.to_f, 0.05].max)
|
|
51
54
|
|
|
52
55
|
receive_one
|
|
@@ -99,10 +102,28 @@ module Telethon
|
|
|
99
102
|
end
|
|
100
103
|
|
|
101
104
|
def receive_one
|
|
102
|
-
|
|
105
|
+
poll_http if http_transport?
|
|
106
|
+
packet = @transport.recv_packet
|
|
107
|
+
return if packet.empty? # an HTTP answer with nothing pending
|
|
108
|
+
|
|
109
|
+
msg_id, seq_no, body = @state.decrypt(packet)
|
|
103
110
|
handle(msg_id, seq_no, body)
|
|
104
111
|
end
|
|
105
112
|
|
|
113
|
+
# Telegram's HTTP transport only answers requests: when every POST has been answered and the
|
|
114
|
+
# result is still to come, ask for it with http_wait (Telegram holds the answer up to max_wait ms).
|
|
115
|
+
def http_transport?
|
|
116
|
+
transport = @transport
|
|
117
|
+
transport.respond_to?(:http?) && transport.http?
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def poll_http
|
|
121
|
+
return if @transport.outstanding.positive?
|
|
122
|
+
|
|
123
|
+
wait = TL.encode(TL.make("http_wait", "max_delay" => 0, "wait_after" => 0, "max_wait" => HTTP_MAX_WAIT))
|
|
124
|
+
@transport.send_packet(@state.encrypt(@state.next_msg_id, @state.next_seq_no(false), wait))
|
|
125
|
+
end
|
|
126
|
+
|
|
106
127
|
def handle(msg_id, seq_no, body)
|
|
107
128
|
@pending_acks << msg_id if seq_no.odd?
|
|
108
129
|
handle_body(msg_id, body)
|
|
@@ -194,6 +194,17 @@ module Telethon
|
|
|
194
194
|
|
|
195
195
|
def send_packet(data)
|
|
196
196
|
@io.write(@codec.encode_packet(data))
|
|
197
|
+
@outstanding = outstanding + 1
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Over HTTP, Telegram answers each POST once: the number of POSTs whose answer is unread. A
|
|
201
|
+
# read with none outstanding would wait forever, so the sender first sends http_wait.
|
|
202
|
+
def outstanding
|
|
203
|
+
@outstanding || 0
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def http?
|
|
207
|
+
@connection == :http
|
|
197
208
|
end
|
|
198
209
|
|
|
199
210
|
def ready?(timeout)
|
|
@@ -201,7 +212,9 @@ module Telethon
|
|
|
201
212
|
end
|
|
202
213
|
|
|
203
214
|
def recv_packet
|
|
204
|
-
@codec.read_packet(@io)
|
|
215
|
+
packet = @codec.read_packet(@io)
|
|
216
|
+
@outstanding = outstanding - 1 if outstanding.positive?
|
|
217
|
+
packet
|
|
205
218
|
end
|
|
206
219
|
|
|
207
220
|
private
|
|
@@ -5,8 +5,12 @@ module Telethon
|
|
|
5
5
|
module PrivateFiles
|
|
6
6
|
module_function
|
|
7
7
|
|
|
8
|
+
# Missing directories above it are made owner-only too (an account's TG_HOME/accounts/NAME).
|
|
8
9
|
def directory(path)
|
|
9
|
-
|
|
10
|
+
unless Dir.exist?(path)
|
|
11
|
+
parents(path)
|
|
12
|
+
Dir.mkdir(path, 0o700)
|
|
13
|
+
end
|
|
10
14
|
File.chmod(0o700, path)
|
|
11
15
|
path
|
|
12
16
|
end
|
data/lib/telethon/version.rb
CHANGED
data/lib/telethon.rb
CHANGED
|
@@ -67,6 +67,14 @@ require_relative "telethon/client/dialogs"
|
|
|
67
67
|
require_relative "telethon/client/chats"
|
|
68
68
|
require_relative "telethon/client/bots"
|
|
69
69
|
require_relative "telethon/qr"
|
|
70
|
+
require_relative "telethon/cli/table"
|
|
70
71
|
require_relative "telethon/cli"
|
|
71
72
|
require_relative "telethon/cli/args"
|
|
73
|
+
require_relative "telethon/cli/call"
|
|
74
|
+
require_relative "telethon/cli/completion"
|
|
75
|
+
require_relative "telethon/cli/commands/account"
|
|
76
|
+
require_relative "telethon/cli/commands/reading"
|
|
77
|
+
require_relative "telethon/cli/commands/writing"
|
|
78
|
+
require_relative "telethon/cli/commands/chats"
|
|
79
|
+
require_relative "telethon/cli/commands/bots"
|
|
72
80
|
require_relative "telethon/cli/commands"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: telethon
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- shogi-dojo
|
|
@@ -43,7 +43,15 @@ files:
|
|
|
43
43
|
- lib/telethon/assertions.rb
|
|
44
44
|
- lib/telethon/cli.rb
|
|
45
45
|
- lib/telethon/cli/args.rb
|
|
46
|
+
- lib/telethon/cli/call.rb
|
|
46
47
|
- lib/telethon/cli/commands.rb
|
|
48
|
+
- lib/telethon/cli/commands/account.rb
|
|
49
|
+
- lib/telethon/cli/commands/bots.rb
|
|
50
|
+
- lib/telethon/cli/commands/chats.rb
|
|
51
|
+
- lib/telethon/cli/commands/reading.rb
|
|
52
|
+
- lib/telethon/cli/commands/writing.rb
|
|
53
|
+
- lib/telethon/cli/completion.rb
|
|
54
|
+
- lib/telethon/cli/table.rb
|
|
47
55
|
- lib/telethon/client.rb
|
|
48
56
|
- lib/telethon/client/auth.rb
|
|
49
57
|
- lib/telethon/client/bots.rb
|