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,423 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
# Telethon's tl/custom/message.py: a message (or service message) with the client that got it and
|
|
5
|
+
# the users and chats that came with it. The TL message is in raw, and each of its fields through
|
|
6
|
+
# [] (message["views"]); the common ones have methods, as do the actions on it.
|
|
7
|
+
class Message
|
|
8
|
+
include ChatGetter
|
|
9
|
+
include SenderGetter
|
|
10
|
+
|
|
11
|
+
attr_reader :raw, :client, :known_entities, :forward, :reply_to_chat, :reply_to_sender, :action_entities,
|
|
12
|
+
:via_bot, :linked_chat
|
|
13
|
+
|
|
14
|
+
def initialize(raw, client = nil, entities = {}, input_chat = nil)
|
|
15
|
+
@raw = raw
|
|
16
|
+
@client = nil
|
|
17
|
+
@known_entities = {}
|
|
18
|
+
@chat_peer = raw["peer_id"]
|
|
19
|
+
@chat = nil
|
|
20
|
+
@input_chat = nil
|
|
21
|
+
@broadcast = raw["post"] ? true : false
|
|
22
|
+
@sender_id = Client.sender_id(raw)
|
|
23
|
+
@sender = nil
|
|
24
|
+
@input_sender = nil
|
|
25
|
+
@out = raw["out"] ? true : false
|
|
26
|
+
@text = nil
|
|
27
|
+
@file = nil
|
|
28
|
+
@forward = nil
|
|
29
|
+
@reply_to_chat = nil
|
|
30
|
+
@reply_to_sender = nil
|
|
31
|
+
@action_entities = nil
|
|
32
|
+
@via_bot = nil
|
|
33
|
+
@linked_chat = nil
|
|
34
|
+
@reply_message = nil
|
|
35
|
+
attach(client, entities, input_chat) if client
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Telethon's _finish_init: the client, and the users and chats that came with the message.
|
|
39
|
+
def attach(client, entities, input_chat = nil)
|
|
40
|
+
@client = client
|
|
41
|
+
@known_entities = entities
|
|
42
|
+
# A message to oneself is outgoing unless it is a forward, as the official apps show it.
|
|
43
|
+
self_id = client.session.user_id
|
|
44
|
+
@out = true if self_id && @chat_peer && @chat_peer.is?("peerUser") && @chat_peer["user_id"] == self_id && !fwd_from
|
|
45
|
+
@sender = @sender_id && entities[@sender_id]
|
|
46
|
+
@chat = @chat_peer && entities[Client.peer_id(@chat_peer)]
|
|
47
|
+
@input_sender = nil
|
|
48
|
+
@input_chat = input_chat
|
|
49
|
+
via = @raw["via_bot_id"]
|
|
50
|
+
@via_bot = via && entities[via]
|
|
51
|
+
fwd = fwd_from
|
|
52
|
+
@forward = fwd ? Forward.new(client, fwd, entities) : nil
|
|
53
|
+
@action_entities = Message.action_entities(action, entities)
|
|
54
|
+
replies = @raw["replies"]
|
|
55
|
+
@linked_chat = entities[-(Client::CHANNEL_BASE + replies["channel_id"])] if replies && replies["channel_id"]
|
|
56
|
+
reply = @raw["reply_to"]
|
|
57
|
+
if reply && reply.is?("messageReplyHeader")
|
|
58
|
+
@reply_to_chat = entities[Client.peer_id(reply["reply_to_peer_id"])] if reply["reply_to_peer_id"]
|
|
59
|
+
from = reply["reply_from"]
|
|
60
|
+
@reply_to_sender = entities[Client.peer_id(from["from_id"])] if from && from["from_id"]
|
|
61
|
+
end
|
|
62
|
+
self
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# The users or chats a service message's action names, or nil for other actions.
|
|
66
|
+
def self.action_entities(action, entities)
|
|
67
|
+
return nil unless action
|
|
68
|
+
|
|
69
|
+
case action.name
|
|
70
|
+
when "messageActionChatAddUser", "messageActionChatCreate" then action["users"].map { |id| entities[id] }
|
|
71
|
+
when "messageActionChatDeleteUser" then [entities[action["user_id"]]]
|
|
72
|
+
when "messageActionChatJoinedByLink" then [entities[action["inviter_id"]]]
|
|
73
|
+
when "messageActionChatMigrateTo" then [entities[-(Client::CHANNEL_BASE + action["channel_id"])]]
|
|
74
|
+
when "messageActionChannelMigrateFrom" then [entities[-action["chat_id"]]]
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def [](key)
|
|
79
|
+
@raw[key]
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def is?(name)
|
|
83
|
+
@raw.is?(name)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def name
|
|
87
|
+
@raw.name
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def id
|
|
91
|
+
@raw["id"]
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def peer_id
|
|
95
|
+
@raw["peer_id"]
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def date
|
|
99
|
+
@raw["date"]
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def edit_date
|
|
103
|
+
@raw["edit_date"]
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# The text in the client's parse mode (markdown unless changed), nil for a service message, or
|
|
107
|
+
# for a message no client was given (as Telethon's).
|
|
108
|
+
def text
|
|
109
|
+
@text = @client.unparse_message_text(raw_text, entities) if @text.nil? && @client
|
|
110
|
+
@text
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Sets the text, parsed in the client's parse mode (the entities follow).
|
|
114
|
+
def text=(value)
|
|
115
|
+
@text = value
|
|
116
|
+
if @client
|
|
117
|
+
message, ents = @client.parse_message_text(value, :default)
|
|
118
|
+
@raw["message"] = message
|
|
119
|
+
@raw["entities"] = ents
|
|
120
|
+
else
|
|
121
|
+
@raw["message"] = value
|
|
122
|
+
@raw["entities"] = []
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# The text without formatting (Telethon's raw_text and message), nil for a service message.
|
|
127
|
+
def raw_text
|
|
128
|
+
@raw["message"]
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def raw_text=(value)
|
|
132
|
+
@raw["message"] = value
|
|
133
|
+
@raw["entities"] = []
|
|
134
|
+
@text = nil
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# The formatting: TL messageEntity values.
|
|
138
|
+
def entities
|
|
139
|
+
@raw["entities"]
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def out?
|
|
143
|
+
@out
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def mentioned?
|
|
147
|
+
@raw["mentioned"] ? true : false
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def media_unread?
|
|
151
|
+
@raw["media_unread"] ? true : false
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def silent?
|
|
155
|
+
@raw["silent"] ? true : false
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def post?
|
|
159
|
+
@raw["post"] ? true : false
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def pinned?
|
|
163
|
+
@raw["pinned"] ? true : false
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def service?
|
|
167
|
+
@raw.is?("messageService")
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def action
|
|
171
|
+
@raw["action"]
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def media
|
|
175
|
+
@raw["media"]
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def fwd_from
|
|
179
|
+
@raw["fwd_from"]
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def grouped_id
|
|
183
|
+
@raw["grouped_id"]
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def views
|
|
187
|
+
@raw["views"]
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def reply_markup
|
|
191
|
+
@raw["reply_markup"]
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def via_bot_id
|
|
195
|
+
@raw["via_bot_id"]
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def reply_to
|
|
199
|
+
@raw["reply_to"]
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# Replies to a message (or a story).
|
|
203
|
+
def reply?
|
|
204
|
+
!@raw["reply_to"].nil?
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def reply_to_msg_id
|
|
208
|
+
reply = @raw["reply_to"]
|
|
209
|
+
reply && reply.is?("messageReplyHeader") ? reply["reply_to_msg_id"] : nil
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def via_input_bot
|
|
213
|
+
@via_bot && Custom.input_peer_of(@client, @via_bot)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# The peer the message was sent to: this account in a private chat it received, else the chat.
|
|
217
|
+
def to_id
|
|
218
|
+
return TL.make("peerUser", "user_id" => @client.session.user_id) if @client && !@out && private?
|
|
219
|
+
|
|
220
|
+
@chat_peer
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# The photo or document as a FileInfo (Telethon's message.file), nil for other media.
|
|
224
|
+
def file
|
|
225
|
+
if @file.nil?
|
|
226
|
+
found = photo || document
|
|
227
|
+
@file = FileInfo.new(found) if found
|
|
228
|
+
end
|
|
229
|
+
@file
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# The photo of photo media, of a chat photo change, or of a link preview.
|
|
233
|
+
def photo
|
|
234
|
+
m = media
|
|
235
|
+
if m && m.is?("messageMediaPhoto")
|
|
236
|
+
p = m["photo"]
|
|
237
|
+
return p && p.is?("photo") ? p : nil
|
|
238
|
+
end
|
|
239
|
+
a = action
|
|
240
|
+
return a["photo"] if a && a.is?("messageActionChatEditPhoto")
|
|
241
|
+
|
|
242
|
+
web = web_preview
|
|
243
|
+
web && web["photo"] && web["photo"].is?("photo") ? web["photo"] : nil
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def document
|
|
247
|
+
m = media
|
|
248
|
+
if m && m.is?("messageMediaDocument")
|
|
249
|
+
d = m["document"]
|
|
250
|
+
return d && d.is?("document") ? d : nil
|
|
251
|
+
end
|
|
252
|
+
web = web_preview
|
|
253
|
+
web && web["document"] && web["document"].is?("document") ? web["document"] : nil
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def web_preview
|
|
257
|
+
m = media
|
|
258
|
+
m && m.is?("messageMediaWebPage") && m["webpage"] && m["webpage"].is?("webPage") ? m["webpage"] : nil
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def audio
|
|
262
|
+
document_by_attribute("documentAttributeAudio", "voice", false)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def voice
|
|
266
|
+
document_by_attribute("documentAttributeAudio", "voice", true)
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def video
|
|
270
|
+
document_by_attribute("documentAttributeVideo", nil, nil)
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def video_note
|
|
274
|
+
document_by_attribute("documentAttributeVideo", "round_message", true)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def gif
|
|
278
|
+
document_by_attribute("documentAttributeAnimated", nil, nil)
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def sticker
|
|
282
|
+
document_by_attribute("documentAttributeSticker", nil, nil)
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def contact
|
|
286
|
+
media_of(%w[messageMediaContact])
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def game
|
|
290
|
+
m = media_of(%w[messageMediaGame])
|
|
291
|
+
m && m["game"]
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def geo
|
|
295
|
+
m = media_of(%w[messageMediaGeo messageMediaGeoLive messageMediaVenue])
|
|
296
|
+
m && m["geo"]
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def invoice
|
|
300
|
+
media_of(%w[messageMediaInvoice])
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
def poll
|
|
304
|
+
media_of(%w[messageMediaPoll])
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def venue
|
|
308
|
+
media_of(%w[messageMediaVenue])
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
def dice
|
|
312
|
+
media_of(%w[messageMediaDice])
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
# The file name of a document (Telethon's message.file.name), nil for other media.
|
|
316
|
+
def file_name
|
|
317
|
+
f = file
|
|
318
|
+
f && f.name
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# [entity, text it covers] for each formatting entity, or those of the given TL names.
|
|
322
|
+
def get_entities_text(names = nil)
|
|
323
|
+
ents = entities
|
|
324
|
+
return [] if ents.nil? || ents.empty?
|
|
325
|
+
|
|
326
|
+
ents = ents.select { |e| names.include?(e.name) } if names
|
|
327
|
+
texts = Text.inner_text(raw_text, ents)
|
|
328
|
+
out = []
|
|
329
|
+
ents.each_with_index { |e, i| out << [e, texts[i]] }
|
|
330
|
+
out
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
# The message this one replies to, or nil.
|
|
334
|
+
def get_reply_message
|
|
335
|
+
if @reply_message.nil? && @client
|
|
336
|
+
wanted = reply_to_msg_id
|
|
337
|
+
return nil unless wanted
|
|
338
|
+
|
|
339
|
+
@reply_message = @client.get_messages(target, [wanted])[0]
|
|
340
|
+
end
|
|
341
|
+
@reply_message
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# Sends text to the same chat; answers the new Message.
|
|
345
|
+
def respond(text, parse_mode: :default, link_preview: true, silent: false)
|
|
346
|
+
@client.send_message(target, text, parse_mode: parse_mode, link_preview: link_preview, silent: silent)
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
# Sends text as a reply to this message; answers the new Message.
|
|
350
|
+
def reply(text, parse_mode: :default, link_preview: true, silent: false)
|
|
351
|
+
@client.send_message(target, text, reply_to: id, parse_mode: parse_mode, link_preview: link_preview,
|
|
352
|
+
silent: silent)
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# Forwards to another chat; answers the new Message.
|
|
356
|
+
def forward_to(destination, drop_author: false)
|
|
357
|
+
@client.forward_messages(destination, [id], target, drop_author: drop_author)[0]
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
# Edits the text. As Telethon's Message#edit, keeps the link preview the message has unless told.
|
|
361
|
+
def edit(text, parse_mode: :default, link_preview: nil)
|
|
362
|
+
link_preview = !web_preview.nil? if link_preview.nil?
|
|
363
|
+
@client.edit_message(target, id, text, parse_mode: parse_mode, link_preview: link_preview)
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
def delete(revoke: true)
|
|
367
|
+
@client.delete_messages(target, [id], revoke: revoke)
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
# Saves the message's media: Client#download_media for this message.
|
|
371
|
+
def download_media(file = nil, thumb: nil, progress_callback: nil)
|
|
372
|
+
@client.download_media(self, file, thumb: thumb, progress_callback: progress_callback)
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# Marks the chat read up to this message.
|
|
376
|
+
def mark_read
|
|
377
|
+
@client.send_read_acknowledge(target, max_id: id)
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
def pin(notify: false, pm_oneside: false)
|
|
381
|
+
@client.pin_message(target, id, notify: notify, pm_oneside: pm_oneside)
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
def unpin
|
|
385
|
+
@client.unpin_message(target, id)
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
def to_s
|
|
389
|
+
"#<Telethon::Message #{chat_id}/#{id} #{(raw_text || (action && action.name)).inspect}>"
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
def inspect
|
|
393
|
+
to_s
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
private
|
|
397
|
+
|
|
398
|
+
# The chat to act in: its marked id, which the client turns into an input peer.
|
|
399
|
+
def target
|
|
400
|
+
chat_id
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
def media_of(names)
|
|
404
|
+
m = media
|
|
405
|
+
m && names.include?(m.name) ? m : nil
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
# Telethon's _document_by_attribute: the document when its first attribute of that kind has
|
|
409
|
+
# field == want (or any, with no field).
|
|
410
|
+
def document_by_attribute(kind, field, want)
|
|
411
|
+
doc = document
|
|
412
|
+
return nil unless doc
|
|
413
|
+
|
|
414
|
+
doc["attributes"].each do |attr|
|
|
415
|
+
next unless attr.is?(kind)
|
|
416
|
+
return doc if field.nil?
|
|
417
|
+
|
|
418
|
+
return (attr[field] ? true : false) == want ? doc : nil
|
|
419
|
+
end
|
|
420
|
+
nil
|
|
421
|
+
end
|
|
422
|
+
end
|
|
423
|
+
end
|