gruubY 0.2.2 → 0.2.3

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 (56) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -21
  3. data/README.md +11 -6
  4. data/Rakefile +51 -51
  5. data/example/bot.rb +120 -1
  6. data/example/generate_user_session.rb +81 -81
  7. data/example/tdlib.rb +77 -77
  8. data/example/userbot.rb +133 -133
  9. data/lib/gruubY/api.rb +540 -363
  10. data/lib/gruubY/async.rb +13 -13
  11. data/lib/gruubY/bot.rb +104 -90
  12. data/lib/gruubY/bound.rb +14 -14
  13. data/lib/gruubY/client.rb +310 -296
  14. data/lib/gruubY/context.rb +296 -242
  15. data/lib/gruubY/dispatcher.rb +60 -60
  16. data/lib/gruubY/enums.rb +57 -62
  17. data/lib/gruubY/exception.rb +5 -5
  18. data/lib/gruubY/file_stream.rb +17 -17
  19. data/lib/gruubY/filters.rb +292 -292
  20. data/lib/gruubY/group_manager.rb +213 -213
  21. data/lib/gruubY/handlers.rb +225 -170
  22. data/lib/gruubY/keyboard.rb +49 -49
  23. data/lib/gruubY/media.rb +38 -38
  24. data/lib/gruubY/middleware.rb +18 -18
  25. data/lib/gruubY/plugin.rb +26 -26
  26. data/lib/gruubY/raw.rb +25 -25
  27. data/lib/gruubY/retry.rb +14 -14
  28. data/lib/gruubY/server.rb +18 -18
  29. data/lib/gruubY/session.rb +30 -30
  30. data/lib/gruubY/tdlib/client.rb +565 -565
  31. data/lib/gruubY/tdlib/client_manager.rb +37 -37
  32. data/lib/gruubY/tdlib/decorators.rb +17 -17
  33. data/lib/gruubY/tdlib/errors.rb +6 -6
  34. data/lib/gruubY/tdlib/group_manager.rb +58 -58
  35. data/lib/gruubY/tdlib/native.rb +72 -72
  36. data/lib/gruubY/tdlib/schema_builder.rb +237 -237
  37. data/lib/gruubY/tdlib/tdjson.rb +49 -49
  38. data/lib/gruubY/tdlib/user_session.rb +69 -69
  39. data/lib/gruubY/types/base_object.rb +72 -72
  40. data/lib/gruubY/types/bound_entities.rb +289 -245
  41. data/lib/gruubY/types/chat.rb +93 -93
  42. data/lib/gruubY/types/chat_info.rb +32 -31
  43. data/lib/gruubY/types/extra.rb +289 -251
  44. data/lib/gruubY/types/message.rb +301 -295
  45. data/lib/gruubY/types/message_entity.rb +14 -14
  46. data/lib/gruubY/types/registry.rb +86 -67
  47. data/lib/gruubY/types/user.rb +47 -31
  48. data/lib/gruubY/webapp.rb +22 -22
  49. data/lib/gruubY.rb +39 -41
  50. data/plugins/logger.rb +7 -7
  51. metadata +3 -9
  52. data/example/ntgcalls.rb +0 -80
  53. data/lib/gruubY/ntgcalls/native.rb +0 -100
  54. data/lib/gruubY/ntgcalls.rb +0 -478
  55. data/lib/gruubY/raw_types.rb +0 -57
  56. data/plugins/media_tools.rb +0 -13
