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,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ module Events
5
+ # Telethon's events.NewMessage: a message arriving, sent from this account elsewhere (outgoing) or
6
+ # to it (incoming). Messages this client sends itself are not events, as in Telethon v1.
7
+ #
8
+ # client.on(Telethon::Events::NewMessage.new(pattern: /\A\/start/)) { |event| event.reply("hi") }
9
+ class NewMessage < Builder
10
+ attr_reader :incoming, :outgoing, :from_users, :forwards
11
+
12
+ # pattern: a Regexp (or a String to make one) matched at the start of the text, or a Proc given
13
+ # the text; what it answers is in event.pattern_match. from_users limits the senders, forwards
14
+ # keeps only forwards (true) or only the others (false).
15
+ def initialize(chats: nil, blacklist_chats: false, func: nil, incoming: nil, outgoing: nil, from_users: nil,
16
+ forwards: nil, pattern: nil)
17
+ if incoming && outgoing
18
+ incoming = nil
19
+ outgoing = nil
20
+ elsif !incoming.nil? && outgoing.nil?
21
+ outgoing = !incoming
22
+ elsif !outgoing.nil? && incoming.nil?
23
+ incoming = !outgoing
24
+ elsif incoming == false && outgoing == false
25
+ raise ArgumentError, "Don't create an event handler if you don't want neither incoming nor outgoing!"
26
+ end
27
+ super(chats: chats, blacklist_chats: blacklist_chats, func: func)
28
+ @incoming = incoming
29
+ @outgoing = outgoing
30
+ @from_users = from_users
31
+ @forwards = forwards
32
+ # Kept apart by kind: a Spinel build cannot call through a variable that may hold either.
33
+ @pattern_regexp = pattern.is_a?(String) ? Regexp.new(pattern) : (pattern.is_a?(Regexp) ? pattern : nil)
34
+ @pattern_proc = pattern.is_a?(Proc) ? pattern : nil
35
+ @from_ids = nil
36
+ @no_check = chats.nil? && incoming.nil? && outgoing.nil? && pattern.nil? && from_users.nil? &&
37
+ forwards.nil? && func.nil?
38
+ end
39
+
40
+ def pattern
41
+ @pattern_regexp || @pattern_proc
42
+ end
43
+
44
+ def kind
45
+ "NewMessage"
46
+ end
47
+
48
+ def build(update, self_id)
49
+ case update.name
50
+ when "updateNewMessage", "updateNewChannelMessage"
51
+ message = update["message"]
52
+ return nil unless message.is?("message")
53
+
54
+ NewMessage::Event.new(message)
55
+ when "updateShortMessage"
56
+ NewMessage::Event.new(NewMessage.short_message(update, self_id, "peerUser", update["user_id"],
57
+ update["out"] ? self_id : update["user_id"]))
58
+ when "updateShortChatMessage"
59
+ NewMessage::Event.new(NewMessage.short_message(update, self_id, "peerChat", update["chat_id"],
60
+ update["out"] ? self_id : update["from_id"]))
61
+ end
62
+ end
63
+
64
+ # The message an updateShortMessage or updateShortChatMessage stands for.
65
+ def self.short_message(update, _self_id, peer, peer_id, from_id)
66
+ field = peer == "peerUser" ? "user_id" : "chat_id"
67
+ TL.make("message", "out" => update["out"], "mentioned" => update["mentioned"],
68
+ "media_unread" => update["media_unread"], "silent" => update["silent"], "id" => update["id"],
69
+ "peer_id" => TL.make(peer, field => peer_id),
70
+ "from_id" => TL.make("peerUser", "user_id" => from_id), "message" => update["message"],
71
+ "date" => update["date"], "fwd_from" => update["fwd_from"], "via_bot_id" => update["via_bot_id"],
72
+ "reply_to" => update["reply_to"], "entities" => update["entities"],
73
+ "ttl_period" => update["ttl_period"])
74
+ end
75
+
76
+ def filter(event)
77
+ return true if @no_check
78
+
79
+ message = event.message
80
+ return false if @incoming && message.out?
81
+ return false if @outgoing && !message.out?
82
+ return false if !@forwards.nil? && (@forwards ? true : false) != !message.fwd_from.nil?
83
+ return false if @from_ids && !@from_ids.key?(message.sender_id)
84
+
85
+ if @pattern_regexp || @pattern_proc
86
+ match = pattern_match(message.raw_text || "")
87
+ return false unless match
88
+
89
+ event.pattern_match = match
90
+ end
91
+ super
92
+ end
93
+
94
+ private
95
+
96
+ def resolve_ids(client)
97
+ super
98
+ @from_ids = Events.id_set(client, @from_users)
99
+ end
100
+
101
+ # Python's re.match: only a match that starts the text.
102
+ def pattern_match(text)
103
+ return @pattern_proc.call(text) if @pattern_proc
104
+
105
+ match = @pattern_regexp.match(text)
106
+ match && match.begin(0).zero? ? match : nil
107
+ end
108
+
109
+ public
110
+
111
+ # The event: the message, and what the pattern matched. The message's own methods are here too.
112
+ class Event < Common
113
+ attr_accessor :pattern_match
114
+
115
+ # A def, not attr_reader: Spinel miscompiles an attr_reader named message read through a value
116
+ # of unknown class (DEVELOPING.md).
117
+ def message
118
+ @message
119
+ end
120
+
121
+ def initialize(raw)
122
+ super(raw["peer_id"], raw["id"], raw["post"] ? true : false)
123
+ @message = Message.new(raw)
124
+ @pattern_match = nil
125
+ end
126
+
127
+ def kind
128
+ "NewMessage"
129
+ end
130
+
131
+ def attach(client, update, entities)
132
+ super
133
+ @message.attach(client, entities)
134
+ self
135
+ end
136
+
137
+ def id = @message.id
138
+ def text = @message.text
139
+ def raw_text = @message.raw_text
140
+ def date = @message.date
141
+ def out? = @message.out?
142
+ def sender_id = @message.sender_id
143
+ def sender = @message.sender
144
+ def get_sender = @message.get_sender
145
+ def media = @message.media
146
+ def fwd_from = @message.fwd_from
147
+ def reply_to_msg_id = @message.reply_to_msg_id
148
+ def reply? = @message.reply?
149
+ def get_reply_message = @message.get_reply_message
150
+ def reply(text) = @message.reply(text)
151
+ def edit(text) = @message.edit(text)
152
+ def delete(revoke: true) = @message.delete(revoke: revoke)
153
+ def forward_to(target, drop_author: false) = @message.forward_to(target, drop_author: drop_author)
154
+ end
155
+ end
156
+
157
+ # Telethon's events.MessageEdited: a message's new text or media.
158
+ class MessageEdited < NewMessage
159
+ def kind
160
+ "MessageEdited"
161
+ end
162
+
163
+ def build(update, _self_id)
164
+ return nil unless %w[updateEditMessage updateEditChannelMessage].include?(update.name)
165
+
166
+ MessageEdited::Event.new(update["message"])
167
+ end
168
+
169
+ class Event < NewMessage::Event
170
+ def kind
171
+ "MessageEdited"
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ module Events
5
+ # Telethon's events.Raw: every update as it came (a TL::Object such as updateUserStatus), or only
6
+ # those whose constructor names are in types.
7
+ class Raw < Builder
8
+ attr_reader :types
9
+
10
+ def initialize(types: nil, func: nil)
11
+ super(func: func)
12
+ @types = types.nil? ? nil : (types.is_a?(Array) ? types : [types])
13
+ end
14
+
15
+ def kind
16
+ "Raw"
17
+ end
18
+
19
+ def resolve(_client)
20
+ @resolved = true
21
+ end
22
+
23
+ def build(update, _self_id)
24
+ update
25
+ end
26
+
27
+ def filter(update)
28
+ return false unless @types.nil? || @types.include?(update.name)
29
+ return true unless @func
30
+
31
+ @func.call(update) ? true : false
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ module Events
5
+ # Telethon's events.UserUpdate: someone came online or went offline, or is typing, recording or
6
+ # uploading something.
7
+ class UserUpdate < Builder
8
+ def kind
9
+ "UserUpdate"
10
+ end
11
+
12
+ def build(update, _self_id)
13
+ case update.name
14
+ when "updateUserStatus"
15
+ UserUpdate::Event.new(TL.make("peerUser", "user_id" => update["user_id"]), status: update["status"])
16
+ when "updateChannelUserTyping"
17
+ UserUpdate::Event.new(update["from_id"], chat_peer: TL.make("peerChannel", "channel_id" => update["channel_id"]),
18
+ typing: update["action"])
19
+ when "updateChatUserTyping"
20
+ UserUpdate::Event.new(update["from_id"], chat_peer: TL.make("peerChat", "chat_id" => update["chat_id"]),
21
+ typing: update["action"])
22
+ when "updateUserTyping"
23
+ UserUpdate::Event.new(update["user_id"], typing: update["action"])
24
+ end
25
+ end
26
+
27
+ class Event < Common
28
+ UPLOADING = %w[sendMessageChooseContactAction sendMessageChooseStickerAction sendMessageUploadAudioAction
29
+ sendMessageUploadDocumentAction sendMessageUploadPhotoAction sendMessageUploadRoundAction
30
+ sendMessageUploadVideoAction].freeze
31
+ RECORDING = %w[sendMessageRecordAudioAction sendMessageRecordRoundAction sendMessageRecordVideoAction].freeze
32
+ DAY = 24 * 60 * 60
33
+
34
+ attr_reader :status, :action, :sender_id
35
+
36
+ def initialize(peer, status: nil, chat_peer: nil, typing: nil)
37
+ super(chat_peer || peer)
38
+ @sender_id = Events.peer_id_of(peer)
39
+ @status = status
40
+ @action = typing
41
+ end
42
+
43
+ def kind
44
+ "UserUpdate"
45
+ end
46
+
47
+ def user_id
48
+ @sender_id
49
+ end
50
+
51
+ def user
52
+ @entities[@sender_id]
53
+ end
54
+
55
+ def sender
56
+ user
57
+ end
58
+
59
+ def get_user
60
+ user || @client.get_entity(@sender_id)
61
+ end
62
+
63
+ def input_user
64
+ @client.input_peer(@sender_id)
65
+ end
66
+
67
+ # What the user is doing: nil when the update was about the status, not an action.
68
+ def typing? = action_is(%w[sendMessageTypingAction])
69
+ def uploading? = action_is(UPLOADING)
70
+ def recording? = action_is(RECORDING)
71
+ def playing? = action_is(%w[sendMessageGamePlayAction])
72
+ def cancel? = action_is(%w[sendMessageCancelAction])
73
+ def geo? = action_is(%w[sendMessageGeoLocationAction])
74
+ def audio? = action_is(%w[sendMessageRecordAudioAction sendMessageUploadAudioAction])
75
+ def round? = action_is(%w[sendMessageRecordRoundAction sendMessageUploadRoundAction])
76
+ def video? = action_is(%w[sendMessageRecordVideoAction sendMessageUploadVideoAction])
77
+ def contact? = action_is(%w[sendMessageChooseContactAction])
78
+ def document? = action_is(%w[sendMessageUploadDocumentAction])
79
+ def sticker? = action_is(%w[sendMessageChooseStickerAction])
80
+ def photo? = action_is(%w[sendMessageUploadPhotoAction])
81
+
82
+ # When an offline user was last seen (Unix time), or nil.
83
+ def last_seen
84
+ return nil if @status.nil?
85
+
86
+ @status.is?("userStatusOffline") ? @status["was_online"] : nil
87
+ end
88
+
89
+ # Until when an online user counts as online (Unix time), or nil.
90
+ def until
91
+ return nil if @status.nil?
92
+
93
+ @status.is?("userStatusOnline") ? @status["expires"] : nil
94
+ end
95
+
96
+ # How recently the user was seen: nil when the update was about an action, not the status.
97
+ def online? = seen_within(0)
98
+ def recently? = seen_within(DAY)
99
+ def within_weeks? = seen_within(7 * DAY)
100
+ def within_months? = seen_within(30 * DAY)
101
+
102
+ private
103
+
104
+ def action_is(names)
105
+ return nil if @action.nil?
106
+
107
+ names.include?(@action.name)
108
+ end
109
+
110
+ def seen_within(seconds)
111
+ return nil if @status.nil?
112
+
113
+ last_seen_delta <= seconds
114
+ end
115
+
116
+ def last_seen_delta
117
+ case @status.name
118
+ when "userStatusOffline" then Time.now.to_i - @status["was_online"]
119
+ when "userStatusOnline" then 0
120
+ when "userStatusRecently" then DAY
121
+ when "userStatusLastWeek" then 7 * DAY
122
+ when "userStatusLastMonth" then 30 * DAY
123
+ else 365 * DAY
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end