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
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
module Events
|
|
5
|
+
# Telethon's events.Album: the messages of one album (a grouped_id) as one event. They arrive as
|
|
6
|
+
# separate updates, so the client holds each album until half a second passes with no new part
|
|
7
|
+
# (Telethon's AlbumHack), then hands it to the Album handlers.
|
|
8
|
+
class Album < Builder
|
|
9
|
+
DELAY = 0.5
|
|
10
|
+
|
|
11
|
+
def kind
|
|
12
|
+
"Album"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def build(update, _self_id)
|
|
16
|
+
return nil unless %w[updateNewMessage updateNewChannelMessage].include?(update.name)
|
|
17
|
+
|
|
18
|
+
message = update["message"]
|
|
19
|
+
return nil unless message.is?("message") && message["grouped_id"]
|
|
20
|
+
|
|
21
|
+
Album::Event.new([message])
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Only a complete album: a single part waits for the rest.
|
|
25
|
+
def filter(event)
|
|
26
|
+
return false unless event.messages.size > 1
|
|
27
|
+
|
|
28
|
+
super
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
class Event < Common
|
|
32
|
+
attr_reader :messages, :sender_id
|
|
33
|
+
|
|
34
|
+
def initialize(raws)
|
|
35
|
+
first = raws[0]
|
|
36
|
+
super(first["peer_id"], first["id"], first["post"] ? true : false)
|
|
37
|
+
@sender_id = Client.sender_id(first)
|
|
38
|
+
@messages = raws.map { |raw| Message.new(raw) }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def kind
|
|
42
|
+
"Album"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def attach(client, update, entities)
|
|
46
|
+
super
|
|
47
|
+
@messages.each { |m| m.attach(client, entities) }
|
|
48
|
+
self
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Adds the parts of another event of the same album.
|
|
52
|
+
def extend_with(other)
|
|
53
|
+
other.messages.each { |m| @messages << m }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def grouped_id
|
|
57
|
+
@messages[0].grouped_id
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def size
|
|
61
|
+
@messages.size
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def [](index)
|
|
65
|
+
@messages[index]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def each
|
|
69
|
+
@messages.each { |m| yield m }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def sender
|
|
73
|
+
@entities[@sender_id]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# The caption: the first part's text that is not empty.
|
|
77
|
+
def text
|
|
78
|
+
found = @messages.find { |m| !m.text.empty? }
|
|
79
|
+
found ? found.text : ""
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def raw_text
|
|
83
|
+
text
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def reply? = @messages[0].reply?
|
|
87
|
+
def fwd_from = @messages[0].fwd_from
|
|
88
|
+
def get_reply_message = @messages[0].get_reply_message
|
|
89
|
+
def respond(text) = @messages[0].respond(text)
|
|
90
|
+
def reply(text) = @messages[0].reply(text)
|
|
91
|
+
|
|
92
|
+
def forward_to(target, drop_author: false)
|
|
93
|
+
@client.forward_messages(target, @messages.map { |m| m.id }, chat_id, drop_author: drop_author)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Edits the part that has the caption, else the first.
|
|
97
|
+
def edit(text)
|
|
98
|
+
found = @messages.find { |m| !m.raw_text.empty? } || @messages[0]
|
|
99
|
+
found.edit(text)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def delete(revoke: true)
|
|
103
|
+
@client.delete_messages(chat_id, @messages.map { |m| m.id }, revoke: revoke)
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
module Events
|
|
5
|
+
# Telethon's events.ChatAction: someone joined, left, was added or kicked; a chat was created,
|
|
6
|
+
# renamed or got a new photo; a message was pinned or unpinned; a game score changed.
|
|
7
|
+
class ChatAction < Builder
|
|
8
|
+
def kind
|
|
9
|
+
"ChatAction"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def build(update, _self_id)
|
|
13
|
+
case update.name
|
|
14
|
+
when "updatePinnedChannelMessages"
|
|
15
|
+
return nil if update["pinned"]
|
|
16
|
+
|
|
17
|
+
ChatAction::Event.new(TL.make("peerChannel", "channel_id" => update["channel_id"]),
|
|
18
|
+
pin_ids: update["messages"], pin: update["pinned"])
|
|
19
|
+
when "updatePinnedMessages"
|
|
20
|
+
return nil if update["pinned"]
|
|
21
|
+
|
|
22
|
+
ChatAction::Event.new(update["peer"], pin_ids: update["messages"], pin: update["pinned"])
|
|
23
|
+
when "updateChatParticipantAdd"
|
|
24
|
+
ChatAction::Event.new(ChatAction.chat(update), added_by: update["inviter_id"] || true, users: update["user_id"])
|
|
25
|
+
when "updateChatParticipantDelete"
|
|
26
|
+
ChatAction::Event.new(ChatAction.chat(update), kicked_by: true, users: update["user_id"])
|
|
27
|
+
when "updateNewMessage", "updateNewChannelMessage"
|
|
28
|
+
message = update["message"]
|
|
29
|
+
message.is?("messageService") ? ChatAction.from_service(message) : nil
|
|
30
|
+
when "updateChannelParticipant"
|
|
31
|
+
joined = !update["new_participant"].nil?
|
|
32
|
+
return nil if joined == !update["prev_participant"].nil?
|
|
33
|
+
|
|
34
|
+
ChatAction::Event.new(TL.make("peerChannel", "channel_id" => update["channel_id"]), users: update["user_id"],
|
|
35
|
+
added_by: joined ? update["actor_id"] : nil,
|
|
36
|
+
kicked_by: joined ? nil : update["actor_id"])
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.chat(update)
|
|
41
|
+
TL.make("peerChat", "chat_id" => update["chat_id"])
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.from_service(message)
|
|
45
|
+
action = message["action"]
|
|
46
|
+
from = message["from_id"]
|
|
47
|
+
case action.name
|
|
48
|
+
when "messageActionChatJoinedByLink"
|
|
49
|
+
ChatAction::Event.new(message, added_by: true, users: from)
|
|
50
|
+
when "messageActionChatAddUser"
|
|
51
|
+
added_by = [Client.sender_id(message)] == action["users"] ? true : from
|
|
52
|
+
ChatAction::Event.new(message, added_by: added_by, users: action["users"])
|
|
53
|
+
when "messageActionChatDeleteUser"
|
|
54
|
+
ChatAction::Event.new(message, kicked_by: from ? Client.peer_id(from) : true, users: action["user_id"])
|
|
55
|
+
when "messageActionChatCreate"
|
|
56
|
+
ChatAction::Event.new(message, users: action["users"], created: true, new_title: action["title"])
|
|
57
|
+
when "messageActionChannelCreate"
|
|
58
|
+
ChatAction::Event.new(message, created: true, users: from, new_title: action["title"])
|
|
59
|
+
when "messageActionChatEditTitle"
|
|
60
|
+
ChatAction::Event.new(message, users: from, new_title: action["title"])
|
|
61
|
+
when "messageActionChatEditPhoto"
|
|
62
|
+
ChatAction::Event.new(message, users: from, new_photo: action["photo"])
|
|
63
|
+
when "messageActionChatDeletePhoto"
|
|
64
|
+
ChatAction::Event.new(message, users: from, new_photo: true)
|
|
65
|
+
when "messageActionPinMessage"
|
|
66
|
+
reply = message["reply_to"]
|
|
67
|
+
return nil unless reply
|
|
68
|
+
|
|
69
|
+
ChatAction::Event.new(message, pin_ids: [reply["reply_to_msg_id"]])
|
|
70
|
+
when "messageActionGameScore"
|
|
71
|
+
ChatAction::Event.new(message, new_score: action["score"])
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
class Event < Common
|
|
76
|
+
attr_reader :action_message, :new_title, :new_score, :photo
|
|
77
|
+
|
|
78
|
+
def initialize(where, new_photo: nil, added_by: nil, kicked_by: nil, created: nil, users: nil, new_title: nil,
|
|
79
|
+
pin_ids: nil, pin: nil, new_score: nil)
|
|
80
|
+
if where.is?("messageService")
|
|
81
|
+
@action_message = Message.new(where)
|
|
82
|
+
where = where["peer_id"]
|
|
83
|
+
else
|
|
84
|
+
@action_message = nil
|
|
85
|
+
end
|
|
86
|
+
super(where, pin_ids ? pin_ids[0] : nil)
|
|
87
|
+
@new_pin = !pin_ids.nil?
|
|
88
|
+
@pin_ids = pin_ids
|
|
89
|
+
@pinned_messages = nil
|
|
90
|
+
@new_photo = !new_photo.nil?
|
|
91
|
+
@photo = new_photo.is_a?(TL::Object) && new_photo.is?("photo") ? new_photo : nil
|
|
92
|
+
@added_by = nil
|
|
93
|
+
@kicked_by = nil
|
|
94
|
+
@user_added = @user_joined = @user_left = @user_kicked = false
|
|
95
|
+
if added_by == true
|
|
96
|
+
@user_joined = true
|
|
97
|
+
elsif added_by
|
|
98
|
+
@user_added = true
|
|
99
|
+
@added_by = added_by
|
|
100
|
+
end
|
|
101
|
+
if kicked_by == true || (!users.nil? && kicked_by == users)
|
|
102
|
+
@user_left = true
|
|
103
|
+
elsif kicked_by
|
|
104
|
+
@user_kicked = true
|
|
105
|
+
@kicked_by = kicked_by
|
|
106
|
+
end
|
|
107
|
+
@created = created ? true : false
|
|
108
|
+
@user_ids = if users.is_a?(Array)
|
|
109
|
+
users.map { |u| Events.peer_id_of(u) }
|
|
110
|
+
elsif users
|
|
111
|
+
[Events.peer_id_of(users)]
|
|
112
|
+
else
|
|
113
|
+
[]
|
|
114
|
+
end
|
|
115
|
+
@new_title = new_title
|
|
116
|
+
@new_score = new_score
|
|
117
|
+
@unpin = !pin # as Telethon: true for any event but a pin
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def kind
|
|
121
|
+
"ChatAction"
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def attach(client, update, entities)
|
|
125
|
+
super
|
|
126
|
+
@action_message&.attach(client, entities)
|
|
127
|
+
self
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def new_pin? = @new_pin
|
|
131
|
+
def new_photo? = @new_photo
|
|
132
|
+
def user_added? = @user_added
|
|
133
|
+
def user_joined? = @user_joined
|
|
134
|
+
def user_left? = @user_left
|
|
135
|
+
def user_kicked? = @user_kicked
|
|
136
|
+
def created? = @created
|
|
137
|
+
def unpin? = @unpin
|
|
138
|
+
|
|
139
|
+
def user_ids
|
|
140
|
+
@user_ids.dup
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def user_id
|
|
144
|
+
@user_ids.empty? ? nil : @user_ids[0]
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# The users the update came with, of those the action is about.
|
|
148
|
+
def users
|
|
149
|
+
found = []
|
|
150
|
+
@user_ids.each { |id| found << @entities[id] if @entities.key?(id) }
|
|
151
|
+
found
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def user
|
|
155
|
+
users[0]
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Who added or kicked the users: the user if the update came with them, else their id or peer.
|
|
159
|
+
def added_by
|
|
160
|
+
entity_for(@added_by)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def kicked_by
|
|
164
|
+
entity_for(@kicked_by)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def input_users
|
|
168
|
+
found = []
|
|
169
|
+
@user_ids.each do |id|
|
|
170
|
+
peer = @client.input_peer(id)
|
|
171
|
+
found << peer if peer
|
|
172
|
+
end
|
|
173
|
+
found
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def reply(text)
|
|
177
|
+
return respond(text) unless @action_message
|
|
178
|
+
|
|
179
|
+
@client.send_message(chat_id, text, reply_to: @action_message.id)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def delete(revoke: true)
|
|
183
|
+
return nil unless @action_message
|
|
184
|
+
|
|
185
|
+
@client.delete_messages(chat_id, [@action_message.id], revoke: revoke)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def pin_ids
|
|
189
|
+
@pin_ids
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def get_pinned_messages
|
|
193
|
+
return [] unless @pin_ids
|
|
194
|
+
|
|
195
|
+
@pinned_messages ||= @client.get_messages(chat_id, @pin_ids).compact
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def get_pinned_message
|
|
199
|
+
get_pinned_messages[0]
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
private
|
|
203
|
+
|
|
204
|
+
def entity_for(value)
|
|
205
|
+
return value if value.nil? || (value.is_a?(TL::Object) && value.name.start_with?("user"))
|
|
206
|
+
|
|
207
|
+
@entities[Events.peer_id_of(value)] || value
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
# Telethon's events: builders that turn an update into an event and decide whether a handler gets it,
|
|
5
|
+
# registered with Client#on or #add_event_handler.
|
|
6
|
+
module Events
|
|
7
|
+
# Raised in a handler to stop the handlers after it from seeing the same update.
|
|
8
|
+
class StopPropagation < StandardError
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# utils.get_peer_id of what an event holds: a marked id stays as it is, a peer or entity is marked.
|
|
12
|
+
def self.peer_id_of(value)
|
|
13
|
+
value.is_a?(Integer) ? value : Client.peer_id(value)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Telethon's _into_id_set: the marked ids a chats or from_users filter names, as a set. A positive
|
|
17
|
+
# number may be a user, a basic group or a channel, so it stands for all three.
|
|
18
|
+
def self.id_set(client, chats)
|
|
19
|
+
return nil if chats.nil?
|
|
20
|
+
|
|
21
|
+
list = chats.is_a?(Array) ? chats : [chats]
|
|
22
|
+
set = {}
|
|
23
|
+
list.each do |chat|
|
|
24
|
+
if chat.is_a?(Integer)
|
|
25
|
+
if chat.negative?
|
|
26
|
+
set[chat] = true
|
|
27
|
+
else
|
|
28
|
+
set[chat] = true
|
|
29
|
+
set[-chat] = true
|
|
30
|
+
set[-(Client::CHANNEL_BASE + chat)] = true
|
|
31
|
+
end
|
|
32
|
+
elsif chat.is_a?(TL::Object)
|
|
33
|
+
set[Client.peer_id(chat)] = true
|
|
34
|
+
else
|
|
35
|
+
set[Client.peer_id(client.get_entity(chat))] = true
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
set
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Telethon's EventBuilder. chats limits it to those chats (or, with blacklist_chats, to all others),
|
|
42
|
+
# func is a last check that gets the event.
|
|
43
|
+
class Builder
|
|
44
|
+
attr_reader :chats, :blacklist_chats, :func
|
|
45
|
+
|
|
46
|
+
def initialize(chats: nil, blacklist_chats: false, func: nil)
|
|
47
|
+
@chats = chats
|
|
48
|
+
@blacklist_chats = blacklist_chats ? true : false
|
|
49
|
+
@func = func
|
|
50
|
+
@chat_ids = nil
|
|
51
|
+
@resolved = false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The event's name, which also keys the events one update builds (Telethon's EventBuilderDict).
|
|
55
|
+
def kind
|
|
56
|
+
"Builder"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def resolved?
|
|
60
|
+
@resolved
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def resolve(client)
|
|
64
|
+
return if @resolved
|
|
65
|
+
|
|
66
|
+
resolve_ids(client)
|
|
67
|
+
@resolved = true
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The event for an update, or nil. self_id is the logged-in user's.
|
|
71
|
+
def build(_update, _self_id)
|
|
72
|
+
nil
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def filter(event)
|
|
76
|
+
return false unless @resolved
|
|
77
|
+
|
|
78
|
+
if @chat_ids
|
|
79
|
+
inside = @chat_ids.key?(event.chat_id)
|
|
80
|
+
return false if inside == @blacklist_chats
|
|
81
|
+
end
|
|
82
|
+
return true unless @func
|
|
83
|
+
|
|
84
|
+
@func.call(event) ? true : false
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
def resolve_ids(client)
|
|
90
|
+
@chat_ids = Events.id_set(client, @chats)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Telethon's EventCommon with its ChatGetter: the chat an event happened in, and the update and
|
|
95
|
+
# entities it came with.
|
|
96
|
+
class Common
|
|
97
|
+
attr_reader :client, :original_update, :entities, :chat_peer
|
|
98
|
+
|
|
99
|
+
def initialize(chat_peer = nil, message_id = nil, broadcast = nil)
|
|
100
|
+
@chat_peer = chat_peer
|
|
101
|
+
@message_id = message_id
|
|
102
|
+
@broadcast = broadcast
|
|
103
|
+
@client = nil
|
|
104
|
+
@original_update = nil
|
|
105
|
+
@entities = {}
|
|
106
|
+
@chat = nil
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def attach(client, update, entities)
|
|
110
|
+
@client = client
|
|
111
|
+
@original_update = update
|
|
112
|
+
@entities = entities
|
|
113
|
+
self
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def kind
|
|
117
|
+
"Event"
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def chat_id
|
|
121
|
+
@chat_peer.nil? ? nil : Events.peer_id_of(@chat_peer)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def private?
|
|
125
|
+
id = chat_id
|
|
126
|
+
!id.nil? && id.positive?
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# A basic group or a megagroup: true, false, or nil for a channel whose kind is not known yet.
|
|
130
|
+
def group?
|
|
131
|
+
id = chat_id
|
|
132
|
+
return false if id.nil? || id.positive?
|
|
133
|
+
return true if id > -Client::CHANNEL_BASE
|
|
134
|
+
|
|
135
|
+
broadcast = @broadcast
|
|
136
|
+
broadcast = chat["broadcast"] ? true : false if broadcast.nil? && chat
|
|
137
|
+
broadcast.nil? ? nil : !broadcast
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def channel?
|
|
141
|
+
id = chat_id
|
|
142
|
+
!id.nil? && id <= -Client::CHANNEL_BASE
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# The chat as the update gave it, or nil; get_chat fetches it when it did not.
|
|
146
|
+
def chat
|
|
147
|
+
id = chat_id
|
|
148
|
+
id && @entities[id]
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def get_chat
|
|
152
|
+
@chat ||= chat || (chat_id && @client.get_entity(chat_id))
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def input_chat
|
|
156
|
+
id = chat_id
|
|
157
|
+
id && @client.input_peer(id)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def respond(text)
|
|
161
|
+
@client.send_message(chat_id, text)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def to_s
|
|
165
|
+
"#<Telethon::Events::#{kind} chat=#{chat_id.inspect}>"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def inspect
|
|
169
|
+
to_s
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
module Events
|
|
5
|
+
# Telethon's events.MessageDeleted. Telegram does not say which chat a deletion outside channels
|
|
6
|
+
# was in, so chat_id is nil for those and a chats filter never matches them.
|
|
7
|
+
class MessageDeleted < Builder
|
|
8
|
+
def kind
|
|
9
|
+
"MessageDeleted"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def build(update, _self_id)
|
|
13
|
+
case update.name
|
|
14
|
+
when "updateDeleteMessages"
|
|
15
|
+
MessageDeleted::Event.new(update["messages"], nil)
|
|
16
|
+
when "updateDeleteChannelMessages"
|
|
17
|
+
MessageDeleted::Event.new(update["messages"], TL.make("peerChannel", "channel_id" => update["channel_id"]))
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class Event < Common
|
|
22
|
+
attr_reader :deleted_id, :deleted_ids
|
|
23
|
+
|
|
24
|
+
def initialize(deleted_ids, peer)
|
|
25
|
+
super(peer, deleted_ids.empty? ? 0 : deleted_ids[0])
|
|
26
|
+
@deleted_id = deleted_ids.empty? ? nil : deleted_ids[0]
|
|
27
|
+
@deleted_ids = deleted_ids
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def kind
|
|
31
|
+
"MessageDeleted"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Telethon's events.MessageRead: messages read up to max_id, by the other side (outbox) or by
|
|
37
|
+
# this account (inbox: pass inbox: true), or their contents (a voice note listened to).
|
|
38
|
+
class MessageRead < Builder
|
|
39
|
+
attr_reader :inbox
|
|
40
|
+
|
|
41
|
+
def initialize(chats: nil, blacklist_chats: false, func: nil, inbox: false)
|
|
42
|
+
super(chats: chats, blacklist_chats: blacklist_chats, func: func)
|
|
43
|
+
@inbox = inbox
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def kind
|
|
47
|
+
"MessageRead"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def build(update, _self_id)
|
|
51
|
+
case update.name
|
|
52
|
+
when "updateReadHistoryInbox"
|
|
53
|
+
MessageRead::Event.new(update["peer"], update["max_id"], false)
|
|
54
|
+
when "updateReadHistoryOutbox"
|
|
55
|
+
MessageRead::Event.new(update["peer"], update["max_id"], true)
|
|
56
|
+
when "updateReadChannelInbox"
|
|
57
|
+
MessageRead::Event.new(MessageRead.channel(update), update["max_id"], false)
|
|
58
|
+
when "updateReadChannelOutbox"
|
|
59
|
+
MessageRead::Event.new(MessageRead.channel(update), update["max_id"], true)
|
|
60
|
+
when "updateReadMessagesContents"
|
|
61
|
+
MessageRead::Event.new(nil, nil, false, true, update["messages"])
|
|
62
|
+
when "updateChannelReadMessagesContents"
|
|
63
|
+
MessageRead::Event.new(MessageRead.channel(update), nil, false, true, update["messages"])
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.channel(update)
|
|
68
|
+
TL.make("peerChannel", "channel_id" => update["channel_id"])
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def filter(event)
|
|
72
|
+
return false if @inbox == event.outbox?
|
|
73
|
+
|
|
74
|
+
super
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
class Event < Common
|
|
78
|
+
attr_reader :max_id, :message_ids
|
|
79
|
+
|
|
80
|
+
def initialize(peer, max_id, outbox, contents = false, message_ids = nil)
|
|
81
|
+
@outbox = outbox
|
|
82
|
+
@contents = contents
|
|
83
|
+
@message_ids = message_ids || []
|
|
84
|
+
@messages = nil
|
|
85
|
+
@max_id = max_id || (@message_ids.empty? ? nil : @message_ids.max)
|
|
86
|
+
super(peer, @max_id)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def kind
|
|
90
|
+
"MessageRead"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def outbox?
|
|
94
|
+
@outbox
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def inbox?
|
|
98
|
+
!@outbox
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def contents?
|
|
102
|
+
@contents
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Whether a message id (or each of several) is read by now.
|
|
106
|
+
def read?(message)
|
|
107
|
+
return message.map { |m| read?(m) } if message.is_a?(Array)
|
|
108
|
+
|
|
109
|
+
(message.is_a?(Integer) ? message : message.id) <= @max_id
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# The messages whose contents were read, fetched once.
|
|
113
|
+
def get_messages
|
|
114
|
+
@messages ||= chat_id ? @client.get_messages(chat_id, @message_ids).compact : []
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|