gruubY 0.2.1 → 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 (57) 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/config/config.rb +1 -1
  6. data/example/bot.rb +122 -2
  7. data/example/generate_user_session.rb +81 -81
  8. data/example/tdlib.rb +77 -77
  9. data/example/userbot.rb +133 -133
  10. data/lib/gruubY/api.rb +540 -363
  11. data/lib/gruubY/async.rb +13 -13
  12. data/lib/gruubY/bot.rb +104 -90
  13. data/lib/gruubY/bound.rb +14 -14
  14. data/lib/gruubY/client.rb +310 -296
  15. data/lib/gruubY/context.rb +296 -242
  16. data/lib/gruubY/dispatcher.rb +60 -60
  17. data/lib/gruubY/enums.rb +57 -62
  18. data/lib/gruubY/exception.rb +5 -5
  19. data/lib/gruubY/file_stream.rb +17 -17
  20. data/lib/gruubY/filters.rb +292 -292
  21. data/lib/gruubY/group_manager.rb +213 -213
  22. data/lib/gruubY/handlers.rb +225 -170
  23. data/lib/gruubY/keyboard.rb +49 -49
  24. data/lib/gruubY/media.rb +38 -38
  25. data/lib/gruubY/middleware.rb +18 -18
  26. data/lib/gruubY/plugin.rb +27 -14
  27. data/lib/gruubY/raw.rb +25 -25
  28. data/lib/gruubY/retry.rb +14 -14
  29. data/lib/gruubY/server.rb +18 -18
  30. data/lib/gruubY/session.rb +30 -30
  31. data/lib/gruubY/tdlib/client.rb +565 -565
  32. data/lib/gruubY/tdlib/client_manager.rb +37 -37
  33. data/lib/gruubY/tdlib/decorators.rb +17 -17
  34. data/lib/gruubY/tdlib/errors.rb +6 -6
  35. data/lib/gruubY/tdlib/group_manager.rb +58 -58
  36. data/lib/gruubY/tdlib/native.rb +72 -72
  37. data/lib/gruubY/tdlib/schema_builder.rb +237 -237
  38. data/lib/gruubY/tdlib/tdjson.rb +49 -49
  39. data/lib/gruubY/tdlib/user_session.rb +69 -69
  40. data/lib/gruubY/types/base_object.rb +72 -72
  41. data/lib/gruubY/types/bound_entities.rb +289 -245
  42. data/lib/gruubY/types/chat.rb +93 -93
  43. data/lib/gruubY/types/chat_info.rb +32 -31
  44. data/lib/gruubY/types/extra.rb +289 -251
  45. data/lib/gruubY/types/message.rb +301 -295
  46. data/lib/gruubY/types/message_entity.rb +14 -14
  47. data/lib/gruubY/types/registry.rb +86 -67
  48. data/lib/gruubY/types/user.rb +47 -31
  49. data/lib/gruubY/webapp.rb +22 -22
  50. data/lib/gruubY.rb +39 -41
  51. data/plugins/logger.rb +7 -7
  52. metadata +3 -9
  53. data/example/ntgcalls.rb +0 -80
  54. data/lib/gruubY/ntgcalls/native.rb +0 -100
  55. data/lib/gruubY/ntgcalls.rb +0 -478
  56. data/lib/gruubY/raw_types.rb +0 -57
  57. data/plugins/media_tools.rb +0 -13
