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.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +11 -6
- data/Rakefile +51 -51
- data/example/bot.rb +120 -1
- data/example/generate_user_session.rb +81 -81
- data/example/tdlib.rb +77 -77
- data/example/userbot.rb +133 -133
- data/lib/gruubY/api.rb +540 -363
- data/lib/gruubY/async.rb +13 -13
- data/lib/gruubY/bot.rb +104 -90
- data/lib/gruubY/bound.rb +14 -14
- data/lib/gruubY/client.rb +310 -296
- data/lib/gruubY/context.rb +296 -242
- data/lib/gruubY/dispatcher.rb +60 -60
- data/lib/gruubY/enums.rb +57 -62
- data/lib/gruubY/exception.rb +5 -5
- data/lib/gruubY/file_stream.rb +17 -17
- data/lib/gruubY/filters.rb +292 -292
- data/lib/gruubY/group_manager.rb +213 -213
- data/lib/gruubY/handlers.rb +225 -170
- data/lib/gruubY/keyboard.rb +49 -49
- data/lib/gruubY/media.rb +38 -38
- data/lib/gruubY/middleware.rb +18 -18
- data/lib/gruubY/plugin.rb +26 -26
- data/lib/gruubY/raw.rb +25 -25
- data/lib/gruubY/retry.rb +14 -14
- data/lib/gruubY/server.rb +18 -18
- data/lib/gruubY/session.rb +30 -30
- data/lib/gruubY/tdlib/client.rb +565 -565
- data/lib/gruubY/tdlib/client_manager.rb +37 -37
- data/lib/gruubY/tdlib/decorators.rb +17 -17
- data/lib/gruubY/tdlib/errors.rb +6 -6
- data/lib/gruubY/tdlib/group_manager.rb +58 -58
- data/lib/gruubY/tdlib/native.rb +72 -72
- data/lib/gruubY/tdlib/schema_builder.rb +237 -237
- data/lib/gruubY/tdlib/tdjson.rb +49 -49
- data/lib/gruubY/tdlib/user_session.rb +69 -69
- data/lib/gruubY/types/base_object.rb +72 -72
- data/lib/gruubY/types/bound_entities.rb +289 -245
- data/lib/gruubY/types/chat.rb +93 -93
- data/lib/gruubY/types/chat_info.rb +32 -31
- data/lib/gruubY/types/extra.rb +289 -251
- data/lib/gruubY/types/message.rb +301 -295
- data/lib/gruubY/types/message_entity.rb +14 -14
- data/lib/gruubY/types/registry.rb +86 -67
- data/lib/gruubY/types/user.rb +47 -31
- data/lib/gruubY/webapp.rb +22 -22
- data/lib/gruubY.rb +39 -41
- data/plugins/logger.rb +7 -7
- metadata +3 -9
- data/example/ntgcalls.rb +0 -80
- data/lib/gruubY/ntgcalls/native.rb +0 -100
- data/lib/gruubY/ntgcalls.rb +0 -478
- data/lib/gruubY/raw_types.rb +0 -57
- data/plugins/media_tools.rb +0 -13
data/lib/gruubY/api.rb
CHANGED
|
@@ -1,363 +1,540 @@
|
|
|
1
|
-
require "json"
|
|
2
|
-
require "net/http"
|
|
3
|
-
require "uri"
|
|
4
|
-
require_relative "retry"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
def
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
def
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
end
|
|
214
|
-
|
|
215
|
-
def
|
|
216
|
-
request("
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}.merge(opts))
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
def
|
|
224
|
-
request("
|
|
225
|
-
end
|
|
226
|
-
|
|
227
|
-
def
|
|
228
|
-
request("
|
|
229
|
-
end
|
|
230
|
-
|
|
231
|
-
def
|
|
232
|
-
request("
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
def
|
|
236
|
-
request("
|
|
237
|
-
end
|
|
238
|
-
|
|
239
|
-
def
|
|
240
|
-
request("
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}.merge(opts))
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
def
|
|
248
|
-
request("
|
|
249
|
-
end
|
|
250
|
-
|
|
251
|
-
def
|
|
252
|
-
request("
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
end
|
|
343
|
-
|
|
344
|
-
def
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
1
|
+
require "json"
|
|
2
|
+
require "net/http"
|
|
3
|
+
require "uri"
|
|
4
|
+
require_relative "retry"
|
|
5
|
+
require "stringio"
|
|
6
|
+
|
|
7
|
+
module GrubY
|
|
8
|
+
class API
|
|
9
|
+
class Error < StandardError; end
|
|
10
|
+
BOT_API_VERSION = "10.5".freeze
|
|
11
|
+
OFFICIAL_METHODS = %w[
|
|
12
|
+
getFile getUpdates setWebhook deleteWebhook getWebhookInfo
|
|
13
|
+
sendAudio sendDocument sendVoice getMe logOut close sendMessage
|
|
14
|
+
forwardMessage forwardMessages copyMessage copyMessages sendPhoto
|
|
15
|
+
sendLivePhoto sendVideo sendAnimation sendVideoNote sendWidget
|
|
16
|
+
sendRichMessage sendRichMessageDraft
|
|
17
|
+
sendSharedWidget sendPaidMedia sendMediaGroup sendLocation sendVenue
|
|
18
|
+
sendContact sendPoll sendChecklist sendDice sendMessageDraft
|
|
19
|
+
sendChatAction setMessageReaction getUserProfilePhotos getUserProfileAudios
|
|
20
|
+
setUserEmojiStatus banChatMember unbanChatMember restrictChatMember
|
|
21
|
+
promoteChatMember setChatAdministratorCustomTitle setChatMemberTag
|
|
22
|
+
banChatSenderChat unbanChatSenderChat setChatPermissions
|
|
23
|
+
exportChatInviteLink createChatInviteLink editChatInviteLink
|
|
24
|
+
createChatSubscriptionInviteLink editChatSubscriptionInviteLink
|
|
25
|
+
revokeChatInviteLink approveChatJoinRequest declineChatJoinRequest
|
|
26
|
+
answerChatJoinRequestQuery sendChatJoinRequestWebApp setChatPhoto
|
|
27
|
+
deleteChatPhoto setChatTitle setChatDescription pinChatMessage
|
|
28
|
+
unpinChatMessage unpinAllChatMessages leaveChat getChat
|
|
29
|
+
getChatAdministrators getChatMemberCount getChatMember
|
|
30
|
+
getChatShareThemeBackdrops getUserPersonalChatMessages setChatStickerSet
|
|
31
|
+
deleteChatStickerSet getForumTopicIconStickers createForumTopic
|
|
32
|
+
editForumTopic closeForumTopic reopenForumTopic deleteForumTopic
|
|
33
|
+
unpinAllForumTopicMessages editGeneralForumTopic closeGeneralForumTopic
|
|
34
|
+
reopenGeneralForumTopic hideGeneralForumTopic unhideGeneralForumTopic
|
|
35
|
+
unpinAllGeneralForumTopicMessages answerCallbackQuery answerWidgetQuery
|
|
36
|
+
answerGuestQuery getUserChatBoosts getBusinessConnection
|
|
37
|
+
getManagedBotToken replaceManagedBotToken getManagedBotAccessSettings
|
|
38
|
+
setManagedBotAccessSettings setMySettings setMyCommands deleteMyCommands
|
|
39
|
+
getMyCommands setMyName getMyName setMyDescription getMyDescription
|
|
40
|
+
setMyShortDescription getMyShortDescription setMyProfilePhoto
|
|
41
|
+
removeMyProfilePhoto setChatMenuButton getChatMenuButton
|
|
42
|
+
setMyDefaultAdministratorRights getMyDefaultAdministratorRights
|
|
43
|
+
getAvailableGifts sendGift giftPremiumSubscription verifyUser verifyChat
|
|
44
|
+
removeUserVerification removeChatVerification readBusinessMessage
|
|
45
|
+
deleteBusinessMessages setBusinessAccountName setBusinessAccountUsername
|
|
46
|
+
setBusinessAccountBio setBusinessAccountProfilePhoto
|
|
47
|
+
removeBusinessAccountProfilePhoto setBusinessAccountGiftSettings
|
|
48
|
+
getBusinessAccountStarBalance transferBusinessAccountStars
|
|
49
|
+
getBusinessAccountGifts getUserGifts getChatGifts getUniqueGift
|
|
50
|
+
convertGiftToStars upgradeGift transferGift deleteBusinessAccountGift
|
|
51
|
+
postStory repostStory editStory deleteStory answerWebAppQuery
|
|
52
|
+
savePreparedInlineMessage savePreparedKeyboardButton editMessageText
|
|
53
|
+
editMessageCaption editMessageMedia editMessageLiveLocation
|
|
54
|
+
stopMessageLiveLocation editMessageChecklist editMessageWidgetData
|
|
55
|
+
editSharedWidgetData editMessageReplyMarkup stopPoll
|
|
56
|
+
editEphemeralMessageText editEphemeralMessageMedia
|
|
57
|
+
editEphemeralMessageCaption editEphemeralMessageReplyMarkup
|
|
58
|
+
approveSuggestedPost declineSuggestedPost deleteMessage deleteMessages
|
|
59
|
+
deleteEphemeralMessage deleteMessageReaction deleteAllMessageReactions
|
|
60
|
+
sendSticker getStickerSet getCustomEmojiStickers uploadStickerFile
|
|
61
|
+
createNewStickerSet addStickerToSet setStickerPositionInSet
|
|
62
|
+
deleteStickerFromSet replaceStickerInSet setStickerEmojiList
|
|
63
|
+
setStickerKeywords setStickerMaskPosition setStickerSetTitle
|
|
64
|
+
setStickerSetThumbnail setCustomEmojiStickerSetThumbnail deleteStickerSet
|
|
65
|
+
answerInlineQuery sendInvoice createInvoiceLink answerShippingQuery
|
|
66
|
+
answerPreCheckoutQuery getMyStarBalance getStarTransactions
|
|
67
|
+
refundStarPayment editUserStarSubscription setPassportDataErrors
|
|
68
|
+
sendGame setGameScore getGameHighScores
|
|
69
|
+
].freeze
|
|
70
|
+
METHOD_ALIASES = {
|
|
71
|
+
"set_bot_commands" => "setMyCommands",
|
|
72
|
+
"get_bot_commands" => "getMyCommands",
|
|
73
|
+
"delete_bot_commands" => "deleteMyCommands",
|
|
74
|
+
"set_bot_name" => "setMyName",
|
|
75
|
+
"get_bot_name" => "getMyName",
|
|
76
|
+
"set_bot_info_description" => "setMyDescription",
|
|
77
|
+
"get_bot_info_description" => "getMyDescription",
|
|
78
|
+
"set_bot_info_short_description" => "setMyShortDescription",
|
|
79
|
+
"get_bot_info_short_description" => "getMyShortDescription",
|
|
80
|
+
"set_bot_default_privileges" => "setMyDefaultAdministratorRights",
|
|
81
|
+
"get_bot_default_privileges" => "getMyDefaultAdministratorRights",
|
|
82
|
+
"get_chat_members_count" => "getChatMemberCount",
|
|
83
|
+
"get_chat_photos" => "getUserProfilePhotos",
|
|
84
|
+
"get_chat_audios" => "getUserProfileAudios",
|
|
85
|
+
"set_emoji_status" => "setUserEmojiStatus",
|
|
86
|
+
"get_managed_bot_token" => "getManagedBotToken",
|
|
87
|
+
"replace_managed_bot_token" => "replaceManagedBotToken",
|
|
88
|
+
"ban_chat_member" => "banChatMember",
|
|
89
|
+
"unban_chat_member" => "unbanChatMember",
|
|
90
|
+
"restrict_chat_member" => "restrictChatMember",
|
|
91
|
+
"promote_chat_member" => "promoteChatMember",
|
|
92
|
+
"set_administrator_title" => "setChatAdministratorCustomTitle",
|
|
93
|
+
"set_chat_member_tag" => "setChatMemberTag",
|
|
94
|
+
"set_chat_menu_button" => "setChatMenuButton",
|
|
95
|
+
"get_chat_menu_button" => "getChatMenuButton",
|
|
96
|
+
"send_reaction" => "setMessageReaction",
|
|
97
|
+
"set_message_reaction" => "setMessageReaction",
|
|
98
|
+
"export_chat_invite_link" => "exportChatInviteLink",
|
|
99
|
+
"create_chat_invite_link" => "createChatInviteLink",
|
|
100
|
+
"edit_chat_invite_link" => "editChatInviteLink",
|
|
101
|
+
"revoke_chat_invite_link" => "revokeChatInviteLink",
|
|
102
|
+
"approve_chat_join_request" => "approveChatJoinRequest",
|
|
103
|
+
"decline_chat_join_request" => "declineChatJoinRequest",
|
|
104
|
+
"send_message_draft" => "sendMessageDraft",
|
|
105
|
+
"set_my_settings" => "setMySettings",
|
|
106
|
+
"send_rich_message" => "sendRichMessage",
|
|
107
|
+
"send_rich_message_draft" => "sendRichMessageDraft",
|
|
108
|
+
"send_live_photo" => "sendLivePhoto",
|
|
109
|
+
"send_widget" => "sendWidget",
|
|
110
|
+
"send_shared_widget" => "sendSharedWidget",
|
|
111
|
+
"answer_widget_query" => "answerWidgetQuery",
|
|
112
|
+
"get_unique_gift" => "getUniqueGift",
|
|
113
|
+
"delete_ephemeral_message" => "deleteEphemeralMessage",
|
|
114
|
+
"edit_ephemeral_message_text" => "editEphemeralMessageText",
|
|
115
|
+
"edit_ephemeral_message_media" => "editEphemeralMessageMedia",
|
|
116
|
+
"edit_ephemeral_message_caption" => "editEphemeralMessageCaption",
|
|
117
|
+
"edit_ephemeral_message_reply_markup" => "editEphemeralMessageReplyMarkup",
|
|
118
|
+
"send_paid_reaction" => "sendPaidReaction",
|
|
119
|
+
"get_user_chat_boosts" => "getUserChatBoosts",
|
|
120
|
+
"get_business_connection" => "getBusinessConnection",
|
|
121
|
+
"forward_media_group" => "forwardMessages",
|
|
122
|
+
"copy_media_group" => "copyMessages"
|
|
123
|
+
}.freeze
|
|
124
|
+
|
|
125
|
+
def initialize(token)
|
|
126
|
+
@token = token
|
|
127
|
+
@base = URI("https://api.telegram.org/bot#{token}/")
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def request(method, params = {})
|
|
131
|
+
payload = raw(method, params)
|
|
132
|
+
payload["result"]
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def raw(method, params = {})
|
|
136
|
+
uri = @base + method.to_s
|
|
137
|
+
response = Retry.call do
|
|
138
|
+
clean_params = params.each_with_object({}) do |(key, value), out|
|
|
139
|
+
next if value.nil?
|
|
140
|
+
|
|
141
|
+
out[key.to_s] = value
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
if clean_params.any? { |key, value| multipart_file_field?(key) && upload_value?(value) }
|
|
145
|
+
post_multipart(uri, clean_params)
|
|
146
|
+
else
|
|
147
|
+
post_json(uri, clean_params)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
parse_response(response)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def get_updates(offset = nil, timeout: 30, **opts)
|
|
154
|
+
params = { timeout: timeout }.merge(opts)
|
|
155
|
+
params[:offset] = offset if offset
|
|
156
|
+
request("getUpdates", params)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def get_file(file_id)
|
|
160
|
+
request("getFile", { file_id: file_id })
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def download_file(file_path, save_as)
|
|
164
|
+
url = URI("https://api.telegram.org/file/bot#{@token}/#{file_path}")
|
|
165
|
+
File.open(save_as, "wb") do |file|
|
|
166
|
+
file.write(Net::HTTP.get(url))
|
|
167
|
+
end
|
|
168
|
+
save_as
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def send_message(chat_id, text, **opts)
|
|
172
|
+
request("sendMessage", { chat_id: chat_id, text: text }.merge(opts))
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def forward_message(chat_id, from_chat_id, message_id, **opts)
|
|
176
|
+
request("forwardMessage", {
|
|
177
|
+
chat_id: chat_id,
|
|
178
|
+
from_chat_id: from_chat_id,
|
|
179
|
+
message_id: message_id
|
|
180
|
+
}.merge(opts))
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def forward_messages(chat_id, from_chat_id, message_ids, **opts)
|
|
184
|
+
request("forwardMessages", {
|
|
185
|
+
chat_id: chat_id,
|
|
186
|
+
from_chat_id: from_chat_id,
|
|
187
|
+
message_ids: message_ids
|
|
188
|
+
}.merge(opts))
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def forward_media_group(chat_id, from_chat_id, message_ids, **opts)
|
|
192
|
+
forward_messages(chat_id, from_chat_id, message_ids, **opts)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def copy_message(chat_id, from_chat_id, message_id, **opts)
|
|
196
|
+
request("copyMessage", {
|
|
197
|
+
chat_id: chat_id,
|
|
198
|
+
from_chat_id: from_chat_id,
|
|
199
|
+
message_id: message_id
|
|
200
|
+
}.merge(opts))
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def copy_messages(chat_id, from_chat_id, message_ids, **opts)
|
|
204
|
+
request("copyMessages", {
|
|
205
|
+
chat_id: chat_id,
|
|
206
|
+
from_chat_id: from_chat_id,
|
|
207
|
+
message_ids: message_ids
|
|
208
|
+
}.merge(opts))
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def copy_media_group(chat_id, from_chat_id, message_ids, **opts)
|
|
212
|
+
copy_messages(chat_id, from_chat_id, message_ids, **opts)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def send_photo(chat_id, photo, **opts)
|
|
216
|
+
request("sendPhoto", { chat_id: chat_id, photo: photo }.merge(opts))
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def send_audio(chat_id, audio, **opts)
|
|
220
|
+
request("sendAudio", { chat_id: chat_id, audio: audio }.merge(opts))
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def send_document(chat_id, document, **opts)
|
|
224
|
+
request("sendDocument", { chat_id: chat_id, document: document }.merge(opts))
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def send_video(chat_id, video, **opts)
|
|
228
|
+
request("sendVideo", { chat_id: chat_id, video: video }.merge(opts))
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def send_animation(chat_id, animation, **opts)
|
|
232
|
+
request("sendAnimation", { chat_id: chat_id, animation: animation }.merge(opts))
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def send_voice(chat_id, voice, **opts)
|
|
236
|
+
request("sendVoice", { chat_id: chat_id, voice: voice }.merge(opts))
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def send_video_note(chat_id, video_note, **opts)
|
|
240
|
+
request("sendVideoNote", { chat_id: chat_id, video_note: video_note }.merge(opts))
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def send_sticker(chat_id, sticker, **opts)
|
|
244
|
+
request("sendSticker", { chat_id: chat_id, sticker: sticker }.merge(opts))
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def send_cached_media(chat_id, file_id, **opts)
|
|
248
|
+
request("sendDocument", { chat_id: chat_id, document: file_id }.merge(opts))
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def send_screenshot_notification(chat_id, **opts)
|
|
252
|
+
request("sendScreenshotNotification", { chat_id: chat_id }.merge(opts))
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def send_paid_media(chat_id, star_count, media, **opts)
|
|
256
|
+
request("sendPaidMedia", {
|
|
257
|
+
chat_id: chat_id,
|
|
258
|
+
star_count: star_count,
|
|
259
|
+
media: media
|
|
260
|
+
}.merge(opts))
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def send_media_group(chat_id, media, **opts)
|
|
264
|
+
request("sendMediaGroup", { chat_id: chat_id, media: media }.merge(opts))
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def send_location(chat_id, latitude, longitude, **opts)
|
|
268
|
+
request("sendLocation", {
|
|
269
|
+
chat_id: chat_id,
|
|
270
|
+
latitude: latitude,
|
|
271
|
+
longitude: longitude
|
|
272
|
+
}.merge(opts))
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def send_venue(chat_id, latitude, longitude, title, address, **opts)
|
|
276
|
+
request("sendVenue", {
|
|
277
|
+
chat_id: chat_id,
|
|
278
|
+
latitude: latitude,
|
|
279
|
+
longitude: longitude,
|
|
280
|
+
title: title,
|
|
281
|
+
address: address
|
|
282
|
+
}.merge(opts))
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def send_contact(chat_id, phone_number, first_name, **opts)
|
|
286
|
+
request("sendContact", {
|
|
287
|
+
chat_id: chat_id,
|
|
288
|
+
phone_number: phone_number,
|
|
289
|
+
first_name: first_name
|
|
290
|
+
}.merge(opts))
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def send_poll(chat_id, question, options, **opts)
|
|
294
|
+
request("sendPoll", {
|
|
295
|
+
chat_id: chat_id,
|
|
296
|
+
question: question,
|
|
297
|
+
options: options
|
|
298
|
+
}.merge(opts))
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def send_checklist(chat_id, checklist, business_connection_id:, **opts)
|
|
302
|
+
request("sendChecklist", {
|
|
303
|
+
business_connection_id: business_connection_id,
|
|
304
|
+
chat_id: chat_id,
|
|
305
|
+
checklist: checklist
|
|
306
|
+
}.merge(opts))
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def get_web_app_link_url(**opts)
|
|
310
|
+
request("getWebAppLinkUrl", opts)
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def get_web_app_url(**opts)
|
|
314
|
+
request("getWebAppUrl", opts)
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def open_web_app(**opts)
|
|
318
|
+
request("openWebApp", opts)
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def send_dice(chat_id, **opts)
|
|
322
|
+
request("sendDice", { chat_id: chat_id }.merge(opts))
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
def send_message_draft(chat_id, draft_id, text, **opts)
|
|
326
|
+
request("sendMessageDraft", {
|
|
327
|
+
chat_id: chat_id,
|
|
328
|
+
draft_id: draft_id,
|
|
329
|
+
text: text
|
|
330
|
+
}.merge(opts))
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
def send_chat_action(chat_id, action, **opts)
|
|
334
|
+
request("sendChatAction", { chat_id: chat_id, action: action }.merge(opts))
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def set_message_reaction(chat_id, message_id, **opts)
|
|
338
|
+
request("setMessageReaction", {
|
|
339
|
+
chat_id: chat_id,
|
|
340
|
+
message_id: message_id
|
|
341
|
+
}.merge(opts))
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def send_reaction(chat_id, message_id, **opts)
|
|
345
|
+
set_message_reaction(chat_id, message_id, **opts)
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def answer_callback_query(callback_query_id, **opts)
|
|
349
|
+
request("answerCallbackQuery", {
|
|
350
|
+
callback_query_id: callback_query_id
|
|
351
|
+
}.merge(opts))
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def answer_callback(callback_query_id, **opts)
|
|
355
|
+
answer_callback_query(callback_query_id, **opts)
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
def edit_message_text(chat_id, message_id, text, **opts)
|
|
359
|
+
request("editMessageText", {
|
|
360
|
+
chat_id: chat_id,
|
|
361
|
+
message_id: message_id,
|
|
362
|
+
text: text
|
|
363
|
+
}.merge(opts))
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
def delete_message(chat_id, message_id)
|
|
367
|
+
request("deleteMessage", {
|
|
368
|
+
chat_id: chat_id,
|
|
369
|
+
message_id: message_id
|
|
370
|
+
})
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
def delete_messages(chat_id, message_ids, **opts)
|
|
374
|
+
request("deleteMessages", {
|
|
375
|
+
chat_id: chat_id,
|
|
376
|
+
message_ids: message_ids
|
|
377
|
+
}.merge(opts))
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
OFFICIAL_METHODS.each do |api_method|
|
|
381
|
+
snake_name = api_method
|
|
382
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
|
383
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
|
384
|
+
.downcase
|
|
385
|
+
|
|
386
|
+
define_method(snake_name) do |*args, **kwargs|
|
|
387
|
+
params = {}
|
|
388
|
+
if args.length == 1 && args.first.is_a?(Hash)
|
|
389
|
+
params.merge!(args.first)
|
|
390
|
+
elsif !args.empty?
|
|
391
|
+
raise ArgumentError, "#{snake_name} accepts a parameter hash or keyword arguments"
|
|
392
|
+
end
|
|
393
|
+
params.merge!(kwargs) unless kwargs.empty?
|
|
394
|
+
request(api_method, params)
|
|
395
|
+
end
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
def method_missing(name, *args, **kwargs, &block)
|
|
399
|
+
return super if block
|
|
400
|
+
|
|
401
|
+
params = {}
|
|
402
|
+
if args.length == 1 && args.first.is_a?(Hash)
|
|
403
|
+
params.merge!(args.first)
|
|
404
|
+
elsif !args.empty?
|
|
405
|
+
return super
|
|
406
|
+
end
|
|
407
|
+
params.merge!(kwargs) unless kwargs.empty?
|
|
408
|
+
|
|
409
|
+
request(self.class.camelize_api_method(name), params)
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
def respond_to_missing?(_name, _include_private = false)
|
|
413
|
+
true
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
def self.escape_markdown_v2(text)
|
|
417
|
+
text.to_s.gsub(/([_\*\[\]\(\)~`>#+\-=|{}.!\\])/, '\\\\\1')
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
def self.escape_html(text)
|
|
421
|
+
text.to_s
|
|
422
|
+
.gsub("&", "&")
|
|
423
|
+
.gsub("<", "<")
|
|
424
|
+
.gsub(">", ">")
|
|
425
|
+
.gsub('"', """)
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
def self.camelize_api_method(name)
|
|
429
|
+
alias_name = METHOD_ALIASES[name.to_s]
|
|
430
|
+
return alias_name if alias_name
|
|
431
|
+
|
|
432
|
+
parts = name.to_s.split("_")
|
|
433
|
+
return name.to_s if parts.empty?
|
|
434
|
+
|
|
435
|
+
parts.first + parts[1..].map(&:capitalize).join
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
private
|
|
439
|
+
|
|
440
|
+
def post_json(uri, params)
|
|
441
|
+
request = Net::HTTP::Post.new(uri)
|
|
442
|
+
request["Content-Type"] = "application/json"
|
|
443
|
+
request.body = JSON.generate(params)
|
|
444
|
+
Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
|
|
445
|
+
http.request(request)
|
|
446
|
+
end
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
def post_multipart(uri, params)
|
|
450
|
+
boundary = "----gruby#{object_id}#{Process.pid}"
|
|
451
|
+
body = StringIO.new
|
|
452
|
+
|
|
453
|
+
params.each do |key, value|
|
|
454
|
+
if multipart_file_field?(key) && upload_value?(value)
|
|
455
|
+
append_multipart_file(body, boundary, key, value)
|
|
456
|
+
elsif value.is_a?(Hash) || value.is_a?(Array)
|
|
457
|
+
append_multipart_field(body, boundary, key, JSON.generate(value))
|
|
458
|
+
else
|
|
459
|
+
append_multipart_field(body, boundary, key, value.to_s)
|
|
460
|
+
end
|
|
461
|
+
end
|
|
462
|
+
body.write("--#{boundary}--\r\n")
|
|
463
|
+
|
|
464
|
+
request = Net::HTTP::Post.new(uri)
|
|
465
|
+
request["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
|
|
466
|
+
request.body = body.string
|
|
467
|
+
Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
|
|
468
|
+
http.request(request)
|
|
469
|
+
end
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
def append_multipart_field(body, boundary, name, value)
|
|
473
|
+
body.write("--#{boundary}\r\n")
|
|
474
|
+
body.write("Content-Disposition: form-data; name=\"#{name}\"\r\n\r\n")
|
|
475
|
+
body.write(value)
|
|
476
|
+
body.write("\r\n")
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
def append_multipart_file(body, boundary, name, value)
|
|
480
|
+
io = value.respond_to?(:read) ? value : File.open(value, "rb")
|
|
481
|
+
filename = if value.respond_to?(:path)
|
|
482
|
+
File.basename(value.path)
|
|
483
|
+
elsif value.is_a?(String)
|
|
484
|
+
File.basename(value)
|
|
485
|
+
else
|
|
486
|
+
"upload"
|
|
487
|
+
end
|
|
488
|
+
body.write("--#{boundary}\r\n")
|
|
489
|
+
body.write("Content-Disposition: form-data; name=\"#{name}\"; filename=\"#{filename}\"\r\n")
|
|
490
|
+
body.write("Content-Type: application/octet-stream\r\n\r\n")
|
|
491
|
+
body.write(io.read)
|
|
492
|
+
body.write("\r\n")
|
|
493
|
+
io.close if value.is_a?(String)
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
def upload_value?(value)
|
|
497
|
+
value.is_a?(File) ||
|
|
498
|
+
value.is_a?(StringIO) ||
|
|
499
|
+
value.respond_to?(:read) ||
|
|
500
|
+
(value.is_a?(String) && File.file?(value))
|
|
501
|
+
rescue StandardError
|
|
502
|
+
false
|
|
503
|
+
end
|
|
504
|
+
|
|
505
|
+
def multipart_file_field?(name)
|
|
506
|
+
%w[
|
|
507
|
+
photo video animation audio voice video_note document sticker
|
|
508
|
+
thumb thumbnail certificate png_sticker tgs_sticker webp_sticker
|
|
509
|
+
profile_photo file
|
|
510
|
+
].include?(name.to_s)
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
def serialize_params(params)
|
|
514
|
+
params.each_with_object({}) do |(key, value), out|
|
|
515
|
+
next if value.nil?
|
|
516
|
+
|
|
517
|
+
out[key.to_s] = serialize_value(value)
|
|
518
|
+
end
|
|
519
|
+
end
|
|
520
|
+
|
|
521
|
+
def serialize_value(value)
|
|
522
|
+
case value
|
|
523
|
+
when Hash, Array
|
|
524
|
+
JSON.generate(value)
|
|
525
|
+
else
|
|
526
|
+
value
|
|
527
|
+
end
|
|
528
|
+
end
|
|
529
|
+
|
|
530
|
+
def parse_response(response)
|
|
531
|
+
payload = JSON.parse(response.body.to_s)
|
|
532
|
+
return payload if payload["ok"]
|
|
533
|
+
|
|
534
|
+
message = payload["description"] || "Unknown Telegram API error"
|
|
535
|
+
raise Error, message
|
|
536
|
+
rescue JSON::ParserError
|
|
537
|
+
raise Error, "Invalid Telegram API response (HTTP #{response.code})"
|
|
538
|
+
end
|
|
539
|
+
end
|
|
540
|
+
end
|