telethon 0.1.0 → 0.2.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 +96 -0
- data/README.md +103 -8
- data/lib/telethon/cli/commands.rb +25 -20
- data/lib/telethon/client/auth.rb +22 -1
- data/lib/telethon/client/chats.rb +577 -0
- data/lib/telethon/client/dialogs.rb +211 -0
- data/lib/telethon/client/downloads.rb +659 -0
- data/lib/telethon/client/entities.rb +50 -2
- data/lib/telethon/client/files.rb +341 -192
- data/lib/telethon/client/messages.rb +264 -40
- data/lib/telethon/client/updates.rb +407 -0
- data/lib/telethon/client.rb +119 -31
- data/lib/telethon/crypto/rsa.rb +81 -5
- data/lib/telethon/custom/admin_log_event.rb +133 -0
- data/lib/telethon/custom/conversation.rb +358 -0
- data/lib/telethon/custom/dialog.rb +144 -0
- data/lib/telethon/custom/file.rb +130 -0
- data/lib/telethon/custom/forward.rb +63 -0
- data/lib/telethon/custom/getters.rb +152 -0
- data/lib/telethon/custom/participant_permissions.rb +100 -0
- data/lib/telethon/errors.rb +12 -0
- data/lib/telethon/events/album.rb +108 -0
- data/lib/telethon/events/chat_action.rb +212 -0
- data/lib/telethon/events/common.rb +173 -0
- data/lib/telethon/events/message_deleted.rb +119 -0
- data/lib/telethon/events/new_message.rb +176 -0
- data/lib/telethon/events/raw.rb +35 -0
- data/lib/telethon/events/user_update.rb +129 -0
- data/lib/telethon/extensions/html.rb +373 -0
- data/lib/telethon/extensions/markdown.rb +116 -0
- data/lib/telethon/extensions/text.rb +171 -0
- data/lib/telethon/message.rb +423 -0
- data/lib/telethon/mime.rb +1930 -0
- data/lib/telethon/mime_guess.rb +54 -0
- data/lib/telethon/network/authenticator.rb +5 -4
- data/lib/telethon/network/sender.rb +7 -0
- data/lib/telethon/private_files.rb +25 -0
- data/lib/telethon/session.rb +18 -0
- data/lib/telethon/tl/object.rb +4 -0
- data/lib/telethon/updates/message_box.rb +481 -0
- data/lib/telethon/version.rb +1 -1
- data/lib/telethon.rb +25 -0
- metadata +26 -1
data/lib/telethon/crypto/rsa.rb
CHANGED
|
@@ -19,14 +19,15 @@ module Telethon
|
|
|
19
19
|
|
|
20
20
|
module_function
|
|
21
21
|
|
|
22
|
-
def key_for(fingerprint, use_old: false)
|
|
23
|
-
|
|
22
|
+
def key_for(fingerprint, use_old: false, keys: KEYS)
|
|
23
|
+
keys.find { |key| key[0] == fingerprint && (use_old || !key[3]) }
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
# sha1(data) + data + random padding to 255 bytes, raised to e mod n: how Telethon encrypts the
|
|
27
|
-
# p_q_inner_data. Answers nil when no key matches the fingerprint.
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
# p_q_inner_data. Answers nil when no key matches the fingerprint. keys are KEYS unless given (a
|
|
28
|
+
# CDN data center's, from parse_pem).
|
|
29
|
+
def encrypt(fingerprint, data, use_old: false, padding: nil, keys: KEYS)
|
|
30
|
+
key = key_for(fingerprint, use_old: use_old, keys: keys)
|
|
30
31
|
return nil unless key
|
|
31
32
|
|
|
32
33
|
filler = padding || Crypto.random_bytes(235 - data.bytesize)
|
|
@@ -34,6 +35,81 @@ module Telethon
|
|
|
34
35
|
modulus = [key[1]].pack("H*")
|
|
35
36
|
Num.modpow(payload, Num.from_int(key[2], 3), modulus)
|
|
36
37
|
end
|
|
38
|
+
|
|
39
|
+
# A key as KEYS holds it, from a PEM "RSA PUBLIC KEY" (PKCS#1), as help.getCdnConfig gives CDN
|
|
40
|
+
# keys: [fingerprint, modulus in hex, exponent, false].
|
|
41
|
+
def parse_pem(pem)
|
|
42
|
+
body = pem.lines.reject { |line| line.start_with?("-----") }.join.delete("\r\n ")
|
|
43
|
+
der = base64_decode(body).unpack("C*")
|
|
44
|
+
at = [0]
|
|
45
|
+
expect(der, at, 0x30)
|
|
46
|
+
der_length(der, at)
|
|
47
|
+
modulus = der_integer(der, at)
|
|
48
|
+
exponent = der_integer(der, at)
|
|
49
|
+
e = 0
|
|
50
|
+
exponent.each { |byte| e = (e << 8) | byte }
|
|
51
|
+
[fingerprint(modulus.pack("C*"), exponent.pack("C*")), modulus.pack("C*").unpack1("H*"), e, false]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Telegram's key fingerprint: the low 64 bits of sha1 of n and e as TL bytes.
|
|
55
|
+
def fingerprint(modulus, exponent)
|
|
56
|
+
writer = TL::Writer.new
|
|
57
|
+
writer.tl_bytes(modulus)
|
|
58
|
+
writer.tl_bytes(exponent)
|
|
59
|
+
digest = Crypto.sha1(writer.to_s)
|
|
60
|
+
digest.byteslice(12, 8).unpack1("q<")
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def expect(der, at, tag)
|
|
64
|
+
raise SecurityError, "not an RSA public key" unless der[at[0]] == tag
|
|
65
|
+
|
|
66
|
+
at[0] += 1
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def der_length(der, at)
|
|
70
|
+
first = der[at[0]]
|
|
71
|
+
at[0] += 1
|
|
72
|
+
return first if first < 0x80
|
|
73
|
+
|
|
74
|
+
length = 0
|
|
75
|
+
(first & 0x7f).times do
|
|
76
|
+
length = (length << 8) | der[at[0]]
|
|
77
|
+
at[0] += 1
|
|
78
|
+
end
|
|
79
|
+
length
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# An INTEGER's bytes, without the sign byte DER adds.
|
|
83
|
+
def der_integer(der, at)
|
|
84
|
+
expect(der, at, 0x02)
|
|
85
|
+
length = der_length(der, at)
|
|
86
|
+
bytes = der[at[0], length]
|
|
87
|
+
at[0] += length
|
|
88
|
+
bytes = bytes[1..] while bytes.size > 1 && bytes[0].zero?
|
|
89
|
+
bytes
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
93
|
+
|
|
94
|
+
def base64_decode(text)
|
|
95
|
+
out = []
|
|
96
|
+
bits = 0
|
|
97
|
+
count = 0
|
|
98
|
+
text.each_char do |char|
|
|
99
|
+
break if char == "="
|
|
100
|
+
|
|
101
|
+
value = BASE64.index(char)
|
|
102
|
+
raise SecurityError, "not base64" if value.nil?
|
|
103
|
+
|
|
104
|
+
bits = ((bits & 0xffff) << 6) | value # never past 64 bits (Spinel)
|
|
105
|
+
count += 6
|
|
106
|
+
if count >= 8
|
|
107
|
+
count -= 8
|
|
108
|
+
out << ((bits >> count) & 0xff)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
out.pack("C*")
|
|
112
|
+
end
|
|
37
113
|
end
|
|
38
114
|
end
|
|
39
115
|
end
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
# Telethon's tl/custom/adminlogevent.py: one entry of a channel's admin log (Client#iter_admin_log).
|
|
5
|
+
# old and new are the value before and after the change the action made; the predicates say which
|
|
6
|
+
# change it was. Messages in it are Messages.
|
|
7
|
+
class AdminLogEvent
|
|
8
|
+
PREFIX = "channelAdminLogEventAction"
|
|
9
|
+
# The actions whose old is prev_value and new new_value.
|
|
10
|
+
VALUE_CHANGES = %w[ChangeAbout ChangeTitle ChangeUsername ChangeLocation ChangeHistoryTTL].freeze
|
|
11
|
+
TOGGLES = %w[ToggleInvites TogglePreHistoryHidden ToggleSignatures].freeze
|
|
12
|
+
|
|
13
|
+
attr_reader :original, :entities, :user, :input_user
|
|
14
|
+
|
|
15
|
+
def initialize(client, original, entities, input_chat = nil)
|
|
16
|
+
@original = original
|
|
17
|
+
@entities = entities
|
|
18
|
+
@user = entities[original["user_id"]]
|
|
19
|
+
@input_user = @user && Custom.input_peer_of(client, @user)
|
|
20
|
+
@messages = {}
|
|
21
|
+
action = original["action"]
|
|
22
|
+
# Telethon gives an edited or deleted message the client and the log's users and chats.
|
|
23
|
+
%w[prev_message new_message message].each do |field|
|
|
24
|
+
raw = action[field]
|
|
25
|
+
next unless raw
|
|
26
|
+
|
|
27
|
+
attached = %w[EditMessage DeleteMessage].include?(kind)
|
|
28
|
+
@messages[field] = attached ? Message.new(raw, client, entities, input_chat) : Message.new(raw)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def id
|
|
33
|
+
@original["id"]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def date
|
|
37
|
+
@original["date"]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def user_id
|
|
41
|
+
@original["user_id"]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def action
|
|
45
|
+
@original["action"]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The action's name without its prefix: "ChangeTitle", "ParticipantJoin"...
|
|
49
|
+
def kind
|
|
50
|
+
action.name.delete_prefix(PREFIX)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The value before the change, or nil when the action has none.
|
|
54
|
+
def old
|
|
55
|
+
a = action
|
|
56
|
+
k = kind
|
|
57
|
+
return a["prev_value"] if VALUE_CHANGES.include?(k)
|
|
58
|
+
return !a["new_value"] if TOGGLES.include?(k)
|
|
59
|
+
|
|
60
|
+
case k
|
|
61
|
+
when "ChangePhoto" then a["prev_photo"]
|
|
62
|
+
when "ChangeStickerSet" then a["prev_stickerset"]
|
|
63
|
+
when "EditMessage" then @messages["prev_message"]
|
|
64
|
+
when "ParticipantToggleAdmin", "ParticipantToggleBan" then a["prev_participant"]
|
|
65
|
+
when "DeleteMessage" then @messages["message"]
|
|
66
|
+
when "DefaultBannedRights" then a["prev_banned_rights"]
|
|
67
|
+
when "DiscardGroupCall" then a["call"]
|
|
68
|
+
when "ExportedInviteDelete", "ExportedInviteRevoke", "ParticipantJoinByInvite" then a["invite"]
|
|
69
|
+
when "ExportedInviteEdit" then a["prev_invite"]
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The value after the change, or nil when the action has none.
|
|
74
|
+
def new
|
|
75
|
+
a = action
|
|
76
|
+
k = kind
|
|
77
|
+
return a["new_value"] if VALUE_CHANGES.include?(k) || TOGGLES.include?(k)
|
|
78
|
+
|
|
79
|
+
case k
|
|
80
|
+
when "ChangePhoto" then a["new_photo"]
|
|
81
|
+
when "ChangeStickerSet" then a["new_stickerset"]
|
|
82
|
+
when "EditMessage" then @messages["new_message"]
|
|
83
|
+
when "ParticipantToggleAdmin", "ParticipantToggleBan" then a["new_participant"]
|
|
84
|
+
when "ParticipantInvite", "ParticipantVolume", "ParticipantMute", "ParticipantUnmute" then a["participant"]
|
|
85
|
+
when "DefaultBannedRights" then a["new_banned_rights"]
|
|
86
|
+
when "StopPoll" then @messages["message"]
|
|
87
|
+
when "StartGroupCall" then a["call"]
|
|
88
|
+
when "ToggleGroupCallSetting" then a["join_muted"]
|
|
89
|
+
when "ExportedInviteEdit" then a["new_invite"]
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def changed_about? = kind == "ChangeAbout"
|
|
94
|
+
def changed_title? = kind == "ChangeTitle"
|
|
95
|
+
def changed_username? = kind == "ChangeUsername"
|
|
96
|
+
def changed_photo? = kind == "ChangePhoto"
|
|
97
|
+
def changed_sticker_set? = kind == "ChangeStickerSet"
|
|
98
|
+
def changed_message? = kind == "EditMessage"
|
|
99
|
+
def deleted_message? = kind == "DeleteMessage"
|
|
100
|
+
def changed_admin? = kind == "ParticipantToggleAdmin"
|
|
101
|
+
def changed_restrictions? = kind == "ParticipantToggleBan"
|
|
102
|
+
def changed_invites? = kind == "ToggleInvites"
|
|
103
|
+
def changed_location? = kind == "ChangeLocation"
|
|
104
|
+
def joined? = kind == "ParticipantJoin"
|
|
105
|
+
def joined_invite? = kind == "ParticipantInvite"
|
|
106
|
+
def left? = kind == "ParticipantLeave"
|
|
107
|
+
def changed_hide_history? = kind == "TogglePreHistoryHidden"
|
|
108
|
+
def changed_signatures? = kind == "ToggleSignatures"
|
|
109
|
+
def changed_pin? = kind == "UpdatePinned"
|
|
110
|
+
def changed_default_banned_rights? = kind == "DefaultBannedRights"
|
|
111
|
+
def stopped_poll? = kind == "StopPoll"
|
|
112
|
+
def started_group_call? = kind == "StartGroupCall"
|
|
113
|
+
def discarded_group_call? = kind == "DiscardGroupCall"
|
|
114
|
+
def user_muted? = kind == "ParticipantMute"
|
|
115
|
+
# Telethon spells it user_unmutted.
|
|
116
|
+
def user_unmuted? = kind == "ParticipantUnmute"
|
|
117
|
+
def changed_call_settings? = kind == "ToggleGroupCallSetting"
|
|
118
|
+
def changed_history_ttl? = kind == "ChangeHistoryTTL"
|
|
119
|
+
def deleted_exported_invite? = kind == "ExportedInviteDelete"
|
|
120
|
+
def edited_exported_invite? = kind == "ExportedInviteEdit"
|
|
121
|
+
def revoked_exported_invite? = kind == "ExportedInviteRevoke"
|
|
122
|
+
def joined_by_invite? = kind == "ParticipantJoinByInvite"
|
|
123
|
+
def changed_user_volume? = kind == "ParticipantVolume"
|
|
124
|
+
|
|
125
|
+
def to_s
|
|
126
|
+
"#<Telethon::AdminLogEvent #{id} #{kind}>"
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def inspect
|
|
130
|
+
to_s
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
# Telethon's tl/custom/conversation.py: a conversation in one chat, opened by Client#conversation.
|
|
5
|
+
# What it sends is remembered, and get_response, get_reply, get_edit, wait_read and wait_event
|
|
6
|
+
# answer what the chat says back. Telethon awaits futures that its update loop fills; here the
|
|
7
|
+
# waiting runs the client's update loop (step_updates) until the answer comes or the time is up, so
|
|
8
|
+
# call them from the thread that runs the updates.
|
|
9
|
+
class Conversation
|
|
10
|
+
include ChatGetter
|
|
11
|
+
|
|
12
|
+
EDIT_COLLISION_DELTA = 0.001
|
|
13
|
+
|
|
14
|
+
# A future: done with a value, failed with an error, or cancelled.
|
|
15
|
+
class Slot
|
|
16
|
+
attr_reader :value, :error
|
|
17
|
+
|
|
18
|
+
def initialize
|
|
19
|
+
@done = false
|
|
20
|
+
@value = nil
|
|
21
|
+
@error = nil
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def done?
|
|
25
|
+
@done
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def set(value)
|
|
29
|
+
@value = value
|
|
30
|
+
@done = true
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def fail(error)
|
|
34
|
+
@error = error
|
|
35
|
+
@done = true
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def cancel
|
|
39
|
+
fail(CancelledError.new("The conversation was cancelled"))
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
attr_reader :client, :incoming, :outgoing
|
|
44
|
+
|
|
45
|
+
def initialize(client, input_chat, timeout:, total_timeout:, max_messages:, exclusive:, replies_are_responses:)
|
|
46
|
+
@client = client
|
|
47
|
+
@entity = input_chat
|
|
48
|
+
@chat_peer = nil
|
|
49
|
+
@chat = nil
|
|
50
|
+
@input_chat = nil
|
|
51
|
+
@broadcast = nil
|
|
52
|
+
@timeout = timeout
|
|
53
|
+
@total_timeout = total_timeout
|
|
54
|
+
@total_due = nil
|
|
55
|
+
@outgoing = {}
|
|
56
|
+
@last_outgoing = 0
|
|
57
|
+
@incoming = []
|
|
58
|
+
@max_incoming = max_messages
|
|
59
|
+
@last_read = nil
|
|
60
|
+
@custom = {}
|
|
61
|
+
@custom_counter = 0
|
|
62
|
+
@pending_responses = {}
|
|
63
|
+
@pending_replies = {}
|
|
64
|
+
@pending_edits = {}
|
|
65
|
+
@pending_reads = {}
|
|
66
|
+
@exclusive = exclusive
|
|
67
|
+
@cancelled = false
|
|
68
|
+
@response_indices = {}
|
|
69
|
+
@replies_are_responses = replies_are_responses
|
|
70
|
+
@reply_indices = replies_are_responses ? @response_indices : {}
|
|
71
|
+
@edit_dates = {}
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Telethon's __aenter__: joins the client's conversations in the chat.
|
|
75
|
+
def open
|
|
76
|
+
@input_chat = @client.get_input_entity(@entity)
|
|
77
|
+
@chat_peer = @client.get_peer(@input_chat)
|
|
78
|
+
set = (@client.conversations[chat_id] ||= [])
|
|
79
|
+
raise AlreadyInConversationError, "Cannot open exclusive conversation in a chat that already has one open conversation" if @exclusive && !set.empty?
|
|
80
|
+
|
|
81
|
+
set << self
|
|
82
|
+
@cancelled = false
|
|
83
|
+
@last_outgoing = 0
|
|
84
|
+
@outgoing.clear
|
|
85
|
+
@incoming.clear
|
|
86
|
+
@pending_responses.clear
|
|
87
|
+
@pending_replies.clear
|
|
88
|
+
@pending_edits.clear
|
|
89
|
+
@response_indices.clear
|
|
90
|
+
@reply_indices.clear
|
|
91
|
+
@edit_dates.clear
|
|
92
|
+
@custom.clear
|
|
93
|
+
@total_due = @total_timeout ? @client.clock.now + @total_timeout.to_f : nil
|
|
94
|
+
self
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Telethon's __aexit__: leaves the client's conversations, and cancels what still waits.
|
|
98
|
+
def close
|
|
99
|
+
set = @client.conversations[chat_id]
|
|
100
|
+
if set
|
|
101
|
+
set.delete(self)
|
|
102
|
+
@client.conversations.delete(chat_id) if set.empty?
|
|
103
|
+
end
|
|
104
|
+
cancel_all_pending
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def send_message(text, reply_to: nil, parse_mode: :default, formatting_entities: nil, link_preview: true,
|
|
108
|
+
silent: false)
|
|
109
|
+
check_cancelled
|
|
110
|
+
sent = @client.send_message(@input_chat, text, reply_to: reply_to, parse_mode: parse_mode,
|
|
111
|
+
formatting_entities: formatting_entities, link_preview: link_preview,
|
|
112
|
+
silent: silent)
|
|
113
|
+
remember_outgoing([sent])
|
|
114
|
+
sent
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def send_file(files, caption: "", reply_to: nil, force_document: false, parse_mode: :default)
|
|
118
|
+
check_cancelled
|
|
119
|
+
sent = @client.send_file(@input_chat, files, caption: caption, reply_to: reply_to, force_document: force_document,
|
|
120
|
+
parse_mode: parse_mode)
|
|
121
|
+
remember_outgoing(sent.is_a?(Array) ? sent : [sent])
|
|
122
|
+
sent
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Marks the chat read up to message (an id or a Message), or up to the last message received.
|
|
126
|
+
def mark_read(message = nil)
|
|
127
|
+
check_cancelled
|
|
128
|
+
max_id = if message.nil?
|
|
129
|
+
@incoming.empty? ? 0 : @incoming[-1].id
|
|
130
|
+
else
|
|
131
|
+
message.is_a?(Integer) ? message : message.id
|
|
132
|
+
end
|
|
133
|
+
@client.send_read_acknowledge(@input_chat, max_id: max_id)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The next message received after message (an id or a Message; the last one sent by default).
|
|
137
|
+
def get_response(message = nil, timeout: nil)
|
|
138
|
+
get_message(message, @response_indices, @pending_responses, timeout, false)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# The next message received that replies to message.
|
|
142
|
+
def get_reply(message = nil, timeout: nil)
|
|
143
|
+
get_message(message, @reply_indices, @pending_replies, timeout, true)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# The next edit of a message received after message.
|
|
147
|
+
def get_edit(message = nil, timeout: nil)
|
|
148
|
+
start = @client.clock.now
|
|
149
|
+
target_id = message_id_of(message)
|
|
150
|
+
target_date = @edit_dates[target_id] || 0.0
|
|
151
|
+
earliest = nil
|
|
152
|
+
@incoming.each do |x|
|
|
153
|
+
next unless x.edit_date && x.id > target_id && x.edit_date.to_f > target_date
|
|
154
|
+
next if earliest && earliest.edit_date <= x.edit_date
|
|
155
|
+
|
|
156
|
+
earliest = x
|
|
157
|
+
end
|
|
158
|
+
if earliest && earliest.edit_date.to_f > target_date
|
|
159
|
+
@edit_dates[target_id] = earliest.edit_date.to_f
|
|
160
|
+
return earliest
|
|
161
|
+
end
|
|
162
|
+
slot = Slot.new
|
|
163
|
+
@pending_edits[target_id] = slot
|
|
164
|
+
result_of(slot, start, timeout, @pending_edits, target_id)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Waits until the other side reads message (the last one sent by default). Answers true, or nil
|
|
168
|
+
# when it was read already.
|
|
169
|
+
def wait_read(message = nil, timeout: nil)
|
|
170
|
+
start = @client.clock.now
|
|
171
|
+
target_id = message_id_of(message)
|
|
172
|
+
@last_read = target_id - 1 if @last_read.nil?
|
|
173
|
+
return nil if @last_read >= target_id
|
|
174
|
+
|
|
175
|
+
slot = Slot.new
|
|
176
|
+
@pending_reads[target_id] = slot
|
|
177
|
+
result_of(slot, start, timeout, @pending_reads, target_id)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# The next event the builder (e.g. Events::NewMessage.new(chats: ...)) takes and lets through its
|
|
181
|
+
# filter.
|
|
182
|
+
def wait_event(builder, timeout: nil)
|
|
183
|
+
start = @client.clock.now
|
|
184
|
+
builder.resolve(@client) unless builder.resolved?
|
|
185
|
+
counter = @custom_counter
|
|
186
|
+
@custom_counter += 1
|
|
187
|
+
slot = Slot.new
|
|
188
|
+
@custom[counter] = [builder, slot]
|
|
189
|
+
begin
|
|
190
|
+
result_of(slot, start, timeout, @custom, counter)
|
|
191
|
+
ensure
|
|
192
|
+
@custom.delete(counter)
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Cancels what this conversation waits for; it cannot be used after.
|
|
197
|
+
def cancel
|
|
198
|
+
cancel_all_pending
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# Cancels every conversation in this chat.
|
|
202
|
+
def cancel_all
|
|
203
|
+
(@client.conversations[chat_id] || []).dup.each { |conv| conv.cancel }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def cancelled?
|
|
207
|
+
@cancelled
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# ---- what the client's update loop hands it ----
|
|
211
|
+
|
|
212
|
+
def on_new_message(event)
|
|
213
|
+
response = event.message
|
|
214
|
+
return if response.chat_id != chat_id || response.out?
|
|
215
|
+
|
|
216
|
+
if @incoming.size == @max_incoming
|
|
217
|
+
cancel_all_pending(Error.new("Too many incoming messages"))
|
|
218
|
+
return
|
|
219
|
+
end
|
|
220
|
+
@incoming << response
|
|
221
|
+
@pending_responses.keys.each do |msg_id|
|
|
222
|
+
@response_indices[msg_id] = @incoming.size
|
|
223
|
+
@pending_responses.delete(msg_id).set(response)
|
|
224
|
+
end
|
|
225
|
+
@pending_replies.keys.each do |msg_id|
|
|
226
|
+
next unless response.reply_to_msg_id == msg_id
|
|
227
|
+
|
|
228
|
+
@reply_indices[msg_id] = @incoming.size
|
|
229
|
+
@pending_replies.delete(msg_id).set(response)
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def on_edit(event)
|
|
234
|
+
message = event.message
|
|
235
|
+
return if message.chat_id != chat_id || message.out?
|
|
236
|
+
|
|
237
|
+
@incoming.each_index do |i|
|
|
238
|
+
if @incoming[i].id == message.id
|
|
239
|
+
@incoming[i] = message
|
|
240
|
+
break
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
@pending_edits.keys.each do |msg_id|
|
|
244
|
+
next unless msg_id < message.id
|
|
245
|
+
|
|
246
|
+
edit_ts = message.edit_date.to_f
|
|
247
|
+
known = @edit_dates[msg_id] || 0.0
|
|
248
|
+
@edit_dates[msg_id] = edit_ts <= known ? known + EDIT_COLLISION_DELTA : edit_ts
|
|
249
|
+
@pending_edits.delete(msg_id).set(message)
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# Telethon resolves a wait_read when the message waited for is at or above the read mark, which
|
|
254
|
+
# is backwards; here it is when the mark reaches it (DEVELOPING.md).
|
|
255
|
+
def on_read(event)
|
|
256
|
+
return if event.chat_id != chat_id || event.inbox?
|
|
257
|
+
|
|
258
|
+
@last_read = event.max_id
|
|
259
|
+
@pending_reads.keys.each do |msg_id|
|
|
260
|
+
@pending_reads.delete(msg_id).set(true) if msg_id <= @last_read
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# Telethon's _check_custom: built is the events this update makes, by kind.
|
|
265
|
+
def check_custom(built)
|
|
266
|
+
@custom.keys.each do |key|
|
|
267
|
+
builder, slot = @custom[key]
|
|
268
|
+
event = built[builder.kind]
|
|
269
|
+
next unless event && builder.filter(event)
|
|
270
|
+
|
|
271
|
+
slot.set(event)
|
|
272
|
+
@custom.delete(key)
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def custom_builders
|
|
277
|
+
@custom.values.map { |pair| pair[0] }
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def to_s
|
|
281
|
+
"#<Telethon::Conversation #{chat_id}>"
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def inspect
|
|
285
|
+
to_s
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
private
|
|
289
|
+
|
|
290
|
+
def remember_outgoing(messages)
|
|
291
|
+
messages.each { |m| @outgoing[m.id] = true if m }
|
|
292
|
+
last = messages.compact[-1]
|
|
293
|
+
@last_outgoing = last.id if last
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def get_message(target_message, indices, pending, timeout, reply)
|
|
297
|
+
start = @client.clock.now
|
|
298
|
+
target_id = message_id_of(target_message)
|
|
299
|
+
unless indices.key?(target_id)
|
|
300
|
+
index = @incoming.index { |incoming| incoming.id > target_id }
|
|
301
|
+
indices[target_id] = index || @incoming.size
|
|
302
|
+
end
|
|
303
|
+
last_idx = indices[target_id]
|
|
304
|
+
if last_idx < @incoming.size
|
|
305
|
+
incoming = @incoming[last_idx]
|
|
306
|
+
if !reply || incoming.reply_to_msg_id == target_id
|
|
307
|
+
indices[target_id] += 1
|
|
308
|
+
return incoming
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
slot = Slot.new
|
|
312
|
+
pending[target_id] = slot
|
|
313
|
+
result_of(slot, start, timeout, pending, target_id)
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
def message_id_of(message)
|
|
317
|
+
return message.is_a?(Integer) ? message : message.id unless message.nil?
|
|
318
|
+
return @last_outgoing if @last_outgoing != 0
|
|
319
|
+
|
|
320
|
+
raise ArgumentError, "No message was sent previously"
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
# Runs the update loop until the slot is filled, or raises TimeoutError at the deadline: timeout
|
|
324
|
+
# (the conversation's by default) from start, and never past total_timeout.
|
|
325
|
+
def result_of(slot, start, timeout, pending, key)
|
|
326
|
+
check_cancelled
|
|
327
|
+
timeout = @timeout if timeout.nil?
|
|
328
|
+
due = @total_due
|
|
329
|
+
if timeout
|
|
330
|
+
own = start + timeout.to_f
|
|
331
|
+
due = own if due.nil? || own < due
|
|
332
|
+
end
|
|
333
|
+
until slot.done?
|
|
334
|
+
remaining = due.nil? ? nil : due - @client.clock.now
|
|
335
|
+
if remaining && remaining <= 0
|
|
336
|
+
pending.delete(key)
|
|
337
|
+
raise TimeoutError, "No answer in the conversation in time"
|
|
338
|
+
end
|
|
339
|
+
@client.step_updates(remaining)
|
|
340
|
+
end
|
|
341
|
+
raise slot.error if slot.error
|
|
342
|
+
|
|
343
|
+
slot.value
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
def check_cancelled
|
|
347
|
+
raise CancelledError, "The conversation was cancelled before" if @cancelled
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
def cancel_all_pending(error = nil)
|
|
351
|
+
@cancelled = true
|
|
352
|
+
[@pending_responses, @pending_replies, @pending_edits].each do |pending|
|
|
353
|
+
pending.each_value { |slot| error ? slot.fail(error) : slot.cancel }
|
|
354
|
+
end
|
|
355
|
+
@custom.each_value { |pair| error ? pair[1].fail(error) : pair[1].cancel }
|
|
356
|
+
end
|
|
357
|
+
end
|
|
358
|
+
end
|