@@ -1,245 +1,289 @@
1
- require_relative "base_object"
2
-
3
- module GrubY
4
- class CallbackQuery < BaseObject
5
- fields :id, :from, :message, :inline_message_id, :chat_instance, :data, :game_short_name
6
-
7
- def answer(text = nil, show_alert: false, **opts)
8
- call_api("answerCallbackQuery", { callback_query_id: id, text: text, show_alert: show_alert }.merge(opts))
9
- end
10
-
11
- def edit_message_text(text, **opts)
12
- edit_payload("editMessageText", { text: text }.merge(opts))
13
- end
14
-
15
- def edit_message_caption(caption, **opts)
16
- edit_payload("editMessageCaption", { caption: caption }.merge(opts))
17
- end
18
-
19
- def edit_message_media(media, **opts)
20
- edit_payload("editMessageMedia", { media: media }.merge(opts))
21
- end
22
-
23
- def edit_message_reply_markup(reply_markup = nil, **opts)
24
- edit_payload("editMessageReplyMarkup", { reply_markup: reply_markup }.merge(opts))
25
- end
26
-
27
- private
28
-
29
- def edit_payload(method, payload)
30
- if inline_message_id.to_s.empty?
31
- msg = message || {}
32
- call_api(method, payload.merge(chat_id: msg.dig("chat", "id"), message_id: msg["message_id"]))
33
- else
34
- call_api(method, payload.merge(inline_message_id: inline_message_id))
35
- end
36
- end
37
- end
38
-
39
- class InlineQuery < BaseObject
40
- fields :id, :from, :query, :offset, :chat_type, :location
41
-
42
- def answer(results, **opts)
43
- call_api("answerInlineQuery", { inline_query_id: id, results: results }.merge(opts))
44
- end
45
- end
46
-
47
- class PreCheckoutQuery < BaseObject
48
- fields :id, :from, :currency, :total_amount, :invoice_payload, :shipping_option_id, :order_info
49
-
50
- def answer(ok:, error_message: nil)
51
- call_api("answerPreCheckoutQuery", { pre_checkout_query_id: id, ok: ok, error_message: error_message }.compact)
52
- end
53
- end
54
-
55
- class ShippingQuery < BaseObject
56
- fields :id, :from, :invoice_payload, :shipping_address
57
-
58
- def answer(ok:, shipping_options: nil, error_message: nil)
59
- call_api("answerShippingQuery", {
60
- shipping_query_id: id,
61
- ok: ok,
62
- shipping_options: shipping_options,
63
- error_message: error_message
64
- }.compact)
65
- end
66
- end
67
-
68
- class ChatJoinRequest < BaseObject
69
- fields :chat, :from, :user_chat_id, :date, :bio, :invite_link
70
-
71
- def approve
72
- call_api("approveChatJoinRequest", { chat_id: chat_id!, user_id: user_id! })
73
- end
74
-
75
- def decline
76
- call_api("declineChatJoinRequest", { chat_id: chat_id!, user_id: user_id! })
77
- end
78
-
79
- private
80
-
81
- def chat_id!
82
- chat.is_a?(Hash) ? chat["id"] : chat&.id
83
- end
84
-
85
- def user_id!
86
- from.is_a?(Hash) ? from["id"] : from&.id
87
- end
88
- end
89
-
90
- class Story < BaseObject
91
- fields :id, :chat, :date
92
-
93
- def reply_text(text, **opts)
94
- chat_id = chat.is_a?(Hash) ? chat["id"] : chat&.id
95
- call_api("sendMessage", { chat_id: chat_id, text: text.to_s }.merge(opts))
96
- end
97
-
98
- alias reply reply_text
99
- alias answer reply_text
100
-
101
- STORY_SENDERS = {
102
- animation: "sendAnimation",
103
- audio: "sendAudio",
104
- cached_media: "sendDocument",
105
- media_group: "sendMediaGroup",
106
- photo: "sendPhoto",
107
- sticker: "sendSticker",
108
- video: "sendVideo",
109
- video_note: "sendVideoNote",
110
- voice: "sendVoice"
111
- }.freeze
112
-
113
- STORY_SENDERS.each do |name, method_name|
114
- define_method("reply_#{name}") do |payload = nil, **opts|
115
- cid = chat.is_a?(Hash) ? chat["id"] : chat&.id
116
- params = { chat_id: cid }.merge(opts)
117
- params = if name == :media_group
118
- params.merge(media: payload)
119
- elsif name == :cached_media
120
- params.merge(document: payload)
121
- else
122
- params.merge(name => payload)
123
- end
124
- call_api(method_name, params)
125
- end
126
-
127
- define_method("answer_#{name}") do |payload = nil, **opts|
128
- public_send("reply_#{name}", payload, **opts)
129
- end
130
- end
131
-
132
- def delete
133
- call_raw_api("deleteStories", { story_ids: [id] })
134
- end
135
-
136
- def edit_media(media, **opts)
137
- call_raw_api("editStoryMedia", { story_id: id, media: media }.merge(opts))
138
- end
139
-
140
- def edit_caption(caption, **opts)
141
- call_raw_api("editStoryCaption", { story_id: id, caption: caption.to_s }.merge(opts))
142
- end
143
-
144
- def edit_privacy(privacy, **opts)
145
- call_raw_api("editStoryPrivacy", { story_id: id, privacy: privacy }.merge(opts))
146
- end
147
-
148
- def react(reaction)
149
- call_raw_api("setStoryReaction", { story_id: id, reaction: reaction })
150
- end
151
-
152
- def forward(chat_id:)
153
- call_raw_api("forwardStory", { story_id: id, chat_id: chat_id })
154
- end
155
-
156
- def download(file_name = "story.dat")
157
- call_raw_api("downloadFile", { story_id: id, file_name: file_name })
158
- end
159
-
160
- def read
161
- call_raw_api("readChatStories", { story_id: id })
162
- end
163
-
164
- def view
165
- call_raw_api("viewStories", { story_ids: [id] })
166
- end
167
- end
168
-
169
- class Folder < BaseObject
170
- fields :id, :name
171
-
172
- def delete
173
- call_raw_api("deleteFolder", { folder_id: id })
174
- end
175
-
176
- def edit(**opts)
177
- call_raw_api("editFolder", { folder_id: id }.merge(opts))
178
- end
179
- end
180
-
181
- class ActiveSession < BaseObject
182
- fields :hash
183
-
184
- def reset
185
- call_raw_api("resetSession", { session_hash: self["hash"] || hash })
186
- end
187
- end
188
-
189
- class Gift < BaseObject
190
- fields :id, :owned_gift_id
191
-
192
- def show
193
- call_raw_api("showGift", gift_payload)
194
- end
195
-
196
- def hide
197
- call_raw_api("hideGift", gift_payload)
198
- end
199
-
200
- def convert
201
- call_raw_api("convertGiftToStars", gift_payload)
202
- end
203
-
204
- def upgrade(**opts)
205
- call_raw_api("upgradeGift", gift_payload.merge(opts))
206
- end
207
-
208
- def transfer(user_id:, **opts)
209
- call_raw_api("transferGift", gift_payload.merge(user_id: user_id).merge(opts))
210
- end
211
-
212
- def wear
213
- call_raw_api("setProfileGift", gift_payload)
214
- end
215
-
216
- def buy(**opts)
217
- call_raw_api("sendGift", gift_payload.merge(opts))
218
- end
219
-
220
- def send(user_id:, **opts)
221
- call_raw_api("sendGift", gift_payload.merge(user_id: user_id).merge(opts))
222
- end
223
-
224
- def get_auction_state
225
- call_raw_api("getGiftAuctionState", gift_payload)
226
- end
227
-
228
- def send_purchase_offer(**opts)
229
- call_raw_api("sendGiftPurchaseOffer", gift_payload.merge(opts))
230
- end
231
-
232
- private
233
-
234
- def gift_payload
235
- { gift_id: self["id"] || id, owned_gift_id: self["owned_gift_id"] || owned_gift_id }.compact
236
- end
237
- end
238
-
239
- class Animation < BaseObject
240
- def add_to_gifs
241
- file_id = self["file_id"] || dig("file_id")
242
- call_raw_api("addToGifs", { file_id: file_id })
243
- end
244
- end
245
- end
1
+ require_relative "base_object"
2
+ require_relative "extra"
3
+
4
+ module GrubY
5
+ class Update < BaseObject
6
+ fields :update_id, :message, :edited_message, :channel_post,
7
+ :edited_channel_post, :business_connection, :business_message,
8
+ :edited_business_message, :deleted_business_messages,
9
+ :deleted_messages, :guest_message, :message_reaction, :message_reaction_count,
10
+ :inline_query, :chosen_inline_result, :callback_query,
11
+ :shipping_query, :pre_checkout_query, :purchased_paid_media,
12
+ :poll, :poll_answer, :chat_member, :my_chat_member,
13
+ :chat_join_request, :chat_boost, :removed_chat_boost,
14
+ :widget_query, :shared_widget_query, :bot_subscription_updated,
15
+ :message_generation_stopped, :community_chat_joined,
16
+ :managed_bot_updated, :managed_bot, :subscription,
17
+ :stopped_message_generation
18
+
19
+ def initialize(data, api: nil, client: nil)
20
+ super(data, api: api, client: client)
21
+ @message = Message.new(@message, api: api, client: client) if @message.is_a?(Hash)
22
+ @edited_message = Message.new(@edited_message, api: api, client: client) if @edited_message.is_a?(Hash)
23
+ @channel_post = Message.new(@channel_post, api: api, client: client) if @channel_post.is_a?(Hash)
24
+ @edited_channel_post = Message.new(@edited_channel_post, api: api, client: client) if @edited_channel_post.is_a?(Hash)
25
+ @business_message = Message.new(@business_message, api: api, client: client) if @business_message.is_a?(Hash)
26
+ @edited_business_message = Message.new(@edited_business_message, api: api, client: client) if @edited_business_message.is_a?(Hash)
27
+ @guest_message = Message.new(@guest_message, api: api, client: client) if @guest_message.is_a?(Hash)
28
+ @callback_query = CallbackQuery.new(@callback_query, api: api, client: client) if @callback_query.is_a?(Hash)
29
+ @inline_query = InlineQuery.new(@inline_query, api: api, client: client) if @inline_query.is_a?(Hash)
30
+ @poll_answer = PollAnswer.new(@poll_answer, api: api, client: client) if @poll_answer.is_a?(Hash)
31
+ @widget_query = wrap_update_type("WidgetQuery", @widget_query, api, client)
32
+ @shared_widget_query = wrap_update_type("SharedWidgetQuery", @shared_widget_query, api, client)
33
+ @bot_subscription_updated = wrap_update_type("BotSubscriptionUpdated", @bot_subscription_updated, api, client)
34
+ @message_generation_stopped = wrap_update_type("MessageGenerationStopped", @message_generation_stopped, api, client)
35
+ end
36
+
37
+ private
38
+
39
+ def wrap_update_type(type_name, data, api, client)
40
+ return data unless data.is_a?(Hash)
41
+ return data unless GrubY.const_defined?(type_name)
42
+
43
+ klass = GrubY.const_get(type_name)
44
+ klass.new(data, api: api, client: client)
45
+ end
46
+ end
47
+
48
+ class CallbackQuery < BaseObject
49
+ fields :id, :from, :message, :inline_message_id, :chat_instance, :data, :game_short_name
50
+
51
+ def answer(text = nil, show_alert: false, **opts)
52
+ call_api("answerCallbackQuery", { callback_query_id: id, text: text, show_alert: show_alert }.merge(opts))
53
+ end
54
+
55
+ def edit_message_text(text, **opts)
56
+ edit_payload("editMessageText", { text: text }.merge(opts))
57
+ end
58
+
59
+ def edit_message_caption(caption, **opts)
60
+ edit_payload("editMessageCaption", { caption: caption }.merge(opts))
61
+ end
62
+
63
+ def edit_message_media(media, **opts)
64
+ edit_payload("editMessageMedia", { media: media }.merge(opts))
65
+ end
66
+
67
+ def edit_message_reply_markup(reply_markup = nil, **opts)
68
+ edit_payload("editMessageReplyMarkup", { reply_markup: reply_markup }.merge(opts))
69
+ end
70
+
71
+ private
72
+
73
+ def edit_payload(method, payload)
74
+ if inline_message_id.to_s.empty?
75
+ msg = message || {}
76
+ call_api(method, payload.merge(chat_id: msg.dig("chat", "id"), message_id: msg["message_id"]))
77
+ else
78
+ call_api(method, payload.merge(inline_message_id: inline_message_id))
79
+ end
80
+ end
81
+ end
82
+
83
+ class InlineQuery < BaseObject
84
+ fields :id, :from, :query, :offset, :chat_type, :location
85
+
86
+ def answer(results, **opts)
87
+ call_api("answerInlineQuery", { inline_query_id: id, results: results }.merge(opts))
88
+ end
89
+ end
90
+
91
+ class PreCheckoutQuery < BaseObject
92
+ fields :id, :from, :currency, :total_amount, :invoice_payload, :shipping_option_id, :order_info
93
+
94
+ def answer(ok:, error_message: nil)
95
+ call_api("answerPreCheckoutQuery", { pre_checkout_query_id: id, ok: ok, error_message: error_message }.compact)
96
+ end
97
+ end
98
+
99
+ class ShippingQuery < BaseObject
100
+ fields :id, :from, :invoice_payload, :shipping_address
101
+
102
+ def answer(ok:, shipping_options: nil, error_message: nil)
103
+ call_api("answerShippingQuery", {
104
+ shipping_query_id: id,
105
+ ok: ok,
106
+ shipping_options: shipping_options,
107
+ error_message: error_message
108
+ }.compact)
109
+ end
110
+ end
111
+
112
+ class ChatJoinRequest < BaseObject
113
+ fields :chat, :from, :user_chat_id, :date, :bio, :invite_link
114
+
115
+ def approve
116
+ call_api("approveChatJoinRequest", { chat_id: chat_id!, user_id: user_id! })
117
+ end
118
+
119
+ def decline
120
+ call_api("declineChatJoinRequest", { chat_id: chat_id!, user_id: user_id! })
121
+ end
122
+
123
+ private
124
+
125
+ def chat_id!
126
+ chat.is_a?(Hash) ? chat["id"] : chat&.id
127
+ end
128
+
129
+ def user_id!
130
+ from.is_a?(Hash) ? from["id"] : from&.id
131
+ end
132
+ end
133
+
134
+ class Story < BaseObject
135
+ fields :id, :chat, :date
136
+
137
+ def reply_text(text, **opts)
138
+ chat_id = chat.is_a?(Hash) ? chat["id"] : chat&.id
139
+ call_api("sendMessage", { chat_id: chat_id, text: text.to_s }.merge(opts))
140
+ end
141
+
142
+ alias reply reply_text
143
+ alias answer reply_text
144
+
145
+ STORY_SENDERS = {
146
+ animation: "sendAnimation",
147
+ audio: "sendAudio",
148
+ cached_media: "sendDocument",
149
+ media_group: "sendMediaGroup",
150
+ photo: "sendPhoto",
151
+ sticker: "sendSticker",
152
+ video: "sendVideo",
153
+ video_note: "sendVideoNote",
154
+ voice: "sendVoice"
155
+ }.freeze
156
+
157
+ STORY_SENDERS.each do |name, method_name|
158
+ define_method("reply_#{name}") do |payload = nil, **opts|
159
+ cid = chat.is_a?(Hash) ? chat["id"] : chat&.id
160
+ params = { chat_id: cid }.merge(opts)
161
+ params = if name == :media_group
162
+ params.merge(media: payload)
163
+ elsif name == :cached_media
164
+ params.merge(document: payload)
165
+ else
166
+ params.merge(name => payload)
167
+ end
168
+ call_api(method_name, params)
169
+ end
170
+
171
+ define_method("answer_#{name}") do |payload = nil, **opts|
172
+ public_send("reply_#{name}", payload, **opts)
173
+ end
174
+ end
175
+
176
+ def delete
177
+ call_raw_api("deleteStories", { story_ids: [id] })
178
+ end
179
+
180
+ def edit_media(media, **opts)
181
+ call_raw_api("editStoryMedia", { story_id: id, media: media }.merge(opts))
182
+ end
183
+
184
+ def edit_caption(caption, **opts)
185
+ call_raw_api("editStoryCaption", { story_id: id, caption: caption.to_s }.merge(opts))
186
+ end
187
+
188
+ def edit_privacy(privacy, **opts)
189
+ call_raw_api("editStoryPrivacy", { story_id: id, privacy: privacy }.merge(opts))
190
+ end
191
+
192
+ def react(reaction)
193
+ call_raw_api("setStoryReaction", { story_id: id, reaction: reaction })
194
+ end
195
+
196
+ def forward(chat_id:)
197
+ call_raw_api("forwardStory", { story_id: id, chat_id: chat_id })
198
+ end
199
+
200
+ def download(file_name = "story.dat")
201
+ call_raw_api("downloadFile", { story_id: id, file_name: file_name })
202
+ end
203
+
204
+ def read
205
+ call_raw_api("readChatStories", { story_id: id })
206
+ end
207
+
208
+ def view
209
+ call_raw_api("viewStories", { story_ids: [id] })
210
+ end
211
+ end
212
+
213
+ class Folder < BaseObject
214
+ fields :id, :name
215
+
216
+ def delete
217
+ call_raw_api("deleteFolder", { folder_id: id })
218
+ end
219
+
220
+ def edit(**opts)
221
+ call_raw_api("editFolder", { folder_id: id }.merge(opts))
222
+ end
223
+ end
224
+
225
+ class ActiveSession < BaseObject
226
+ fields :hash
227
+
228
+ def reset
229
+ call_raw_api("resetSession", { session_hash: self["hash"] || hash })
230
+ end
231
+ end
232
+
233
+ class Gift < BaseObject
234
+ fields :id, :owned_gift_id
235
+
236
+ def show
237
+ call_raw_api("showGift", gift_payload)
238
+ end
239
+
240
+ def hide
241
+ call_raw_api("hideGift", gift_payload)
242
+ end
243
+
244
+ def convert
245
+ call_raw_api("convertGiftToStars", gift_payload)
246
+ end
247
+
248
+ def upgrade(**opts)
249
+ call_raw_api("upgradeGift", gift_payload.merge(opts))
250
+ end
251
+
252
+ def transfer(user_id:, **opts)
253
+ call_raw_api("transferGift", gift_payload.merge(user_id: user_id).merge(opts))
254
+ end
255
+
256
+ def wear
257
+ call_raw_api("setProfileGift", gift_payload)
258
+ end
259
+
260
+ def buy(**opts)
261
+ call_raw_api("sendGift", gift_payload.merge(opts))
262
+ end
263
+
264
+ def send(user_id:, **opts)
265
+ call_raw_api("sendGift", gift_payload.merge(user_id: user_id).merge(opts))
266
+ end
267
+
268
+ def get_auction_state
269
+ call_raw_api("getGiftAuctionState", gift_payload)
270
+ end
271
+
272
+ def send_purchase_offer(**opts)
273
+ call_raw_api("sendGiftPurchaseOffer", gift_payload.merge(opts))
274
+ end
275
+
276
+ private
277
+
278
+ def gift_payload
279
+ { gift_id: self["id"] || id, owned_gift_id: self["owned_gift_id"] || owned_gift_id }.compact
280
+ end
281
+ end
282
+
283
+ class Animation < BaseObject
284
+ def add_to_gifs
285
+ file_id = self["file_id"] || dig("file_id")
286
+ call_raw_api("addToGifs", { file_id: file_id })
287
+ end
288
+ end
289
+ end