@@ -1,295 +1,301 @@
1
- require_relative "base_object"
2
- require_relative "user"
3
- require_relative "chat"
4
- require_relative "message_entity"
5
- require_relative "extra"
6
-
7
- module GrubY
8
- class Message < BaseObject
9
- fields :message_id, :message_thread_id, :direct_messages_topic, :from,
10
- :sender_chat, :sender_boost_count, :sender_business_bot, :sender_tag,
11
- :date, :business_connection_id, :chat, :forward_origin, :is_topic_message,
12
- :is_automatic_forward, :reply_to_message, :external_reply, :quote,
13
- :reply_to_story, :reply_to_checklist_task_id, :reply_to_poll_option_id,
14
- :via_bot, :edit_date, :has_protected_content, :is_from_offline,
15
- :is_paid_post, :media_group_id, :author_signature, :paid_star_count,
16
- :text, :entities, :link_preview_options, :suggested_post_info, :effect_id,
17
- :animation, :audio, :document, :paid_media, :photo, :sticker, :story,
18
- :video, :video_note, :voice, :caption, :caption_entities,
19
- :show_caption_above_media, :has_media_spoiler, :checklist, :contact, :dice,
20
- :game, :poll, :venue, :location, :new_chat_members, :left_chat_member,
21
- :chat_owner_left, :chat_owner_changed, :new_chat_title, :new_chat_photo,
22
- :delete_chat_photo, :group_chat_created, :supergroup_chat_created,
23
- :channel_chat_created, :message_auto_delete_timer_changed,
24
- :migrate_to_chat_id, :migrate_from_chat_id, :pinned_message, :invoice,
25
- :successful_payment, :refunded_payment, :users_shared, :chat_shared, :gift,
26
- :unique_gift, :gift_upgrade_sent, :connected_website,
27
- :write_access_allowed, :passport_data, :proximity_alert_triggered,
28
- :boost_added, :chat_background_set, :checklist_tasks_done,
29
- :checklist_tasks_added, :direct_message_price_changed,
30
- :forum_topic_created, :forum_topic_edited, :forum_topic_closed,
31
- :forum_topic_reopened, :general_forum_topic_hidden,
32
- :general_forum_topic_unhidden, :giveaway_created, :giveaway,
33
- :giveaway_winners, :giveaway_completed, :managed_bot_created,
34
- :paid_message_price_changed, :poll_option_added, :poll_option_deleted,
35
- :suggested_post_approved, :suggested_post_approval_failed,
36
- :suggested_post_declined, :suggested_post_paid,
37
- :suggested_post_refunded, :video_chat_scheduled, :video_chat_started,
38
- :video_chat_ended, :video_chat_participants_invited, :web_app_data,
39
- :reply_markup
40
-
41
- def initialize(data, api: nil, client: nil)
42
- super(data, api: api, client: client)
43
- @from = User.new(@from, api: api, client: client) if @from.is_a?(Hash)
44
- @chat = Chat.new(@chat, api: api, client: client) if @chat.is_a?(Hash)
45
- @sender_chat = Chat.new(@sender_chat, api: api, client: client) if @sender_chat.is_a?(Hash)
46
- @sender_business_bot = User.new(@sender_business_bot, api: api, client: client) if @sender_business_bot.is_a?(Hash)
47
- @via_bot = User.new(@via_bot, api: api, client: client) if @via_bot.is_a?(Hash)
48
- @reply_to_message = Message.new(@reply_to_message, api: api, client: client) if @reply_to_message.is_a?(Hash)
49
- @external_reply = ExternalReplyInfo.new(@external_reply) if @external_reply.is_a?(Hash)
50
- @quote = TextQuote.new(@quote) if @quote.is_a?(Hash)
51
- @reply_to_story = Story.new(@reply_to_story) if @reply_to_story.is_a?(Hash)
52
- @entities = Array(@entities).map { |e| MessageEntity.new(e) }
53
- @caption_entities = Array(@caption_entities).map { |e| MessageEntity.new(e) }
54
- @photo = Array(@photo).map { |p| PhotoSize.new(p) }
55
- @animation = Animation.new(@animation) if @animation.is_a?(Hash)
56
- @audio = Audio.new(@audio) if @audio.is_a?(Hash)
57
- @document = Document.new(@document) if @document.is_a?(Hash)
58
- @story = Story.new(@story) if @story.is_a?(Hash)
59
- @video = Video.new(@video) if @video.is_a?(Hash)
60
- @video_note = VideoNote.new(@video_note) if @video_note.is_a?(Hash)
61
- @voice = Voice.new(@voice) if @voice.is_a?(Hash)
62
- @paid_media = PaidMediaInfo.new(@paid_media) if @paid_media.is_a?(Hash)
63
- @contact = Contact.new(@contact) if @contact.is_a?(Hash)
64
- @dice = Dice.new(@dice) if @dice.is_a?(Hash)
65
- @poll = Poll.new(@poll) if @poll.is_a?(Hash)
66
- @new_chat_members = Array(@new_chat_members).map { |u| User.new(u, api: api, client: client) }
67
- @left_chat_member = User.new(@left_chat_member, api: api, client: client) if @left_chat_member.is_a?(Hash)
68
- end
69
-
70
- def reply(text = nil, **opts)
71
- call_api("sendMessage", { chat_id: chat_id!, text: text.to_s, reply_to_message_id: message_id }.merge(opts))
72
- end
73
-
74
- def reply_text(text = nil, **opts)
75
- reply(text, **opts)
76
- end
77
-
78
- def answer(text = nil, **opts)
79
- reply(text, **opts)
80
- end
81
-
82
- def edit_text(text, **opts)
83
- call_api("editMessageText", { chat_id: chat_id!, message_id: message_id, text: text.to_s }.merge(opts))
84
- end
85
-
86
- alias edit edit_text
87
-
88
- def edit_caption(caption, **opts)
89
- call_api("editMessageCaption", { chat_id: chat_id!, message_id: message_id, caption: caption.to_s }.merge(opts))
90
- end
91
-
92
- def edit_media(media, **opts)
93
- call_api("editMessageMedia", { chat_id: chat_id!, message_id: message_id, media: media }.merge(opts))
94
- end
95
-
96
- def edit_checklist(checklist, **opts)
97
- call_api("editMessageChecklist", { chat_id: chat_id!, message_id: message_id, checklist: checklist }.merge(opts))
98
- end
99
-
100
- def edit_reply_markup(reply_markup = nil, **opts)
101
- call_api("editMessageReplyMarkup", { chat_id: chat_id!, message_id: message_id, reply_markup: reply_markup }.merge(opts))
102
- end
103
-
104
- def edit_live_location(latitude:, longitude:, **opts)
105
- call_api("editMessageLiveLocation", { chat_id: chat_id!, message_id: message_id, latitude: latitude, longitude: longitude }.merge(opts))
106
- end
107
-
108
- def stop_live_location(**opts)
109
- call_api("stopMessageLiveLocation", { chat_id: chat_id!, message_id: message_id }.merge(opts))
110
- end
111
-
112
- def forward(to_chat_id:, from_chat_id: chat_id!, **opts)
113
- call_api("forwardMessage", { chat_id: to_chat_id, from_chat_id: from_chat_id, message_id: message_id }.merge(opts))
114
- end
115
-
116
- def copy(to_chat_id:, from_chat_id: chat_id!, **opts)
117
- call_api("copyMessage", { chat_id: to_chat_id, from_chat_id: from_chat_id, message_id: message_id }.merge(opts))
118
- end
119
-
120
- def copy_media_group(to_chat_id:, from_chat_id: chat_id!, **opts)
121
- call_api("copyMessages", { chat_id: to_chat_id, from_chat_id: from_chat_id, message_ids: [message_id] }.merge(opts))
122
- end
123
-
124
- def delete(**opts)
125
- call_api("deleteMessage", { chat_id: chat_id!, message_id: message_id }.merge(opts))
126
- end
127
-
128
- def react(reaction:, is_big: false)
129
- call_api("setMessageReaction", { chat_id: chat_id!, message_id: message_id, reaction: reaction, is_big: is_big })
130
- end
131
-
132
- def retract_vote(**opts)
133
- call_api("stopPoll", { chat_id: chat_id!, message_id: message_id }.merge(opts))
134
- end
135
-
136
- def vote(option_ids, **opts)
137
- call_api("sendPoll", { chat_id: chat_id!, options: option_ids }.merge(opts))
138
- end
139
-
140
- def pin(**opts)
141
- call_api("pinChatMessage", { chat_id: chat_id!, message_id: message_id }.merge(opts))
142
- end
143
-
144
- def unpin(**opts)
145
- call_api("unpinChatMessage", { chat_id: chat_id!, message_id: message_id }.merge(opts))
146
- end
147
-
148
- def read
149
- call_raw_api("readChatHistory", { chat_id: chat_id! })
150
- end
151
-
152
- def view
153
- call_raw_api("viewMessages", { chat_id: chat_id!, message_ids: [message_id] })
154
- end
155
-
156
- def pay(**opts)
157
- call_raw_api("sendPaymentForm", { chat_id: chat_id!, message_id: message_id }.merge(opts))
158
- end
159
-
160
- def accept_gift_purchase_offer(**opts)
161
- call_raw_api("processGiftPurchaseOffer", { message_id: message_id, accept: true }.merge(opts))
162
- end
163
-
164
- def reject_gift_purchase_offer(**opts)
165
- call_raw_api("processGiftPurchaseOffer", { message_id: message_id, accept: false }.merge(opts))
166
- end
167
-
168
- def summarize(summary_language_code: "en")
169
- call_raw_api("summarizeMessage", { chat_id: chat_id!, message_id: message_id, summary_language_code: summary_language_code })
170
- end
171
-
172
- def fix_text_with_ai(**opts)
173
- call_raw_api("fixTextWithAi", { chat_id: chat_id!, message_id: message_id }.merge(opts))
174
- end
175
-
176
- def compose_text_with_ai(**opts)
177
- call_raw_api("composeTextWithAi", { chat_id: chat_id!, message_id: message_id }.merge(opts))
178
- end
179
-
180
- def reply_inline_bot_result(result_id:, query_id:, **opts)
181
- call_raw_api("sendInlineBotResult", { chat_id: chat_id!, query_id: query_id, result_id: result_id }.merge(opts))
182
- end
183
-
184
- def answer_inline_bot_result(result_id:, query_id:, **opts)
185
- reply_inline_bot_result(result_id: result_id, query_id: query_id, **opts)
186
- end
187
-
188
- def get_media_group
189
- return nil if media_group_id.to_s.empty?
190
-
191
- call_raw_api("getMediaGroup", { chat_id: chat_id!, message_id: message_id })
192
- end
193
-
194
- def reply_chat_action(action)
195
- call_api("sendChatAction", { chat_id: chat_id!, action: action })
196
- end
197
-
198
- def click
199
- raise NotImplementedError, "click() requires callback query context"
200
- end
201
-
202
- def download(file_name = "file.dat")
203
- file_id = resolve_file_id
204
- raise ArgumentError, "message has no downloadable media" if file_id.nil?
205
-
206
- f = call_api("getFile", { file_id: file_id })
207
- call_raw_api("getFile", { file_id: file_id }) unless f
208
- file_path = f && f["file_path"]
209
- raise ArgumentError, "file_path not found for file_id=#{file_id}" if file_path.to_s.empty?
210
-
211
- @api.download_file(file_path, file_name)
212
- file_name
213
- end
214
-
215
- SENDERS = {
216
- animation: "sendAnimation",
217
- audio: "sendAudio",
218
- contact: "sendContact",
219
- document: "sendDocument",
220
- game: "sendGame",
221
- invoice: "sendInvoice",
222
- location: "sendLocation",
223
- media_group: "sendMediaGroup",
224
- photo: "sendPhoto",
225
- poll: "sendPoll",
226
- dice: "sendDice",
227
- sticker: "sendSticker",
228
- venue: "sendVenue",
229
- video: "sendVideo",
230
- video_note: "sendVideoNote",
231
- voice: "sendVoice",
232
- paid_media: "sendPaidMedia",
233
- cached_media: "sendDocument",
234
- checklist: "sendChecklist"
235
- }.freeze
236
-
237
- SENDERS.each do |name, method_name|
238
- define_method("reply_#{name}") do |payload = nil, **opts|
239
- params = { chat_id: chat_id!, reply_to_message_id: message_id }.merge(opts)
240
- params = attach_payload(name, params, payload)
241
- call_api(method_name, params)
242
- end
243
-
244
- define_method("answer_#{name}") do |payload = nil, **opts|
245
- public_send("reply_#{name}", payload, **opts)
246
- end
247
- end
248
-
249
- private
250
-
251
- def chat_id!
252
- @chat&.id || self["chat"]&.dig("id") || raise(ArgumentError, "message has no chat id")
253
- end
254
-
255
- def attach_payload(kind, params, payload)
256
- case kind
257
- when :animation, :audio, :document, :photo, :sticker, :video, :video_note, :voice
258
- params.merge(kind => payload)
259
- when :contact
260
- params.merge(payload.is_a?(Hash) ? payload : {})
261
- when :game
262
- params.merge(game_short_name: payload)
263
- when :invoice
264
- params.merge(payload.is_a?(Hash) ? payload : {})
265
- when :location
266
- params.merge(payload.is_a?(Hash) ? payload : {})
267
- when :media_group
268
- params.merge(media: payload)
269
- when :poll
270
- params.merge(payload.is_a?(Hash) ? payload : {})
271
- when :venue
272
- params.merge(payload.is_a?(Hash) ? payload : {})
273
- when :paid_media
274
- params.merge(payload.is_a?(Hash) ? payload : {})
275
- when :cached_media
276
- params.merge(document: payload)
277
- when :checklist
278
- params.merge(checklist: payload)
279
- else
280
- params
281
- end
282
- end
283
-
284
- def resolve_file_id
285
- return @document.file_id if @document&.respond_to?(:file_id)
286
- return @audio.file_id if @audio&.respond_to?(:file_id)
287
- return @video.file_id if @video&.respond_to?(:file_id)
288
- return @voice.file_id if @voice&.respond_to?(:file_id)
289
- return @animation.file_id if @animation&.respond_to?(:file_id)
290
- return @photo.last.file_id if @photo.is_a?(Array) && @photo.last&.respond_to?(:file_id)
291
-
292
- nil
293
- end
294
- end
295
- end
1
+ require_relative "base_object"
2
+ require_relative "user"
3
+ require_relative "chat"
4
+ require_relative "message_entity"
5
+ require_relative "extra"
6
+
7
+ module GrubY
8
+ class Message < BaseObject
9
+ fields :message_id, :message_thread_id, :direct_messages_topic, :from,
10
+ :sender_chat, :sender_boost_count, :sender_business_bot, :sender_tag,
11
+ :guest_query_id, :guest_bot_caller_user, :guest_bot_caller_chat,
12
+ :date, :business_connection_id, :chat, :forward_origin, :is_topic_message,
13
+ :is_automatic_forward, :reply_to_message, :external_reply, :quote,
14
+ :reply_to_story, :reply_to_checklist_task_id, :reply_to_poll_option_id,
15
+ :via_bot, :edit_date, :has_protected_content, :is_from_offline,
16
+ :is_paid_post, :media_group_id, :author_signature, :paid_star_count,
17
+ :text, :entities, :link_preview_options, :suggested_post_info, :effect_id,
18
+ :rich_message,
19
+ :animation, :audio, :document, :paid_media, :photo, :sticker, :story,
20
+ :video, :video_note, :voice, :caption, :caption_entities,
21
+ :show_caption_above_media, :has_media_spoiler, :checklist, :contact, :dice,
22
+ :game, :poll, :venue, :location, :new_chat_members, :left_chat_member,
23
+ :chat_owner_left, :chat_owner_changed, :new_chat_title, :new_chat_photo,
24
+ :delete_chat_photo, :group_chat_created, :supergroup_chat_created,
25
+ :channel_chat_created, :message_auto_delete_timer_changed,
26
+ :migrate_to_chat_id, :migrate_from_chat_id, :pinned_message, :invoice,
27
+ :successful_payment, :refunded_payment, :users_shared, :chat_shared, :gift,
28
+ :unique_gift, :gift_upgrade_sent, :connected_website,
29
+ :write_access_allowed, :passport_data, :proximity_alert_triggered,
30
+ :boost_added, :chat_background_set, :checklist_tasks_done,
31
+ :checklist_tasks_added, :direct_message_price_changed,
32
+ :forum_topic_created, :forum_topic_edited, :forum_topic_closed,
33
+ :forum_topic_reopened, :general_forum_topic_hidden,
34
+ :general_forum_topic_unhidden, :giveaway_created, :giveaway,
35
+ :giveaway_winners, :giveaway_completed, :managed_bot_created,
36
+ :paid_message_price_changed, :poll_option_added, :poll_option_deleted,
37
+ :suggested_post_approved, :suggested_post_approval_failed,
38
+ :suggested_post_declined, :suggested_post_paid,
39
+ :suggested_post_refunded, :video_chat_scheduled, :video_chat_started,
40
+ :video_chat_ended, :video_chat_participants_invited, :web_app_data,
41
+ :reply_markup, :widget, :receiver_user, :ephemeral_message_id,
42
+ :community_chat_added, :community_chat_joined, :community_chat_removed,
43
+ :paid_messages_waived, :ton_wallet_transfer
44
+
45
+ def initialize(data, api: nil, client: nil)
46
+ super(data, api: api, client: client)
47
+ @from = User.new(@from, api: api, client: client) if @from.is_a?(Hash)
48
+ @chat = Chat.new(@chat, api: api, client: client) if @chat.is_a?(Hash)
49
+ @sender_chat = Chat.new(@sender_chat, api: api, client: client) if @sender_chat.is_a?(Hash)
50
+ @sender_business_bot = User.new(@sender_business_bot, api: api, client: client) if @sender_business_bot.is_a?(Hash)
51
+ @guest_bot_caller_user = User.new(@guest_bot_caller_user, api: api, client: client) if @guest_bot_caller_user.is_a?(Hash)
52
+ @guest_bot_caller_chat = Chat.new(@guest_bot_caller_chat, api: api, client: client) if @guest_bot_caller_chat.is_a?(Hash)
53
+ @via_bot = User.new(@via_bot, api: api, client: client) if @via_bot.is_a?(Hash)
54
+ @reply_to_message = Message.new(@reply_to_message, api: api, client: client) if @reply_to_message.is_a?(Hash)
55
+ @external_reply = ExternalReplyInfo.new(@external_reply) if @external_reply.is_a?(Hash)
56
+ @quote = TextQuote.new(@quote) if @quote.is_a?(Hash)
57
+ @reply_to_story = Story.new(@reply_to_story) if @reply_to_story.is_a?(Hash)
58
+ @entities = Array(@entities).map { |e| MessageEntity.new(e) }
59
+ @caption_entities = Array(@caption_entities).map { |e| MessageEntity.new(e) }
60
+ @photo = Array(@photo).map { |p| PhotoSize.new(p) }
61
+ @animation = Animation.new(@animation) if @animation.is_a?(Hash)
62
+ @audio = Audio.new(@audio) if @audio.is_a?(Hash)
63
+ @document = Document.new(@document) if @document.is_a?(Hash)
64
+ @story = Story.new(@story) if @story.is_a?(Hash)
65
+ @video = Video.new(@video) if @video.is_a?(Hash)
66
+ @video_note = VideoNote.new(@video_note) if @video_note.is_a?(Hash)
67
+ @voice = Voice.new(@voice) if @voice.is_a?(Hash)
68
+ @paid_media = PaidMediaInfo.new(@paid_media) if @paid_media.is_a?(Hash)
69
+ @contact = Contact.new(@contact) if @contact.is_a?(Hash)
70
+ @dice = Dice.new(@dice) if @dice.is_a?(Hash)
71
+ @poll = Poll.new(@poll) if @poll.is_a?(Hash)
72
+ @new_chat_members = Array(@new_chat_members).map { |u| User.new(u, api: api, client: client) }
73
+ @left_chat_member = User.new(@left_chat_member, api: api, client: client) if @left_chat_member.is_a?(Hash)
74
+ end
75
+
76
+ def reply(text = nil, **opts)
77
+ call_api("sendMessage", { chat_id: chat_id!, text: text.to_s, reply_to_message_id: message_id }.merge(opts))
78
+ end
79
+
80
+ def reply_text(text = nil, **opts)
81
+ reply(text, **opts)
82
+ end
83
+
84
+ def answer(text = nil, **opts)
85
+ reply(text, **opts)
86
+ end
87
+
88
+ def edit_text(text, **opts)
89
+ call_api("editMessageText", { chat_id: chat_id!, message_id: message_id, text: text.to_s }.merge(opts))
90
+ end
91
+
92
+ alias edit edit_text
93
+
94
+ def edit_caption(caption, **opts)
95
+ call_api("editMessageCaption", { chat_id: chat_id!, message_id: message_id, caption: caption.to_s }.merge(opts))
96
+ end
97
+
98
+ def edit_media(media, **opts)
99
+ call_api("editMessageMedia", { chat_id: chat_id!, message_id: message_id, media: media }.merge(opts))
100
+ end
101
+
102
+ def edit_checklist(checklist, **opts)
103
+ call_api("editMessageChecklist", { chat_id: chat_id!, message_id: message_id, checklist: checklist }.merge(opts))
104
+ end
105
+
106
+ def edit_reply_markup(reply_markup = nil, **opts)
107
+ call_api("editMessageReplyMarkup", { chat_id: chat_id!, message_id: message_id, reply_markup: reply_markup }.merge(opts))
108
+ end
109
+
110
+ def edit_live_location(latitude:, longitude:, **opts)
111
+ call_api("editMessageLiveLocation", { chat_id: chat_id!, message_id: message_id, latitude: latitude, longitude: longitude }.merge(opts))
112
+ end
113
+
114
+ def stop_live_location(**opts)
115
+ call_api("stopMessageLiveLocation", { chat_id: chat_id!, message_id: message_id }.merge(opts))
116
+ end
117
+
118
+ def forward(to_chat_id:, from_chat_id: chat_id!, **opts)
119
+ call_api("forwardMessage", { chat_id: to_chat_id, from_chat_id: from_chat_id, message_id: message_id }.merge(opts))
120
+ end
121
+
122
+ def copy(to_chat_id:, from_chat_id: chat_id!, **opts)
123
+ call_api("copyMessage", { chat_id: to_chat_id, from_chat_id: from_chat_id, message_id: message_id }.merge(opts))
124
+ end
125
+
126
+ def copy_media_group(to_chat_id:, from_chat_id: chat_id!, **opts)
127
+ call_api("copyMessages", { chat_id: to_chat_id, from_chat_id: from_chat_id, message_ids: [message_id] }.merge(opts))
128
+ end
129
+
130
+ def delete(**opts)
131
+ call_api("deleteMessage", { chat_id: chat_id!, message_id: message_id }.merge(opts))
132
+ end
133
+
134
+ def react(reaction:, is_big: false)
135
+ call_api("setMessageReaction", { chat_id: chat_id!, message_id: message_id, reaction: reaction, is_big: is_big })
136
+ end
137
+
138
+ def retract_vote(**opts)
139
+ call_api("stopPoll", { chat_id: chat_id!, message_id: message_id }.merge(opts))
140
+ end
141
+
142
+ def vote(option_ids, **opts)
143
+ call_api("sendPoll", { chat_id: chat_id!, options: option_ids }.merge(opts))
144
+ end
145
+
146
+ def pin(**opts)
147
+ call_api("pinChatMessage", { chat_id: chat_id!, message_id: message_id }.merge(opts))
148
+ end
149
+
150
+ def unpin(**opts)
151
+ call_api("unpinChatMessage", { chat_id: chat_id!, message_id: message_id }.merge(opts))
152
+ end
153
+
154
+ def read
155
+ call_raw_api("readChatHistory", { chat_id: chat_id! })
156
+ end
157
+
158
+ def view
159
+ call_raw_api("viewMessages", { chat_id: chat_id!, message_ids: [message_id] })
160
+ end
161
+
162
+ def pay(**opts)
163
+ call_raw_api("sendPaymentForm", { chat_id: chat_id!, message_id: message_id }.merge(opts))
164
+ end
165
+
166
+ def accept_gift_purchase_offer(**opts)
167
+ call_raw_api("processGiftPurchaseOffer", { message_id: message_id, accept: true }.merge(opts))
168
+ end
169
+
170
+ def reject_gift_purchase_offer(**opts)
171
+ call_raw_api("processGiftPurchaseOffer", { message_id: message_id, accept: false }.merge(opts))
172
+ end
173
+
174
+ def summarize(summary_language_code: "en")
175
+ call_raw_api("summarizeMessage", { chat_id: chat_id!, message_id: message_id, summary_language_code: summary_language_code })
176
+ end
177
+
178
+ def fix_text_with_ai(**opts)
179
+ call_raw_api("fixTextWithAi", { chat_id: chat_id!, message_id: message_id }.merge(opts))
180
+ end
181
+
182
+ def compose_text_with_ai(**opts)
183
+ call_raw_api("composeTextWithAi", { chat_id: chat_id!, message_id: message_id }.merge(opts))
184
+ end
185
+
186
+ def reply_inline_bot_result(result_id:, query_id:, **opts)
187
+ call_raw_api("sendInlineBotResult", { chat_id: chat_id!, query_id: query_id, result_id: result_id }.merge(opts))
188
+ end
189
+
190
+ def answer_inline_bot_result(result_id:, query_id:, **opts)
191
+ reply_inline_bot_result(result_id: result_id, query_id: query_id, **opts)
192
+ end
193
+
194
+ def get_media_group
195
+ return nil if media_group_id.to_s.empty?
196
+
197
+ call_raw_api("getMediaGroup", { chat_id: chat_id!, message_id: message_id })
198
+ end
199
+
200
+ def reply_chat_action(action)
201
+ call_api("sendChatAction", { chat_id: chat_id!, action: action })
202
+ end
203
+
204
+ def click
205
+ raise NotImplementedError, "click() requires callback query context"
206
+ end
207
+
208
+ def download(file_name = "file.dat")
209
+ file_id = resolve_file_id
210
+ raise ArgumentError, "message has no downloadable media" if file_id.nil?
211
+
212
+ f = call_api("getFile", { file_id: file_id })
213
+ call_raw_api("getFile", { file_id: file_id }) unless f
214
+ file_path = f && f["file_path"]
215
+ raise ArgumentError, "file_path not found for file_id=#{file_id}" if file_path.to_s.empty?
216
+
217
+ @api.download_file(file_path, file_name)
218
+ file_name
219
+ end
220
+
221
+ SENDERS = {
222
+ animation: "sendAnimation",
223
+ audio: "sendAudio",
224
+ contact: "sendContact",
225
+ document: "sendDocument",
226
+ game: "sendGame",
227
+ invoice: "sendInvoice",
228
+ location: "sendLocation",
229
+ media_group: "sendMediaGroup",
230
+ photo: "sendPhoto",
231
+ poll: "sendPoll",
232
+ dice: "sendDice",
233
+ sticker: "sendSticker",
234
+ venue: "sendVenue",
235
+ video: "sendVideo",
236
+ video_note: "sendVideoNote",
237
+ voice: "sendVoice",
238
+ paid_media: "sendPaidMedia",
239
+ cached_media: "sendDocument",
240
+ checklist: "sendChecklist"
241
+ }.freeze
242
+
243
+ SENDERS.each do |name, method_name|
244
+ define_method("reply_#{name}") do |payload = nil, **opts|
245
+ params = { chat_id: chat_id!, reply_to_message_id: message_id }.merge(opts)
246
+ params = attach_payload(name, params, payload)
247
+ call_api(method_name, params)
248
+ end
249
+
250
+ define_method("answer_#{name}") do |payload = nil, **opts|
251
+ public_send("reply_#{name}", payload, **opts)
252
+ end
253
+ end
254
+
255
+ private
256
+
257
+ def chat_id!
258
+ @chat&.id || self["chat"]&.dig("id") || raise(ArgumentError, "message has no chat id")
259
+ end
260
+
261
+ def attach_payload(kind, params, payload)
262
+ case kind
263
+ when :animation, :audio, :document, :photo, :sticker, :video, :video_note, :voice
264
+ params.merge(kind => payload)
265
+ when :contact
266
+ params.merge(payload.is_a?(Hash) ? payload : {})
267
+ when :game
268
+ params.merge(game_short_name: payload)
269
+ when :invoice
270
+ params.merge(payload.is_a?(Hash) ? payload : {})
271
+ when :location
272
+ params.merge(payload.is_a?(Hash) ? payload : {})
273
+ when :media_group
274
+ params.merge(media: payload)
275
+ when :poll
276
+ params.merge(payload.is_a?(Hash) ? payload : {})
277
+ when :venue
278
+ params.merge(payload.is_a?(Hash) ? payload : {})
279
+ when :paid_media
280
+ params.merge(payload.is_a?(Hash) ? payload : {})
281
+ when :cached_media
282
+ params.merge(document: payload)
283
+ when :checklist
284
+ params.merge(checklist: payload)
285
+ else
286
+ params
287
+ end
288
+ end
289
+
290
+ def resolve_file_id
291
+ return @document.file_id if @document&.respond_to?(:file_id)
292
+ return @audio.file_id if @audio&.respond_to?(:file_id)
293
+ return @video.file_id if @video&.respond_to?(:file_id)
294
+ return @voice.file_id if @voice&.respond_to?(:file_id)
295
+ return @animation.file_id if @animation&.respond_to?(:file_id)
296
+ return @photo.last.file_id if @photo.is_a?(Array) && @photo.last&.respond_to?(:file_id)
297
+
298
+ nil
299
+ end
300
+ end
301
+ end
@@ -1,14 +1,14 @@
1
- require_relative "base_object"
2
- require_relative "user"
3
-
4
- module GrubY
5
- class MessageEntity < BaseObject
6
- fields :type, :offset, :length, :url, :user, :language, :custom_emoji_id,
7
- :unix_time, :date_time_format
8
-
9
- def initialize(data)
10
- super(data)
11
- @user = User.new(@user) if @user.is_a?(Hash)
12
- end
13
- end
14
- end
1
+ require_relative "base_object"
2
+ require_relative "user"
3
+
4
+ module GrubY
5
+ class MessageEntity < BaseObject
6
+ fields :type, :offset, :length, :url, :user, :language, :custom_emoji_id,
7
+ :unix_time, :date_time_format
8
+
9
+ def initialize(data)
10
+ super(data)
11
+ @user = User.new(@user) if @user.is_a?(Hash)
12
+ end
13
+ end
14
+ end