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,577 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
class Client
|
|
5
|
+
MAX_PARTICIPANTS_CHUNK = 200
|
|
6
|
+
MAX_ADMIN_LOG_CHUNK = 100
|
|
7
|
+
MAX_PROFILE_PHOTO_CHUNK = 100
|
|
8
|
+
# The participant filters that take a search text.
|
|
9
|
+
QUERY_FILTERS = %w[channelParticipantsBanned channelParticipantsKicked channelParticipantsSearch
|
|
10
|
+
channelParticipantsContacts].freeze
|
|
11
|
+
ADMIN_PERMISSIONS = %w[change_info post_messages edit_messages delete_messages ban_users invite_users pin_messages
|
|
12
|
+
add_admins anonymous manage_call].freeze
|
|
13
|
+
|
|
14
|
+
# ---- participants ----
|
|
15
|
+
|
|
16
|
+
# Yields the users of a group or channel, each with its participant entry in user.participant,
|
|
17
|
+
# as Telethon's iter_participants. search filters by name and username; filter is a
|
|
18
|
+
# channelParticipants* value or its name ("channelParticipantsAdmins"). In a private chat it is
|
|
19
|
+
# the other user. Answers how many there are in all.
|
|
20
|
+
def iter_participants(entity, limit: nil, search: "", filter: nil, aggressive: false)
|
|
21
|
+
_ = aggressive
|
|
22
|
+
filter = participants_filter(filter)
|
|
23
|
+
peer = get_input_entity(entity)
|
|
24
|
+
type = Client.entity_type(peer)
|
|
25
|
+
search_text = search.to_s
|
|
26
|
+
folded = search_text.downcase(:fold)
|
|
27
|
+
matching = !search_text.empty? && (!filter.nil? || type != :channel)
|
|
28
|
+
left = limit.nil? ? nil : [limit, 0].max
|
|
29
|
+
|
|
30
|
+
if type == :chat
|
|
31
|
+
full = call(TL.make("messages.getFullChat", "chat_id" => peer["chat_id"]))
|
|
32
|
+
participants = full["full_chat"]["participants"]
|
|
33
|
+
return 0 unless participants.is?("chatParticipants")
|
|
34
|
+
|
|
35
|
+
users = {}
|
|
36
|
+
full["users"].each { |u| users[u["id"]] = u }
|
|
37
|
+
participants["participants"].each do |participant|
|
|
38
|
+
next if participant.is?("channelParticipantLeft")
|
|
39
|
+
|
|
40
|
+
user = users[participant.is?("channelParticipantBanned") ? participant["peer"]["user_id"] : participant["user_id"]]
|
|
41
|
+
next if matching && !Client.matches_search?(user, folded)
|
|
42
|
+
break if left&.zero?
|
|
43
|
+
|
|
44
|
+
user.participant = participant
|
|
45
|
+
yield user
|
|
46
|
+
left -= 1 if left
|
|
47
|
+
end
|
|
48
|
+
return participants["participants"].size
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
unless type == :channel
|
|
52
|
+
return 1 if left&.zero?
|
|
53
|
+
|
|
54
|
+
user = get_entity(peer)
|
|
55
|
+
if !matching || Client.matches_search?(user, folded)
|
|
56
|
+
user.participant = nil
|
|
57
|
+
yield user
|
|
58
|
+
end
|
|
59
|
+
return 1
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
channel = input_channel(peer)
|
|
63
|
+
if left&.zero?
|
|
64
|
+
return call(TL.make("channels.getFullChannel", "channel" => channel))["full_chat"]["participants_count"]
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
request = TL.make("channels.getParticipants", "channel" => channel,
|
|
68
|
+
"filter" => filter || TL.make("channelParticipantsSearch", "q" => search_text),
|
|
69
|
+
"offset" => 0, "limit" => MAX_PARTICIPANTS_CHUNK, "hash" => 0)
|
|
70
|
+
seen = {}
|
|
71
|
+
total = nil
|
|
72
|
+
loop do
|
|
73
|
+
if limit
|
|
74
|
+
room = limit - request["offset"]
|
|
75
|
+
request["limit"] = room < MAX_PARTICIPANTS_CHUNK ? room : MAX_PARTICIPANTS_CHUNK
|
|
76
|
+
break if request["offset"] > limit
|
|
77
|
+
else
|
|
78
|
+
request["limit"] = MAX_PARTICIPANTS_CHUNK
|
|
79
|
+
end
|
|
80
|
+
if total.nil?
|
|
81
|
+
f = request["filter"]
|
|
82
|
+
unless f.is?("channelParticipantsRecent") || (f.is?("channelParticipantsSearch") && f["q"].to_s.empty?)
|
|
83
|
+
total = call(TL.make("channels.getParticipants", "channel" => channel, "filter" => TL.make("channelParticipantsRecent"),
|
|
84
|
+
"offset" => 0, "limit" => 1, "hash" => 0))["count"]
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
answer = call(request)
|
|
88
|
+
total = answer["count"] if total.nil?
|
|
89
|
+
break if answer["users"].empty?
|
|
90
|
+
|
|
91
|
+
request["offset"] += answer["participants"].size
|
|
92
|
+
users = {}
|
|
93
|
+
answer["users"].each { |u| users[u["id"]] = u }
|
|
94
|
+
buffer = []
|
|
95
|
+
answer["participants"].each do |participant|
|
|
96
|
+
next if participant.is?("channelParticipantLeft")
|
|
97
|
+
|
|
98
|
+
if participant.is?("channelParticipantBanned")
|
|
99
|
+
next unless participant["peer"].is?("peerUser")
|
|
100
|
+
|
|
101
|
+
user_id = participant["peer"]["user_id"]
|
|
102
|
+
else
|
|
103
|
+
user_id = participant["user_id"]
|
|
104
|
+
end
|
|
105
|
+
user = users[user_id]
|
|
106
|
+
next if (matching && !Client.matches_search?(user, folded)) || seen.key?(user["id"])
|
|
107
|
+
|
|
108
|
+
seen[user_id] = true
|
|
109
|
+
user.participant = participant
|
|
110
|
+
buffer << user
|
|
111
|
+
end
|
|
112
|
+
break if buffer.empty?
|
|
113
|
+
|
|
114
|
+
buffer.each do |user|
|
|
115
|
+
yield user
|
|
116
|
+
next if left.nil?
|
|
117
|
+
|
|
118
|
+
left -= 1
|
|
119
|
+
return total if left.zero?
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
total
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def get_participants(entity, limit: nil, search: "", filter: nil, aggressive: false)
|
|
126
|
+
list = TotalList.new
|
|
127
|
+
list.total = iter_participants(entity, limit: limit, search: search, filter: filter, aggressive: aggressive) { |u| list << u }
|
|
128
|
+
list
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Telethon's search match: the display name or the username contains the text, case folded.
|
|
132
|
+
def self.matches_search?(user, folded)
|
|
133
|
+
return true if Custom.display_name(user).downcase(:fold).include?(folded)
|
|
134
|
+
|
|
135
|
+
(user["username"] || "").downcase(:fold).include?(folded)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# ---- admin log ----
|
|
139
|
+
|
|
140
|
+
# Yields a channel's admin log as AdminLogEvents, newest first. The flags (join: true, ban: true...)
|
|
141
|
+
# keep only those kinds; admins keeps those by these users. Answers nil.
|
|
142
|
+
def iter_admin_log(entity, limit: nil, max_id: 0, min_id: 0, search: nil, admins: nil, join: nil, leave: nil,
|
|
143
|
+
invite: nil, restrict: nil, unrestrict: nil, ban: nil, unban: nil, promote: nil, demote: nil,
|
|
144
|
+
info: nil, settings: nil, pinned: nil, edit: nil, delete: nil, group_call: nil)
|
|
145
|
+
wanted = [join, leave, invite, restrict, unrestrict, ban, unban, promote, demote, info, settings, pinned, edit, delete,
|
|
146
|
+
group_call]
|
|
147
|
+
events_filter = nil
|
|
148
|
+
if wanted.any? { |w| w }
|
|
149
|
+
events_filter = TL.make("channelAdminLogEventsFilter",
|
|
150
|
+
"join" => join == true, "leave" => leave == true, "invite" => invite == true,
|
|
151
|
+
"ban" => restrict == true, "unban" => unrestrict == true, "kick" => ban == true,
|
|
152
|
+
"unkick" => unban == true, "promote" => promote == true, "demote" => demote == true,
|
|
153
|
+
"info" => info == true, "settings" => settings == true, "pinned" => pinned == true,
|
|
154
|
+
"edit" => edit == true, "delete" => delete == true, "group_call" => group_call == true)
|
|
155
|
+
end
|
|
156
|
+
peer = get_input_entity(entity)
|
|
157
|
+
admin_list = []
|
|
158
|
+
if admins
|
|
159
|
+
(admins.is_a?(Array) ? admins : [admins]).each { |a| admin_list << Client.input_user(get_input_entity(a)) }
|
|
160
|
+
end
|
|
161
|
+
request = TL.make("channels.getAdminLog", "channel" => input_channel(peer), "q" => search || "", "min_id" => min_id,
|
|
162
|
+
"max_id" => max_id, "limit" => 0, "events_filter" => events_filter,
|
|
163
|
+
"admins" => admin_list.empty? ? nil : admin_list)
|
|
164
|
+
left = limit.nil? ? nil : [limit, 0].max
|
|
165
|
+
return nil if left&.zero?
|
|
166
|
+
|
|
167
|
+
loop do
|
|
168
|
+
request["limit"] = left.nil? || left > MAX_ADMIN_LOG_CHUNK ? MAX_ADMIN_LOG_CHUNK : left
|
|
169
|
+
r = call(request)
|
|
170
|
+
known = entity_map(r)
|
|
171
|
+
lowest = nil
|
|
172
|
+
r["events"].each { |e| lowest = e["id"] if lowest.nil? || e["id"] < lowest }
|
|
173
|
+
request["max_id"] = lowest || 0
|
|
174
|
+
r["events"].each do |ev|
|
|
175
|
+
yield AdminLogEvent.new(self, ev, known, peer)
|
|
176
|
+
next if left.nil?
|
|
177
|
+
|
|
178
|
+
left -= 1
|
|
179
|
+
return nil if left.zero?
|
|
180
|
+
end
|
|
181
|
+
break if r["events"].size < request["limit"] || r["events"].empty?
|
|
182
|
+
end
|
|
183
|
+
nil
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def get_admin_log(entity, limit: nil, max_id: 0, min_id: 0, search: nil, admins: nil, join: nil, leave: nil,
|
|
187
|
+
invite: nil, restrict: nil, unrestrict: nil, ban: nil, unban: nil, promote: nil, demote: nil,
|
|
188
|
+
info: nil, settings: nil, pinned: nil, edit: nil, delete: nil, group_call: nil)
|
|
189
|
+
list = TotalList.new
|
|
190
|
+
iter_admin_log(entity, limit: limit, max_id: max_id, min_id: min_id, search: search, admins: admins, join: join,
|
|
191
|
+
leave: leave, invite: invite, restrict: restrict, unrestrict: unrestrict, ban: ban,
|
|
192
|
+
unban: unban, promote: promote, demote: demote, info: info, settings: settings,
|
|
193
|
+
pinned: pinned, edit: edit, delete: delete, group_call: group_call) { |e| list << e }
|
|
194
|
+
list
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# ---- profile photos ----
|
|
198
|
+
|
|
199
|
+
# Yields the profile photos of a user, or the photos a group or channel has had, newest first, as
|
|
200
|
+
# TL photo values. Answers how many there are in all, when Telegram says.
|
|
201
|
+
def iter_profile_photos(entity, limit: nil, offset: 0, max_id: 0)
|
|
202
|
+
peer = get_input_entity(entity)
|
|
203
|
+
request = if Client.entity_type(peer) == :user
|
|
204
|
+
TL.make("photos.getUserPhotos", "user_id" => Client.input_user(peer), "offset" => offset,
|
|
205
|
+
"max_id" => max_id, "limit" => 1)
|
|
206
|
+
else
|
|
207
|
+
TL.make("messages.search", "peer" => peer, "q" => "", "filter" => TL.make("inputMessagesFilterChatPhotos"),
|
|
208
|
+
"min_date" => 0, "max_date" => 0, "offset_id" => 0, "add_offset" => offset,
|
|
209
|
+
"limit" => 1, "max_id" => max_id, "min_id" => 0, "hash" => 0)
|
|
210
|
+
end
|
|
211
|
+
left = limit.nil? ? nil : [limit, 0].max
|
|
212
|
+
if left&.zero?
|
|
213
|
+
result = call(request)
|
|
214
|
+
return result["photos"].size if result.is?("photos.photos")
|
|
215
|
+
return result["messages"].size if result.is?("messages.messages")
|
|
216
|
+
|
|
217
|
+
return result["count"]
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
total = nil
|
|
221
|
+
loop do
|
|
222
|
+
request["limit"] = left.nil? || left > MAX_PROFILE_PHOTO_CHUNK ? MAX_PROFILE_PHOTO_CHUNK : left
|
|
223
|
+
result = call(request)
|
|
224
|
+
buffer = []
|
|
225
|
+
last_chunk = false
|
|
226
|
+
case result.name
|
|
227
|
+
when "photos.photos"
|
|
228
|
+
buffer = result["photos"]
|
|
229
|
+
total = buffer.size
|
|
230
|
+
last_chunk = true
|
|
231
|
+
when "messages.messages"
|
|
232
|
+
result["messages"].each { |x| buffer << x["action"]["photo"] if Client.chat_photo_action?(x) }
|
|
233
|
+
total = buffer.size
|
|
234
|
+
last_chunk = true
|
|
235
|
+
when "photos.photosSlice"
|
|
236
|
+
buffer = result["photos"]
|
|
237
|
+
total = result["count"]
|
|
238
|
+
if buffer.size < request["limit"]
|
|
239
|
+
last_chunk = true
|
|
240
|
+
else
|
|
241
|
+
request["offset"] += buffer.size
|
|
242
|
+
end
|
|
243
|
+
else
|
|
244
|
+
total = result["count"]
|
|
245
|
+
seen_id = nil
|
|
246
|
+
if result.is?("messages.channelMessages")
|
|
247
|
+
full = call(TL.make("channels.getFullChannel", "channel" => input_channel(request["peer"])))
|
|
248
|
+
photo = full["full_chat"]["chat_photo"]
|
|
249
|
+
if photo&.is?("photo")
|
|
250
|
+
buffer << photo
|
|
251
|
+
seen_id = photo["id"]
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
result["messages"].each do |x|
|
|
255
|
+
buffer << x["action"]["photo"] if Client.chat_photo_action?(x) && x["action"]["photo"]["id"] != seen_id
|
|
256
|
+
end
|
|
257
|
+
if result["messages"].size < request["limit"]
|
|
258
|
+
last_chunk = true
|
|
259
|
+
elsif !result["messages"].empty?
|
|
260
|
+
request["add_offset"] = 0
|
|
261
|
+
request["offset_id"] = result["messages"][-1]["id"]
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
break if buffer.empty?
|
|
265
|
+
|
|
266
|
+
buffer.each do |photo|
|
|
267
|
+
yield photo
|
|
268
|
+
next if left.nil?
|
|
269
|
+
|
|
270
|
+
left -= 1
|
|
271
|
+
return total if left.zero?
|
|
272
|
+
end
|
|
273
|
+
break if last_chunk
|
|
274
|
+
end
|
|
275
|
+
total
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def get_profile_photos(entity, limit: nil, offset: 0, max_id: 0)
|
|
279
|
+
list = TotalList.new
|
|
280
|
+
list.total = iter_profile_photos(entity, limit: limit, offset: offset, max_id: max_id) { |p| list << p }
|
|
281
|
+
list
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def self.chat_photo_action?(message)
|
|
285
|
+
action = message["action"]
|
|
286
|
+
action ? action.is?("messageActionChatEditPhoto") : false
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# ---- chat actions ----
|
|
290
|
+
|
|
291
|
+
# Shows "typing" (or another action) in a chat. With a block it is shown, repeated every delay
|
|
292
|
+
# seconds, while the block runs, and cancelled after unless auto_cancel is false; without one,
|
|
293
|
+
# call start and stop on the ChatAction it answers. "cancel" stops it at once and answers the
|
|
294
|
+
# call's result. action is a name ("typing", "photo", "record-voice"...) or a sendMessage*Action.
|
|
295
|
+
def action(entity, action, delay: 4, auto_cancel: true)
|
|
296
|
+
action = ChatAction.named(action) if action.is_a?(String)
|
|
297
|
+
unless action.is_a?(TL::Object) && TL.schema.named(action.name).result == "SendMessageAction"
|
|
298
|
+
raise ArgumentError, "Cannot use #{action.inspect} as action"
|
|
299
|
+
end
|
|
300
|
+
if action.is?("sendMessageCancelAction")
|
|
301
|
+
return call(TL.make("messages.setTyping", "peer" => get_input_entity(entity), "action" => action))
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
chat_action = ChatAction.new(self, entity, action, delay, auto_cancel)
|
|
305
|
+
return chat_action unless block_given?
|
|
306
|
+
|
|
307
|
+
chat_action.start
|
|
308
|
+
begin
|
|
309
|
+
yield chat_action
|
|
310
|
+
ensure
|
|
311
|
+
chat_action.stop
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
# ---- admin rights ----
|
|
316
|
+
|
|
317
|
+
# Makes user an admin of a group or channel, or changes what the admin may do. In a channel or
|
|
318
|
+
# megagroup each right left nil follows is_admin; in a basic group a user is an admin or not.
|
|
319
|
+
def edit_admin(entity, user, change_info: nil, post_messages: nil, edit_messages: nil, delete_messages: nil,
|
|
320
|
+
ban_users: nil, invite_users: nil, pin_messages: nil, add_admins: nil, manage_call: nil,
|
|
321
|
+
anonymous: nil, is_admin: nil, title: nil)
|
|
322
|
+
peer = get_input_entity(entity)
|
|
323
|
+
user = get_input_entity(user)
|
|
324
|
+
type = Client.entity_type(peer)
|
|
325
|
+
if type == :channel
|
|
326
|
+
if post_messages || edit_messages
|
|
327
|
+
channel_id = peer["channel_id"]
|
|
328
|
+
@megagroup_cache[channel_id] = get_entity(peer)["megagroup"] ? true : false unless @megagroup_cache.key?(channel_id)
|
|
329
|
+
if @megagroup_cache[channel_id]
|
|
330
|
+
post_messages = nil
|
|
331
|
+
edit_messages = nil
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
given = { "change_info" => change_info, "post_messages" => post_messages, "edit_messages" => edit_messages,
|
|
335
|
+
"delete_messages" => delete_messages, "ban_users" => ban_users, "invite_users" => invite_users,
|
|
336
|
+
"pin_messages" => pin_messages, "add_admins" => add_admins, "anonymous" => anonymous,
|
|
337
|
+
"manage_call" => manage_call }
|
|
338
|
+
rights = {}
|
|
339
|
+
ADMIN_PERMISSIONS.each do |name|
|
|
340
|
+
value = given[name].nil? ? is_admin : given[name]
|
|
341
|
+
rights[name] = value ? true : false
|
|
342
|
+
end
|
|
343
|
+
return call(TL.make("channels.editAdmin", "channel" => input_channel(peer), "user_id" => Client.input_user(user),
|
|
344
|
+
"admin_rights" => TL.make("chatAdminRights", rights), "rank" => title || ""))
|
|
345
|
+
end
|
|
346
|
+
raise ArgumentError, "You can only edit permissions in groups and channels" unless type == :chat
|
|
347
|
+
|
|
348
|
+
if is_admin.nil?
|
|
349
|
+
is_admin = [change_info, post_messages, edit_messages, delete_messages, ban_users, invite_users, pin_messages,
|
|
350
|
+
add_admins, anonymous, manage_call].any? { |v| v }
|
|
351
|
+
end
|
|
352
|
+
call(TL.make("messages.editChatAdmin", "chat_id" => peer["chat_id"], "user_id" => Client.input_user(user),
|
|
353
|
+
"is_admin" => is_admin ? true : false))
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
# Restricts a user in a channel or megagroup (or, with no user, everyone's default rights):
|
|
357
|
+
# each right false takes it away, until until_date (a Time or Unix time; nil is forever).
|
|
358
|
+
def edit_permissions(entity, user = nil, until_date = nil, view_messages: true, send_messages: true,
|
|
359
|
+
send_media: true, send_stickers: true, send_gifs: true, send_games: true, send_inline: true,
|
|
360
|
+
embed_link_previews: true, send_polls: true, change_info: true, invite_users: true,
|
|
361
|
+
pin_messages: true)
|
|
362
|
+
peer = get_input_entity(entity)
|
|
363
|
+
raise ArgumentError, "You must pass either a channel or a supergroup" unless Client.entity_type(peer) == :channel
|
|
364
|
+
|
|
365
|
+
until_date = until_date.to_i if until_date.is_a?(Time)
|
|
366
|
+
rights = TL.make("chatBannedRights", "until_date" => until_date || 0, "view_messages" => !view_messages,
|
|
367
|
+
"send_messages" => !send_messages, "send_media" => !send_media,
|
|
368
|
+
"send_stickers" => !send_stickers, "send_gifs" => !send_gifs,
|
|
369
|
+
"send_games" => !send_games, "send_inline" => !send_inline,
|
|
370
|
+
"embed_links" => !embed_link_previews, "send_polls" => !send_polls,
|
|
371
|
+
"change_info" => !change_info, "invite_users" => !invite_users,
|
|
372
|
+
"pin_messages" => !pin_messages)
|
|
373
|
+
if user.nil?
|
|
374
|
+
return call(TL.make("messages.editChatDefaultBannedRights", "peer" => peer, "banned_rights" => rights))
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
call(TL.make("channels.editBanned", "channel" => input_channel(peer), "participant" => get_input_entity(user),
|
|
378
|
+
"banned_rights" => rights))
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
# Removes a user from a group or channel without banning them (in a channel: banned, then let back
|
|
382
|
+
# in half a second later), or leaves it when user is "me". Answers the service message, or nil.
|
|
383
|
+
def kick_participant(entity, user)
|
|
384
|
+
peer = get_input_entity(entity)
|
|
385
|
+
user = get_input_entity(user)
|
|
386
|
+
case Client.entity_type(peer)
|
|
387
|
+
when :chat
|
|
388
|
+
resp = call(TL.make("messages.deleteChatUser", "chat_id" => peer["chat_id"], "user_id" => Client.input_user(user)))
|
|
389
|
+
when :channel
|
|
390
|
+
if user.is?("inputPeerSelf")
|
|
391
|
+
resp = call(TL.make("channels.leaveChannel", "channel" => input_channel(peer)))
|
|
392
|
+
else
|
|
393
|
+
resp = call(TL.make("channels.editBanned", "channel" => input_channel(peer), "participant" => user,
|
|
394
|
+
"banned_rights" => TL.make("chatBannedRights", "until_date" => 0,
|
|
395
|
+
"view_messages" => true)))
|
|
396
|
+
@kick_sleep.call(0.5)
|
|
397
|
+
call(TL.make("channels.editBanned", "channel" => input_channel(peer), "participant" => user,
|
|
398
|
+
"banned_rights" => TL.make("chatBannedRights", "until_date" => 0)))
|
|
399
|
+
end
|
|
400
|
+
else
|
|
401
|
+
raise ArgumentError, "You must pass either a channel or a chat"
|
|
402
|
+
end
|
|
403
|
+
# Telethon gives _get_response_message no request: the first new message the answer has.
|
|
404
|
+
found = response_parts(nil, resp, peer)
|
|
405
|
+
found[2]
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
# How a pause between a kick's two calls is taken (tests replace it).
|
|
409
|
+
attr_writer :kick_sleep
|
|
410
|
+
|
|
411
|
+
# What user may do in a group or channel, as a ParticipantPermissions. With no user, the chat's
|
|
412
|
+
# default banned rights (a chatBannedRights), or nil for a private chat.
|
|
413
|
+
def get_permissions(entity, user = nil)
|
|
414
|
+
entity = get_entity(entity)
|
|
415
|
+
if user.nil?
|
|
416
|
+
case entity.name
|
|
417
|
+
when "channel", "community"
|
|
418
|
+
full = call(TL.make("channels.getFullChannel", "channel" => input_channel(Custom.input_peer_of(self, entity))))
|
|
419
|
+
when "chat"
|
|
420
|
+
full = call(TL.make("messages.getFullChat", "chat_id" => entity["id"]))
|
|
421
|
+
else
|
|
422
|
+
return nil
|
|
423
|
+
end
|
|
424
|
+
return full["chats"][0]["default_banned_rights"]
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
peer = get_input_entity(entity)
|
|
428
|
+
user = get_input_entity(user)
|
|
429
|
+
case Client.entity_type(peer)
|
|
430
|
+
when :channel
|
|
431
|
+
participant = call(TL.make("channels.getParticipant", "channel" => input_channel(peer), "participant" => user))
|
|
432
|
+
ParticipantPermissions.new(participant["participant"], false)
|
|
433
|
+
when :chat
|
|
434
|
+
chat = call(TL.make("messages.getFullChat", "chat_id" => peer["chat_id"]))
|
|
435
|
+
user_id = user.is?("inputPeerSelf") ? @session.user_id : user["user_id"]
|
|
436
|
+
chat["full_chat"]["participants"]["participants"].each do |participant|
|
|
437
|
+
return ParticipantPermissions.new(participant, true) if participant["user_id"] == user_id
|
|
438
|
+
end
|
|
439
|
+
raise RPCError.new(400, "USER_NOT_PARTICIPANT", "messages.getFullChat")
|
|
440
|
+
else
|
|
441
|
+
raise ArgumentError, "You must pass either a channel or a chat"
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
# A channel's statistics (stats.broadcastStats, or stats.megagroupStats for a megagroup), or a
|
|
446
|
+
# message's (stats.messageStats), asked on the data center that keeps them.
|
|
447
|
+
def get_stats(entity, message = nil)
|
|
448
|
+
peer = get_input_entity(entity)
|
|
449
|
+
raise TypeError, "You must pass a channel entity" unless Client.entity_type(peer) == :channel
|
|
450
|
+
|
|
451
|
+
channel = input_channel(peer)
|
|
452
|
+
message = message.id if message.is_a?(Message)
|
|
453
|
+
request = if message.nil?
|
|
454
|
+
TL.make("stats.getBroadcastStats", "channel" => channel)
|
|
455
|
+
else
|
|
456
|
+
TL.make("stats.getMessageStats", "channel" => channel, "msg_id" => message)
|
|
457
|
+
end
|
|
458
|
+
begin
|
|
459
|
+
return call(request)
|
|
460
|
+
rescue RPCError => e
|
|
461
|
+
raise unless e.name == "STATS_MIGRATE" || (message.nil? && e.name == "BROADCAST_REQUIRED")
|
|
462
|
+
|
|
463
|
+
dc = e.value
|
|
464
|
+
if e.name == "BROADCAST_REQUIRED"
|
|
465
|
+
request = TL.make("stats.getMegagroupStats", "channel" => channel)
|
|
466
|
+
begin
|
|
467
|
+
return call(request)
|
|
468
|
+
rescue RPCError => e2
|
|
469
|
+
raise unless e2.name == "STATS_MIGRATE"
|
|
470
|
+
|
|
471
|
+
dc = e2.value
|
|
472
|
+
end
|
|
473
|
+
end
|
|
474
|
+
end
|
|
475
|
+
call_on(dc, request)
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
private
|
|
479
|
+
|
|
480
|
+
# A participants filter from its TL value or name; nil for none.
|
|
481
|
+
def participants_filter(filter)
|
|
482
|
+
return filter if filter.nil? || filter.is_a?(TL::Object)
|
|
483
|
+
|
|
484
|
+
name = filter.to_s
|
|
485
|
+
QUERY_FILTERS.include?(name) ? TL.make(name, "q" => "") : TL.make(name)
|
|
486
|
+
end
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
# Telethon's _ChatAction: shows an action in a chat, repeated every delay seconds from a thread of
|
|
490
|
+
# its own, until stopped.
|
|
491
|
+
class ChatAction
|
|
492
|
+
NAMES = { "typing" => "sendMessageTypingAction", "contact" => "sendMessageChooseContactAction",
|
|
493
|
+
"game" => "sendMessageGamePlayAction", "location" => "sendMessageGeoLocationAction",
|
|
494
|
+
"sticker" => "sendMessageChooseStickerAction", "record-audio" => "sendMessageRecordAudioAction",
|
|
495
|
+
"record-voice" => "sendMessageRecordAudioAction", "record-round" => "sendMessageRecordRoundAction",
|
|
496
|
+
"record-video" => "sendMessageRecordVideoAction", "audio" => "sendMessageUploadAudioAction",
|
|
497
|
+
"voice" => "sendMessageUploadAudioAction", "song" => "sendMessageUploadAudioAction",
|
|
498
|
+
"round" => "sendMessageUploadRoundAction", "video" => "sendMessageUploadVideoAction",
|
|
499
|
+
"photo" => "sendMessageUploadPhotoAction", "document" => "sendMessageUploadDocumentAction",
|
|
500
|
+
"file" => "sendMessageUploadDocumentAction", "cancel" => "sendMessageCancelAction" }.freeze
|
|
501
|
+
# The upload actions, which carry a progress (Telethon starts them at 1).
|
|
502
|
+
UPLOADS = %w[sendMessageUploadAudioAction sendMessageUploadRoundAction sendMessageUploadVideoAction
|
|
503
|
+
sendMessageUploadPhotoAction sendMessageUploadDocumentAction].freeze
|
|
504
|
+
|
|
505
|
+
def self.named(name)
|
|
506
|
+
found = NAMES[name.downcase]
|
|
507
|
+
raise ArgumentError, "No such action \"#{name}\"" unless found
|
|
508
|
+
|
|
509
|
+
UPLOADS.include?(found) ? TL.make(found, "progress" => 1) : TL.make(found)
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
attr_reader :action
|
|
513
|
+
|
|
514
|
+
def initialize(client, chat, action, delay, auto_cancel)
|
|
515
|
+
@client = client
|
|
516
|
+
@chat = chat
|
|
517
|
+
@action = action
|
|
518
|
+
@delay = delay
|
|
519
|
+
@auto_cancel = auto_cancel
|
|
520
|
+
@running = false
|
|
521
|
+
@thread = nil
|
|
522
|
+
@lock = Mutex.new
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
def start
|
|
526
|
+
@chat = @client.get_input_entity(@chat)
|
|
527
|
+
@running = true
|
|
528
|
+
@thread = Thread.new { repeat }
|
|
529
|
+
self
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
def stop
|
|
533
|
+
return unless @thread
|
|
534
|
+
|
|
535
|
+
@lock.lock
|
|
536
|
+
@running = false
|
|
537
|
+
@lock.unlock
|
|
538
|
+
thread = @thread
|
|
539
|
+
@thread = nil
|
|
540
|
+
thread.join
|
|
541
|
+
return unless @auto_cancel
|
|
542
|
+
|
|
543
|
+
@client.call(TL.make("messages.setTyping", "peer" => @chat, "action" => TL.make("sendMessageCancelAction")))
|
|
544
|
+
end
|
|
545
|
+
|
|
546
|
+
# For an upload action: how far it is, as Telethon's progress callback (100 * round(current / total)).
|
|
547
|
+
def progress(current, total)
|
|
548
|
+
return unless @action.key?("progress")
|
|
549
|
+
|
|
550
|
+
@action["progress"] = 100 * (current.to_f / total).round(half: :even)
|
|
551
|
+
end
|
|
552
|
+
|
|
553
|
+
private
|
|
554
|
+
|
|
555
|
+
def running?
|
|
556
|
+
@lock.lock
|
|
557
|
+
running = @running
|
|
558
|
+
@lock.unlock
|
|
559
|
+
running
|
|
560
|
+
end
|
|
561
|
+
|
|
562
|
+
def repeat
|
|
563
|
+
while running?
|
|
564
|
+
begin
|
|
565
|
+
@client.call(TL.make("messages.setTyping", "peer" => @chat, "action" => @action))
|
|
566
|
+
rescue TransportError, IOError, SystemCallError
|
|
567
|
+
return
|
|
568
|
+
end
|
|
569
|
+
waited = 0.0
|
|
570
|
+
while waited < @delay && running?
|
|
571
|
+
sleep(0.05)
|
|
572
|
+
waited += 0.05
|
|
573
|
+
end
|
|
574
|
+
end
|
|
575
|
+
end
|
|
576
|
+
end
|
|
577
|
+
end
|