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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +96 -0
  3. data/README.md +103 -8
  4. data/lib/telethon/cli/commands.rb +25 -20
  5. data/lib/telethon/client/auth.rb +22 -1
  6. data/lib/telethon/client/chats.rb +577 -0
  7. data/lib/telethon/client/dialogs.rb +211 -0
  8. data/lib/telethon/client/downloads.rb +659 -0
  9. data/lib/telethon/client/entities.rb +50 -2
  10. data/lib/telethon/client/files.rb +341 -192
  11. data/lib/telethon/client/messages.rb +264 -40
  12. data/lib/telethon/client/updates.rb +407 -0
  13. data/lib/telethon/client.rb +119 -31
  14. data/lib/telethon/crypto/rsa.rb +81 -5
  15. data/lib/telethon/custom/admin_log_event.rb +133 -0
  16. data/lib/telethon/custom/conversation.rb +358 -0
  17. data/lib/telethon/custom/dialog.rb +144 -0
  18. data/lib/telethon/custom/file.rb +130 -0
  19. data/lib/telethon/custom/forward.rb +63 -0
  20. data/lib/telethon/custom/getters.rb +152 -0
  21. data/lib/telethon/custom/participant_permissions.rb +100 -0
  22. data/lib/telethon/errors.rb +12 -0
  23. data/lib/telethon/events/album.rb +108 -0
  24. data/lib/telethon/events/chat_action.rb +212 -0
  25. data/lib/telethon/events/common.rb +173 -0
  26. data/lib/telethon/events/message_deleted.rb +119 -0
  27. data/lib/telethon/events/new_message.rb +176 -0
  28. data/lib/telethon/events/raw.rb +35 -0
  29. data/lib/telethon/events/user_update.rb +129 -0
  30. data/lib/telethon/extensions/html.rb +373 -0
  31. data/lib/telethon/extensions/markdown.rb +116 -0
  32. data/lib/telethon/extensions/text.rb +171 -0
  33. data/lib/telethon/message.rb +423 -0
  34. data/lib/telethon/mime.rb +1930 -0
  35. data/lib/telethon/mime_guess.rb +54 -0
  36. data/lib/telethon/network/authenticator.rb +5 -4
  37. data/lib/telethon/network/sender.rb +7 -0
  38. data/lib/telethon/private_files.rb +25 -0
  39. data/lib/telethon/session.rb +18 -0
  40. data/lib/telethon/tl/object.rb +4 -0
  41. data/lib/telethon/updates/message_box.rb +481 -0
  42. data/lib/telethon/version.rb +1 -1
  43. data/lib/telethon.rb +25 -0
  44. metadata +26 -1
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ # Telethon's tl/custom/draft.py: the draft of a dialog. text is the draft in markdown, raw_text
5
+ # without it.
6
+ class Draft
7
+ attr_reader :entity, :text, :raw_text, :date, :link_preview, :reply_to_msg_id
8
+
9
+ def initialize(client, entity, draft)
10
+ @client = client
11
+ @entity = entity
12
+ @peer_id = entity && Client.peer_id(entity)
13
+ @input_entity = entity && Custom.input_peer_of(client, entity)
14
+ draft = nil unless draft && draft.is?("draftMessage")
15
+ message = draft ? draft["message"] : ""
16
+ @text = Markdown.unparse(message, draft && draft["entities"])
17
+ @raw_text = message
18
+ @date = draft && draft["date"]
19
+ @link_preview = !(draft && draft["no_webpage"])
20
+ reply = draft && draft["reply_to"]
21
+ @reply_to_msg_id = reply && reply.is?("inputReplyToMessage") ? reply["reply_to_msg_id"] : nil
22
+ end
23
+
24
+ def input_entity
25
+ @input_entity ||= @peer_id && @client.input_peer(@peer_id)
26
+ end
27
+
28
+ def get_entity
29
+ @entity ||= @peer_id && @client.get_entity(@peer_id)
30
+ end
31
+
32
+ def get_input_entity
33
+ input_entity
34
+ end
35
+
36
+ def empty?
37
+ @text.nil? || @text.empty?
38
+ end
39
+
40
+ # Saves the draft (messages.saveDraft): the text in the client's parse mode unless parse_mode says
41
+ # otherwise, replying to reply_to (nil for none, :keep for the draft's).
42
+ def set_message(text = nil, reply_to: :keep, parse_mode: :default, link_preview: nil)
43
+ text = @text if text.nil?
44
+ reply_to = @reply_to_msg_id if reply_to == :keep
45
+ link_preview = @link_preview if link_preview.nil?
46
+ raw_text, entities = @client.parse_message_text(text, parse_mode)
47
+ fields = { "peer" => input_entity, "message" => raw_text, "no_webpage" => !link_preview, "entities" => entities }
48
+ fields["reply_to"] = TL.make("inputReplyToMessage", "reply_to_msg_id" => reply_to) if reply_to
49
+ result = @client.call(TL.make("messages.saveDraft", fields))
50
+ if result
51
+ @text = text
52
+ @raw_text = raw_text
53
+ @link_preview = link_preview
54
+ @reply_to_msg_id = reply_to
55
+ @date = Time.now.to_i
56
+ end
57
+ result
58
+ end
59
+
60
+ # Sends the draft as a message, clearing it unless clear is false.
61
+ def send_draft(clear: true, parse_mode: :default)
62
+ @client.send_message(input_entity, @text, reply_to: @reply_to_msg_id, link_preview: @link_preview,
63
+ parse_mode: parse_mode, clear_draft: clear)
64
+ end
65
+
66
+ def delete
67
+ set_message("")
68
+ end
69
+
70
+ def to_s
71
+ "#<Telethon::Draft #{@peer_id.inspect} #{@text.inspect}>"
72
+ end
73
+
74
+ def inspect
75
+ to_s
76
+ end
77
+ end
78
+
79
+ # Telethon's tl/custom/dialog.py: a chat in the dialog list, with its entity, last message, unread
80
+ # counts and draft.
81
+ class Dialog
82
+ attr_reader :dialog, :pinned, :folder_id, :archived, :message, :date, :entity, :input_entity, :id, :name,
83
+ :unread_count, :unread_mentions_count, :unread_reactions_count, :unread_poll_votes_count, :draft
84
+
85
+ def initialize(client, dialog, entities, message)
86
+ @client = client
87
+ @dialog = dialog
88
+ @pinned = dialog["pinned"] ? true : false
89
+ @folder_id = dialog["folder_id"]
90
+ @archived = !@folder_id.nil?
91
+ @message = message
92
+ @date = message && message.date
93
+ @entity = entities[Client.peer_id(dialog["peer"])]
94
+ @input_entity = Custom.input_peer_of(client, @entity)
95
+ @id = Client.peer_id(@entity)
96
+ @name = Custom.display_name(@entity)
97
+ @unread_count = dialog["unread_count"]
98
+ @unread_mentions_count = dialog["unread_mentions_count"]
99
+ @unread_reactions_count = dialog["unread_reactions_count"]
100
+ @unread_poll_votes_count = dialog["unread_poll_votes_count"]
101
+ @draft = Draft.new(client, @entity, dialog["draft"])
102
+ end
103
+
104
+ def title
105
+ @name
106
+ end
107
+
108
+ def user?
109
+ @entity.is?("user")
110
+ end
111
+
112
+ def group?
113
+ %w[chat chatForbidden].include?(@entity.name) || (@entity.is?("channel") && @entity["megagroup"] ? true : false)
114
+ end
115
+
116
+ def channel?
117
+ @entity.is?("channel")
118
+ end
119
+
120
+ # A community (layer 234), which Telethon's Dialog calls is_community.
121
+ def community?
122
+ @entity.is?("community")
123
+ end
124
+
125
+ def send_message(text, reply_to: nil, parse_mode: :default, link_preview: true, silent: false)
126
+ @client.send_message(@input_entity, text, reply_to: reply_to, parse_mode: parse_mode, link_preview: link_preview,
127
+ silent: silent)
128
+ end
129
+
130
+ # Moves the dialog to a folder: 1 is the archive, 0 takes it out.
131
+ def archive(folder = 1)
132
+ @client.call(TL.make("folders.editPeerFolders",
133
+ "folder_peers" => [TL.make("inputFolderPeer", "peer" => @input_entity, "folder_id" => folder)]))
134
+ end
135
+
136
+ def to_s
137
+ "#<Telethon::Dialog #{@id} #{@name.inspect}>"
138
+ end
139
+
140
+ def inspect
141
+ to_s
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ # Telethon's tl/custom/file.py: a message's photo or document with its attributes by name. What the
5
+ # media does not have is nil. The TL photo or document is in media.
6
+ class FileInfo
7
+ attr_reader :media
8
+
9
+ def initialize(media)
10
+ @media = media
11
+ end
12
+
13
+ def photo?
14
+ @media.is?("photo")
15
+ end
16
+
17
+ def document?
18
+ @media.is?("document")
19
+ end
20
+
21
+ # The documentAttributeFilename's name.
22
+ def name
23
+ from_attr(%w[documentAttributeFilename], "file_name")
24
+ end
25
+
26
+ # The extension for the mime type, else the name's (with its dot), else nil.
27
+ def ext
28
+ mime = mime_type
29
+ known = mime && Mime.guess_extension(mime)
30
+ return known if known
31
+
32
+ ext = File.extname(name || "")
33
+ ext.empty? ? nil : ext
34
+ end
35
+
36
+ def mime_type
37
+ return "image/jpeg" if photo?
38
+
39
+ @media["mime_type"] if document?
40
+ end
41
+
42
+ # In pixels, for a photo (its largest size) or a picture or video.
43
+ def width
44
+ return photo_max("w") if photo?
45
+
46
+ from_attr(%w[documentAttributeImageSize documentAttributeVideo], "w")
47
+ end
48
+
49
+ def height
50
+ return photo_max("h") if photo?
51
+
52
+ from_attr(%w[documentAttributeImageSize documentAttributeVideo], "h")
53
+ end
54
+
55
+ # In seconds, for audio or video.
56
+ def duration
57
+ from_attr(%w[documentAttributeAudio documentAttributeVideo], "duration")
58
+ end
59
+
60
+ def title
61
+ from_attr(%w[documentAttributeAudio], "title")
62
+ end
63
+
64
+ def performer
65
+ from_attr(%w[documentAttributeAudio], "performer")
66
+ end
67
+
68
+ # A sticker's emoji.
69
+ def emoji
70
+ from_attr(%w[documentAttributeSticker], "alt")
71
+ end
72
+
73
+ def sticker_set
74
+ from_attr(%w[documentAttributeSticker], "stickerset")
75
+ end
76
+
77
+ # In bytes: a document's size, or a photo's heaviest size.
78
+ def size
79
+ if photo?
80
+ best = nil
81
+ @media["sizes"].each do |s|
82
+ n = FileInfo.photo_size_bytes(s)
83
+ best = n if n && n.positive? && (best.nil? || n > best)
84
+ end
85
+ return best
86
+ end
87
+ @media["size"] if document?
88
+ end
89
+
90
+ # Telethon's utils._photo_size_byte_count.
91
+ def self.photo_size_bytes(size)
92
+ case size.name
93
+ when "photoSize" then size["size"]
94
+ when "photoStrippedSize"
95
+ bytes = size["bytes"]
96
+ bytes.bytesize < 3 || bytes.getbyte(0) != 1 ? bytes.bytesize : bytes.bytesize + 622
97
+ when "photoCachedSize" then size["bytes"].bytesize
98
+ when "photoSizeProgressive" then size["sizes"].max
99
+ end
100
+ end
101
+
102
+ def to_s
103
+ "#<Telethon::FileInfo #{mime_type} #{name.inspect} #{size.inspect}>"
104
+ end
105
+
106
+ def inspect
107
+ to_s
108
+ end
109
+
110
+ private
111
+
112
+ def photo_max(field)
113
+ best = 0
114
+ @media["sizes"].each do |s|
115
+ v = s.key?(field) ? s[field] : 0
116
+ best = v if v > best
117
+ end
118
+ best
119
+ end
120
+
121
+ def from_attr(names, field)
122
+ return nil unless document?
123
+
124
+ @media["attributes"].each do |attr|
125
+ return attr.key?(field) ? attr[field] : nil if names.include?(attr.name)
126
+ end
127
+ nil
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ # Telethon's tl/custom/forward.py: where a forwarded message came from. A user it came from is the
5
+ # sender; a chat or channel is the chat. The messageFwdHeader is in original_fwd, and its fields
6
+ # through [].
7
+ class Forward
8
+ include ChatGetter
9
+ include SenderGetter
10
+
11
+ attr_reader :original_fwd, :client
12
+
13
+ def initialize(client, original, entities)
14
+ @original_fwd = original
15
+ @client = client
16
+ @sender_id = nil
17
+ @sender = nil
18
+ @input_sender = nil
19
+ @chat_peer = nil
20
+ @chat = nil
21
+ @input_chat = nil
22
+ @broadcast = nil
23
+ from = original["from_id"]
24
+ return unless from
25
+
26
+ if from.is?("peerUser")
27
+ @sender_id = Client.peer_id(from)
28
+ @sender = entities[@sender_id]
29
+ else
30
+ @chat_peer = from
31
+ @chat = entities[Client.peer_id(from)]
32
+ end
33
+ end
34
+
35
+ def [](key)
36
+ @original_fwd[key]
37
+ end
38
+
39
+ def date
40
+ @original_fwd["date"]
41
+ end
42
+
43
+ def from_name
44
+ @original_fwd["from_name"]
45
+ end
46
+
47
+ def channel_post
48
+ @original_fwd["channel_post"]
49
+ end
50
+
51
+ def post_author
52
+ @original_fwd["post_author"]
53
+ end
54
+
55
+ def to_s
56
+ "#<Telethon::Forward sender=#{@sender_id.inspect} chat=#{chat_id.inspect}>"
57
+ end
58
+
59
+ def inspect
60
+ to_s
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ # Telethon's tl/custom/chatgetter.py: the chat something happened in, for Message and Forward. The
5
+ # class sets @chat_peer (a peer, or nil), @chat, @input_chat, @broadcast and @client.
6
+ module ChatGetter
7
+ # The chat as it came with the message, or nil; get_chat fetches it.
8
+ def chat
9
+ @chat
10
+ end
11
+
12
+ def get_chat
13
+ if (@chat.nil? || @chat["min"]) && get_input_chat
14
+ begin
15
+ @chat = @client.get_entity(chat_id)
16
+ rescue Error
17
+ nil
18
+ end
19
+ end
20
+ @chat
21
+ end
22
+
23
+ # The input peer of the chat, from the chat or the client's cache; nil when neither has it.
24
+ def input_chat
25
+ if @input_chat.nil? && @chat_peer && @client
26
+ @input_chat = @chat ? Custom.input_peer_of(@client, @chat) : @client.input_peer(chat_id)
27
+ end
28
+ @input_chat
29
+ end
30
+
31
+ # As input_chat, but looks the chat up (in the dialogs) when the cache does not have it.
32
+ def get_input_chat
33
+ if input_chat.nil? && @chat_peer && @client
34
+ begin
35
+ @input_chat = @client.input_peer_for(chat_id)
36
+ rescue Error
37
+ nil
38
+ end
39
+ end
40
+ @input_chat
41
+ end
42
+
43
+ def chat_id
44
+ @chat_peer && Client.peer_id(@chat_peer)
45
+ end
46
+
47
+ # true in a private chat, nil when there is no chat.
48
+ def private?
49
+ @chat_peer && @chat_peer.is?("peerUser")
50
+ end
51
+
52
+ # A basic group or megagroup: true or false, or nil for a channel whose kind is not known.
53
+ def group?
54
+ @broadcast = @chat["broadcast"] ? true : false if @broadcast.nil? && @chat && @chat.key?("broadcast")
55
+ if @chat_peer && @chat_peer.is?("peerChannel")
56
+ return @broadcast.nil? ? nil : !@broadcast
57
+ end
58
+
59
+ @chat_peer ? @chat_peer.is?("peerChat") : false
60
+ end
61
+
62
+ def channel?
63
+ @chat_peer ? @chat_peer.is?("peerChannel") : false
64
+ end
65
+ end
66
+
67
+ # Telethon's tl/custom/sendergetter.py: who sent something. The class sets @sender_id, @sender,
68
+ # @input_sender and @client.
69
+ module SenderGetter
70
+ def sender_id
71
+ @sender_id
72
+ end
73
+
74
+ # The sender as it came with the message, or nil; get_sender fetches it.
75
+ def sender
76
+ @sender
77
+ end
78
+
79
+ def get_sender
80
+ if (@sender.nil? || @sender["min"]) && get_input_sender
81
+ begin
82
+ @sender = @client.get_entity(@sender_id)
83
+ rescue Error
84
+ nil
85
+ end
86
+ end
87
+ @sender
88
+ end
89
+
90
+ def input_sender
91
+ if @input_sender.nil? && @sender_id && @client
92
+ @input_sender = @sender ? Custom.input_peer_of(@client, @sender) : @client.input_peer(@sender_id)
93
+ end
94
+ @input_sender
95
+ end
96
+
97
+ def get_input_sender
98
+ if input_sender.nil? && @sender_id && @client
99
+ begin
100
+ @input_sender = @client.input_peer_for(@sender_id)
101
+ rescue Error
102
+ nil
103
+ end
104
+ end
105
+ @input_sender
106
+ end
107
+ end
108
+
109
+ # Helpers the custom objects share.
110
+ module Custom
111
+ # utils.get_input_peer of a user, chat or channel (inputPeerSelf for the logged-in user).
112
+ def self.input_peer_of(client, entity)
113
+ return TL.make("inputPeerSelf") if entity.is?("user") && entity["self"]
114
+
115
+ case entity.name
116
+ when "user"
117
+ entity["access_hash"] ? TL.make("inputPeerUser", "user_id" => entity["id"], "access_hash" => entity["access_hash"]) : client.input_peer(entity["id"])
118
+ when "chat", "chatForbidden", "chatEmpty" then TL.make("inputPeerChat", "chat_id" => entity["id"])
119
+ when "channel", "channelForbidden", "community", "communityForbidden"
120
+ if entity["access_hash"]
121
+ TL.make("inputPeerChannel", "channel_id" => entity["id"], "access_hash" => entity["access_hash"])
122
+ else
123
+ client.input_peer(Client.peer_id(entity))
124
+ end
125
+ end
126
+ end
127
+
128
+ # utils.get_display_name: a user's first and last name, a chat's title, "" for anything else.
129
+ def self.display_name(entity)
130
+ return "" if entity.nil?
131
+
132
+ if entity.is?("user")
133
+ first = entity["first_name"]
134
+ last = entity["last_name"]
135
+ return "#{first} #{last}" if first && !first.empty? && last && !last.empty?
136
+ return first if first && !first.empty?
137
+ return last if last && !last.empty?
138
+
139
+ return ""
140
+ end
141
+ return entity["title"] || "" if %w[chat chatForbidden channel channelForbidden community communityForbidden].include?(entity.name)
142
+
143
+ ""
144
+ end
145
+
146
+ # The entity for a marked id among those that came with an answer (Telethon's _get_entity_pair,
147
+ # without the input half, which the getters work out when asked).
148
+ def self.entity(entities, id)
149
+ id.nil? ? nil : entities[id]
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ # Telethon's tl/custom/participantpermissions.py: what a participant may do, from its
5
+ # channelParticipant* or chatParticipant* entry (Client#get_permissions). In a basic group (chat)
6
+ # an admin may do everything but add admins, which only the creator may.
7
+ class ParticipantPermissions
8
+ attr_reader :participant
9
+
10
+ def initialize(participant, chat)
11
+ @participant = participant
12
+ @chat = chat
13
+ end
14
+
15
+ def chat?
16
+ @chat
17
+ end
18
+
19
+ def admin?
20
+ creator? || %w[channelParticipantAdmin chatParticipantAdmin].include?(@participant.name)
21
+ end
22
+
23
+ def creator?
24
+ %w[channelParticipantCreator chatParticipantCreator].include?(@participant.name)
25
+ end
26
+
27
+ def has_default_permissions?
28
+ %w[channelParticipant chatParticipant channelParticipantSelf].include?(@participant.name)
29
+ end
30
+
31
+ def banned?
32
+ @participant.is?("channelParticipantBanned")
33
+ end
34
+
35
+ def has_left?
36
+ @participant.is?("channelParticipantLeft")
37
+ end
38
+
39
+ def add_admins?
40
+ return false unless admin?
41
+ return creator? if @chat
42
+
43
+ @participant["admin_rights"]["add_admins"] ? true : false
44
+ end
45
+
46
+ def ban_users?
47
+ admin_right("ban_users")
48
+ end
49
+
50
+ def pin_messages?
51
+ admin_right("pin_messages")
52
+ end
53
+
54
+ def invite_users?
55
+ admin_right("invite_users")
56
+ end
57
+
58
+ def delete_messages?
59
+ admin_right("delete_messages")
60
+ end
61
+
62
+ def edit_messages?
63
+ admin_right("edit_messages")
64
+ end
65
+
66
+ def post_messages?
67
+ admin_right("post_messages")
68
+ end
69
+
70
+ def change_info?
71
+ admin_right("change_info")
72
+ end
73
+
74
+ def anonymous?
75
+ admin_right("anonymous")
76
+ end
77
+
78
+ def manage_call?
79
+ admin_right("manage_call")
80
+ end
81
+
82
+ def to_s
83
+ "#<Telethon::ParticipantPermissions #{@participant.name}>"
84
+ end
85
+
86
+ def inspect
87
+ to_s
88
+ end
89
+
90
+ private
91
+
92
+ # Telethon's _admin_prop: false for a non-admin, true for a chat admin, else the admin right.
93
+ def admin_right(name)
94
+ return false unless admin?
95
+ return true if @chat
96
+
97
+ @participant["admin_rights"][name] ? true : false
98
+ end
99
+ end
100
+ end
@@ -49,4 +49,16 @@ module Telethon
49
49
  @name == "FLOOD_WAIT" || @name == "FLOOD_PREMIUM_WAIT"
50
50
  end
51
51
  end
52
+
53
+ # A Conversation's wait ran past its timeout (Telethon raises asyncio.TimeoutError).
54
+ class TimeoutError < Error
55
+ end
56
+
57
+ # A Conversation's wait was cancelled, or the conversation used after it was (asyncio.CancelledError).
58
+ class CancelledError < Error
59
+ end
60
+
61
+ # An exclusive Conversation was opened in a chat that has one open already.
62
+ class AlreadyInConversationError < Error
63
+ end
52
64
  end