telethon 0.4.0 → 1.0.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.
@@ -0,0 +1,278 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ module CLI
5
+ module WritingCommands
6
+ private
7
+
8
+ def delete(args)
9
+ rest, opts = parse_for("delete", args)
10
+ options = { yes: opts["--yes"] }
11
+ ids = rest[1..].map { |v| Args.integer(v, "message id") }
12
+ with_client do |client|
13
+ chat = client.get_entity(rest[0])
14
+ found = client.get_messages(chat, ids).compact
15
+ fail!("None of those messages exist.") if found.empty?
16
+ unless opts["--json"]
17
+ found.each do |message|
18
+ text = message.raw_text.to_s
19
+ preview = (text.empty? ? "<media>" : text).tr("\n", " ")[0, 80]
20
+ @out.puts "#{message.id}\t#{name_of(message.sender)}\t#{preview}"
21
+ end
22
+ end
23
+ unless options[:yes]
24
+ fail!("Refusing to delete without a terminal to confirm in; pass --yes to skip the prompt.") unless tty?
25
+ answer = ask("Delete these #{found.size} messages for everyone? Type yes: ")
26
+ fail!("Nothing deleted.") unless answer == "yes"
27
+ end
28
+ deleted_ids = found.map(&:id)
29
+ client.delete_messages(chat, deleted_ids, revoke: true)
30
+ if opts["--json"]
31
+ @out.puts JSON.generate("ok" => true, "deleted" => deleted_ids, "count" => deleted_ids.size)
32
+ else
33
+ @out.puts "Deleted #{found.size} messages."
34
+ end
35
+ end
36
+ end
37
+
38
+ def send_command(args)
39
+ rest, opts = parse_for("send", args)
40
+ topic = opts["--topic"] && Args.integer(opts["--topic"], "--topic")
41
+ schedule = opts["--at"] && Args.parse_time(opts["--at"], "--at")
42
+ files = opts["--file"] || []
43
+ inputs = files.map do |path|
44
+ if path == "-"
45
+ @input
46
+ else
47
+ fail!("no such file: #{path}") unless File.file?(path)
48
+ path
49
+ end
50
+ end
51
+ voice_note = opts["--voice"] ? true : false
52
+ video_note = opts["--video-note"] ? true : false
53
+ # Files go as documents unless --media, as tg send always did: scripts rely on the file arriving intact.
54
+ force_document = !(opts["--media"] || voice_note || video_note)
55
+ with_client do |client|
56
+ chat = client.get_entity(rest[0])
57
+ if inputs.empty?
58
+ sent = client.send_message(chat, rest[1], reply_to: topic, schedule: schedule)
59
+ if opts["--json"]
60
+ @out.puts JSON.generate("ok" => true, "ids" => [sent&.id],
61
+ "messages" => sent ? [message_summary(sent)] : [])
62
+ else
63
+ @out.puts "Sent message #{sent&.id}."
64
+ end
65
+ else
66
+ show_progress = @err.respond_to?(:tty?) && @err.tty?
67
+ err = @err
68
+ cb = show_progress ? ->(done, total) { err.print("\rUploading #{done}/#{total} bytes"); err.flush } : nil
69
+ payload = (opts["--album"] || inputs.size > 1) ? inputs : inputs[0]
70
+ sent = client.send_file(chat, payload, caption: rest[1], reply_to: topic, schedule: schedule,
71
+ voice_note: voice_note, video_note: video_note,
72
+ force_document: force_document, progress_callback: cb)
73
+ @err.puts if show_progress
74
+ sent = [sent] unless sent.is_a?(Array)
75
+ if opts["--json"]
76
+ @out.puts JSON.generate("ok" => true, "ids" => sent.map { |m| m&.id },
77
+ "messages" => sent.compact.map { |m| message_summary(m) })
78
+ else
79
+ @out.puts "Sent message #{sent.map { |m| m&.id }.join(', ')}."
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ def reply_command(args)
86
+ rest, opts = parse_for("reply", args)
87
+ id = Args.positive_int(rest[1], "message id")
88
+ with_client do |client|
89
+ chat = client.get_entity(rest[0])
90
+ sent = client.send_message(chat, rest[2..].join(" "), reply_to: id)
91
+ if opts["--json"]
92
+ @out.puts JSON.generate("ok" => true, "id" => sent&.id, "message" => sent && message_summary(sent))
93
+ else
94
+ @out.puts "Sent message #{sent&.id}."
95
+ end
96
+ end
97
+ end
98
+
99
+ def pin_command(args)
100
+ rest, opts = parse_for("pin", args)
101
+ id = Args.positive_int(rest[1], "message id")
102
+ with_client do |client|
103
+ chat = client.get_entity(rest[0])
104
+ client.pin_message(chat, id, notify: opts["--notify"] ? true : false,
105
+ pm_oneside: opts["--pm-oneside"] ? true : false)
106
+ if opts["--json"]
107
+ @out.puts JSON.generate("ok" => true, "id" => id)
108
+ else
109
+ @out.puts "Pinned message #{id}."
110
+ end
111
+ end
112
+ end
113
+
114
+ def unpin_command(args)
115
+ rest, opts = parse_for("unpin", args)
116
+ id = rest[1] ? Args.positive_int(rest[1], "message id") : nil
117
+ with_client do |client|
118
+ chat = client.get_entity(rest[0])
119
+ client.unpin_message(chat, id, notify: opts["--notify"] ? true : false)
120
+ if opts["--json"]
121
+ @out.puts JSON.generate("ok" => true, "id" => id)
122
+ else
123
+ @out.puts id ? "Unpinned message #{id}." : "Unpinned all messages."
124
+ end
125
+ end
126
+ end
127
+
128
+ def mark_read_command(args)
129
+ rest, opts = parse_for("mark-read", args)
130
+ max_id = rest[1] ? Args.integer(rest[1], "max id") : 0
131
+ with_client do |client|
132
+ chat = client.get_entity(rest[0])
133
+ client.send_read_acknowledge(chat, max_id: max_id,
134
+ clear_mentions: opts["--clear-mentions"] ? true : false,
135
+ clear_reactions: opts["--clear-reactions"] ? true : false)
136
+ if opts["--json"]
137
+ @out.puts JSON.generate("ok" => true, "max_id" => max_id)
138
+ else
139
+ @out.puts "Marked read."
140
+ end
141
+ end
142
+ end
143
+
144
+ def react_command(args)
145
+ rest, opts = parse_for("react", args)
146
+ id = Args.positive_int(rest[1], "message id")
147
+ emoji = rest[2]
148
+ reaction = emoji && !emoji.empty? ? [TL.make("reactionEmoji", "emoticon" => emoji)] : []
149
+ with_client do |client|
150
+ peer = client.get_input_entity(rest[0])
151
+ client.call(TL.make("messages.sendReaction", "peer" => peer, "msg_id" => id,
152
+ "big" => opts["--big"] ? true : nil,
153
+ "reaction" => reaction))
154
+ if opts["--json"]
155
+ @out.puts JSON.generate("ok" => true, "id" => id, "reaction" => emoji)
156
+ else
157
+ @out.puts emoji ? "Reacted with #{emoji}." : "Cleared reaction."
158
+ end
159
+ end
160
+ end
161
+
162
+ def draft_command(args)
163
+ rest, opts = parse_for("draft", args)
164
+ raise UsageError, "--clear and --send cannot go together" if opts["--clear"] && opts["--send"]
165
+
166
+ reply_to = opts["--reply-to"] ? Args.positive_int(opts["--reply-to"], "--reply-to") : :keep
167
+ with_client do |client|
168
+ if rest.empty?
169
+ drafts = client.get_drafts
170
+ if opts["--json"]
171
+ @out.puts JSON.pretty_generate(drafts.map { |d| draft_summary(d) })
172
+ else
173
+ drafts.each { |d| @out.puts "#{d.entity && Client.peer_id(d.entity)}\t#{d.raw_text.tr("\n", ' ')}" }
174
+ end
175
+ return
176
+ end
177
+
178
+ chat = client.get_entity(rest[0])
179
+ d = client.get_drafts(chat)
180
+ if opts["--clear"]
181
+ d.delete
182
+ if opts["--json"]
183
+ @out.puts JSON.generate("ok" => true, "cleared" => true)
184
+ else
185
+ @out.puts "Draft cleared."
186
+ end
187
+ elsif opts["--send"]
188
+ fail!("No draft to send.") if d.empty?
189
+ sent = d.send_draft
190
+ if opts["--json"]
191
+ @out.puts JSON.generate("ok" => true, "id" => sent&.id, "message" => sent && message_summary(sent))
192
+ else
193
+ @out.puts "Sent draft as message #{sent&.id}."
194
+ end
195
+ elsif rest.size >= 2
196
+ text = rest[1..].join(" ")
197
+ d.set_message(text, reply_to: reply_to)
198
+ if opts["--json"]
199
+ @out.puts JSON.generate(draft_summary(d).merge("ok" => true))
200
+ else
201
+ @out.puts "Draft saved."
202
+ end
203
+ elsif opts["--json"]
204
+ @out.puts JSON.pretty_generate(draft_summary(d))
205
+ else
206
+ @out.puts d.raw_text
207
+ end
208
+ end
209
+ end
210
+
211
+ def draft_summary(d)
212
+ { "chat_id" => d.entity && Client.peer_id(d.entity), "text" => d.raw_text,
213
+ "reply_to_msg_id" => d.reply_to_msg_id, "date" => d.date && CLI.iso_date(d.date) }
214
+ end
215
+
216
+ def typing_command(args)
217
+ rest, opts = parse_for("typing", args)
218
+ kind = opts["--action"] || "typing"
219
+ seconds = opts["--seconds"] ? Args.integer(opts["--seconds"], "--seconds") : 0
220
+ raise UsageError, "--seconds must be >= 0" if seconds.negative?
221
+
222
+ action_obj = begin
223
+ ChatAction.named(kind)
224
+ rescue ArgumentError => e
225
+ raise UsageError, e.message
226
+ end
227
+ with_client do |client|
228
+ chat = client.get_entity(rest[0])
229
+ if action_obj.is?("sendMessageCancelAction") || seconds.zero?
230
+ client.call(TL.make("messages.setTyping", "peer" => client.get_input_entity(chat), "action" => action_obj))
231
+ else
232
+ client.action(chat, action_obj) { sleep(seconds) }
233
+ end
234
+ if opts["--json"]
235
+ @out.puts JSON.generate("ok" => true, "action" => kind)
236
+ else
237
+ @out.puts "Sent action #{kind}."
238
+ end
239
+ end
240
+ end
241
+
242
+ def edit(args)
243
+ rest, opts = parse_for("edit", args)
244
+ id = Args.positive_int(rest[1], "message id")
245
+ with_client do |client|
246
+ edited = client.edit_message(client.get_entity(rest[0]), id, rest[2])
247
+ if opts["--json"]
248
+ @out.puts JSON.generate("ok" => true, "id" => edited ? edited.id : id,
249
+ "message" => edited && message_summary(edited))
250
+ else
251
+ @out.puts "Edited message #{edited ? edited.id : id}."
252
+ end
253
+ end
254
+ end
255
+
256
+ def forward(args)
257
+ rest, opts = parse_for("forward", args)
258
+ options = { from: opts["--from"] || "me", delete_source: opts["--delete-source"] }
259
+ ids = message_ids(rest[1..])
260
+ with_client do |client|
261
+ source = client.get_entity(options[:from])
262
+ target = client.get_entity(rest[0])
263
+ sent = client.forward_messages(target, ids, source, drop_author: true)
264
+ if options[:delete_source]
265
+ client.delete_messages(source, ids)
266
+ end
267
+ if opts["--json"]
268
+ @out.puts JSON.generate("ok" => true, "ids" => sent.map { |m| m&.id },
269
+ "deleted_source" => options[:delete_source] ? ids : [])
270
+ else
271
+ @out.puts "Forwarded as message #{sent.map { |m| m&.id }.join(', ')}."
272
+ @out.puts "Deleted #{ids.size} source messages." if options[:delete_source]
273
+ end
274
+ end
275
+ end
276
+ end
277
+ end
278
+ end