telegem 0.2.5 → 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.
- checksums.yaml +4 -4
- data/.replit +13 -0
- data/Contributing.md +553 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +11 -0
- data/LICENSE +21 -0
- data/Readme.md +353 -0
- data/Test-Projects/.gitkeep +0 -0
- data/Test-Projects/bot_test1.rb +75 -0
- data/Test-Projects/pizza_test_bot_guide.md +163 -0
- data/docs/.gitkeep +0 -0
- data/docs/Api.md +419 -0
- data/docs/Cookbook.md +407 -0
- data/docs/How_to_use.md +571 -0
- data/docs/QuickStart.md +258 -0
- data/docs/Usage.md +717 -0
- data/lib/api/client.rb +89 -116
- data/lib/core/bot.rb +103 -92
- data/lib/core/composer.rb +36 -18
- data/lib/core/context.rb +180 -177
- data/lib/core/scene.rb +81 -71
- data/lib/session/memory_store.rb +1 -1
- data/lib/session/middleware.rb +20 -36
- data/lib/telegem.rb +57 -54
- data/lib/webhook/.gitkeep +0 -0
- data/lib/webhook/server.rb +193 -0
- metadata +38 -35
- data/telegem.gemspec +0 -43
- data/webhook/server.rb +0 -86
data/docs/Api.md
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
|
|
2
|
+
# 📚 API Reference
|
|
3
|
+
|
|
4
|
+
Quick reference for all Telegem classes and methods. Use this when you know what you need and want the exact syntax.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 📦 Main Classes
|
|
9
|
+
|
|
10
|
+
### `Telegem::Bot` (Main Class)
|
|
11
|
+
```ruby
|
|
12
|
+
# Create a bot
|
|
13
|
+
bot = Telegem.new(token)
|
|
14
|
+
# or
|
|
15
|
+
bot = Telegem::Core::Bot.new(token, **options)
|
|
16
|
+
|
|
17
|
+
# Options:
|
|
18
|
+
# - logger: Custom logger (default: Logger.new($stdout))
|
|
19
|
+
# - concurrency: Max parallel updates (default: 10)
|
|
20
|
+
# - session_store: :memory, :redis, or custom object
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Context (ctx in handlers)
|
|
24
|
+
|
|
25
|
+
The object you get in every handler. Controls the conversation.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
🎮 Context Methods
|
|
30
|
+
|
|
31
|
+
Sending Messages
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
ctx.reply(text, **options) # Send message
|
|
35
|
+
ctx.edit_message_text(text, **options) # Edit last message
|
|
36
|
+
ctx.delete_message(message_id = nil) # Delete message
|
|
37
|
+
ctx.forward_message(from_chat_id, message_id) # Forward message
|
|
38
|
+
ctx.copy_message(from_chat_id, message_id) # Copy message
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Sending Media
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
ctx.photo(photo, caption: nil, **options) # Send photo
|
|
45
|
+
ctx.document(doc, caption: nil, **options) # Send file
|
|
46
|
+
ctx.audio(audio, caption: nil, **options) # Send audio
|
|
47
|
+
ctx.video(video, caption: nil, **options) # Send video
|
|
48
|
+
ctx.voice(voice, caption: nil, **options) # Send voice
|
|
49
|
+
ctx.sticker(sticker, **options) # Send sticker
|
|
50
|
+
ctx.location(lat, long, **options) # Send location
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Chat Actions
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
ctx.send_chat_action(action, **options) # Show typing/uploading
|
|
57
|
+
ctx.typing(**options) # Shortcut for 'typing'
|
|
58
|
+
ctx.uploading_photo(**options) # Shortcut for 'upload_photo'
|
|
59
|
+
ctx.with_typing { block } # Show typing while block runs
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Callback & Inline Queries
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
ctx.answer_callback_query(text: nil, show_alert: false)
|
|
66
|
+
ctx.answer_inline_query(results, **options)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Chat Management
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
ctx.get_chat(**options)
|
|
73
|
+
ctx.get_chat_administrators(**options)
|
|
74
|
+
ctx.get_chat_members_count(**options)
|
|
75
|
+
ctx.ban_chat_member(user_id, **options)
|
|
76
|
+
ctx.unban_chat_member(user_id, **options)
|
|
77
|
+
ctx.kick_chat_member(user_id, **options)
|
|
78
|
+
ctx.pin_message(message_id, **options)
|
|
79
|
+
ctx.unpin_message(**options)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Keyboard Helpers
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
ctx.keyboard { block } # Create reply keyboard
|
|
86
|
+
ctx.inline_keyboard { block } # Create inline keyboard
|
|
87
|
+
ctx.reply_with_keyboard(text, keyboard, **options)
|
|
88
|
+
ctx.reply_with_inline_keyboard(text, inline, **options)
|
|
89
|
+
ctx.remove_keyboard(text = nil, **options)
|
|
90
|
+
ctx.edit_message_reply_markup(reply_markup, **options)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Scene Management
|
|
94
|
+
|
|
95
|
+
```ruby
|
|
96
|
+
ctx.enter_scene(scene_name, **options)
|
|
97
|
+
ctx.leave_scene(**options)
|
|
98
|
+
ctx.current_scene # Returns current scene object
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Information Getters
|
|
102
|
+
|
|
103
|
+
```ruby
|
|
104
|
+
ctx.message # Message object
|
|
105
|
+
ctx.callback_query # CallbackQuery object
|
|
106
|
+
ctx.inline_query # InlineQuery object
|
|
107
|
+
ctx.from # User who sent
|
|
108
|
+
ctx.chat # Chat object
|
|
109
|
+
ctx.data # Callback data (for buttons)
|
|
110
|
+
ctx.query # Inline query text
|
|
111
|
+
ctx.user_id # Shortcut for from.id
|
|
112
|
+
ctx.command? # true if message is command
|
|
113
|
+
ctx.command_args # Arguments after command
|
|
114
|
+
ctx.raw_update # Raw Telegram JSON
|
|
115
|
+
ctx.api # Direct API client access
|
|
116
|
+
ctx.logger # Bot's logger
|
|
117
|
+
ctx.session # User's session hash
|
|
118
|
+
ctx.state # Temporary state hash
|
|
119
|
+
ctx.match # Regex match data
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
⌨️ Keyboard Builders
|
|
125
|
+
|
|
126
|
+
Reply Keyboard
|
|
127
|
+
|
|
128
|
+
```ruby
|
|
129
|
+
keyboard = Telegem::Markup.keyboard do
|
|
130
|
+
row "Button 1", "Button 2"
|
|
131
|
+
row "Cancel"
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Options can be chained:
|
|
135
|
+
keyboard.resize(false).one_time.selective(true)
|
|
136
|
+
|
|
137
|
+
# Convert to hash for Telegram:
|
|
138
|
+
keyboard.to_h
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Inline Keyboard
|
|
142
|
+
|
|
143
|
+
```ruby
|
|
144
|
+
inline = Telegem::Markup.inline do
|
|
145
|
+
row callback("Option 1", "data1"),
|
|
146
|
+
callback("Option 2", "data2")
|
|
147
|
+
row url("Website", "https://example.com"),
|
|
148
|
+
web_app("Open App", "https://app.example.com")
|
|
149
|
+
row switch_inline("Search", "query"),
|
|
150
|
+
switch_inline_current("Search Here", "")
|
|
151
|
+
end
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Special Markups
|
|
155
|
+
|
|
156
|
+
```ruby
|
|
157
|
+
Telegem::Markup.remove(selective: false) # Remove keyboard
|
|
158
|
+
Telegem::Markup.force_reply(selective: false) # Force reply
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
🧙 Scenes (Wizard System)
|
|
164
|
+
|
|
165
|
+
Defining a Scene
|
|
166
|
+
|
|
167
|
+
```ruby
|
|
168
|
+
bot.scene :registration do
|
|
169
|
+
step :ask_name do |ctx|
|
|
170
|
+
ctx.reply "What's your name?"
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
step :save_name do |ctx|
|
|
174
|
+
ctx.session[:name] = ctx.message.text
|
|
175
|
+
ctx.reply "Thanks #{ctx.session[:name]}!"
|
|
176
|
+
ctx.leave_scene
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
on_enter do |ctx|
|
|
180
|
+
ctx.reply "Welcome to registration!"
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
on_leave do |ctx|
|
|
184
|
+
ctx.reply "Registration complete!"
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Scene Methods
|
|
190
|
+
|
|
191
|
+
```ruby
|
|
192
|
+
scene.enter(ctx, step_name = :default)
|
|
193
|
+
scene.leave(ctx)
|
|
194
|
+
scene.current_step(ctx)
|
|
195
|
+
scene.reset(ctx)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
🔌 Middleware
|
|
201
|
+
|
|
202
|
+
Using Middleware
|
|
203
|
+
|
|
204
|
+
```ruby
|
|
205
|
+
# Add built-in session middleware
|
|
206
|
+
bot.use Telegem::Session::Middleware.new
|
|
207
|
+
|
|
208
|
+
# Add custom middleware
|
|
209
|
+
bot.use do |ctx, next_middleware|
|
|
210
|
+
puts "Before: #{ctx.message&.text}"
|
|
211
|
+
next_middleware.call(ctx)
|
|
212
|
+
puts "After"
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# Add middleware class
|
|
216
|
+
class LoggingMiddleware
|
|
217
|
+
def call(ctx, next_middleware)
|
|
218
|
+
# Your code here
|
|
219
|
+
next_middleware.call(ctx)
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
bot.use LoggingMiddleware.new
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
💾 Session Stores
|
|
228
|
+
|
|
229
|
+
Available Stores
|
|
230
|
+
|
|
231
|
+
```ruby
|
|
232
|
+
# Memory store (default)
|
|
233
|
+
Telegem::Session::MemoryStore.new
|
|
234
|
+
|
|
235
|
+
# Redis store
|
|
236
|
+
require 'telegem/session/redis_store'
|
|
237
|
+
Telegem::Session::RedisStore.new(redis_client)
|
|
238
|
+
|
|
239
|
+
# File store
|
|
240
|
+
require 'telegem/session/file_store'
|
|
241
|
+
Telegem::Session::FileStore.new("sessions.json")
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Session Middleware
|
|
245
|
+
|
|
246
|
+
```ruby
|
|
247
|
+
bot.use Telegem::Session::Middleware.new(
|
|
248
|
+
store: store_object, # defaults to MemoryStore
|
|
249
|
+
key_prefix: "telegem:" # optional prefix for storage keys
|
|
250
|
+
)
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
🌐 Webhook Server
|
|
256
|
+
|
|
257
|
+
Creating a Server
|
|
258
|
+
|
|
259
|
+
```ruby
|
|
260
|
+
server = bot.webhook_server(
|
|
261
|
+
port: 3000, # default: 3000
|
|
262
|
+
endpoint: endpoint_object, # Async::HTTP::Endpoint
|
|
263
|
+
logger: logger_object # custom logger
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
server.run # Start async server
|
|
267
|
+
server.stop # Stop server
|
|
268
|
+
server.running? # Check if running
|
|
269
|
+
server.webhook_url # Get full webhook URL
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
⚡ Bot Methods
|
|
275
|
+
|
|
276
|
+
Handler Registration
|
|
277
|
+
|
|
278
|
+
```ruby
|
|
279
|
+
bot.command(name, **options) { |ctx| } # /command handler
|
|
280
|
+
bot.hears(pattern, **options) { |ctx| } # Text pattern handler
|
|
281
|
+
bot.on(type, filters = {}) { |ctx| } # Generic handler
|
|
282
|
+
|
|
283
|
+
# Handler types:
|
|
284
|
+
# - :message
|
|
285
|
+
# - :callback_query
|
|
286
|
+
# - :inline_query
|
|
287
|
+
# - :chat_member
|
|
288
|
+
# - :poll
|
|
289
|
+
# - :pre_checkout_query
|
|
290
|
+
# - :shipping_query
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
Bot Control
|
|
294
|
+
|
|
295
|
+
```ruby
|
|
296
|
+
bot.start_polling(**options) # Start polling updates
|
|
297
|
+
bot.webhook_server(**options) # Create webhook server
|
|
298
|
+
bot.set_webhook(url, **options) # Set webhook URL
|
|
299
|
+
bot.delete_webhook # Remove webhook
|
|
300
|
+
bot.get_webhook_info # Get webhook status
|
|
301
|
+
bot.shutdown # Graceful shutdown
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Error Handling
|
|
305
|
+
|
|
306
|
+
```ruby
|
|
307
|
+
# Global error handler
|
|
308
|
+
bot.error do |error, ctx|
|
|
309
|
+
ctx.reply "Oops: #{error.message}"
|
|
310
|
+
end
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Scene Management
|
|
314
|
+
|
|
315
|
+
```ruby
|
|
316
|
+
bot.scene(name, &block) # Define a scene
|
|
317
|
+
bot.scenes # Hash of all scenes
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
---
|
|
321
|
+
|
|
322
|
+
📞 Telegram API Methods
|
|
323
|
+
|
|
324
|
+
All methods below are available via ctx.api.call(method, params) or bot.api.call(method, params).
|
|
325
|
+
|
|
326
|
+
Message Methods
|
|
327
|
+
|
|
328
|
+
· sendMessage, sendPhoto, sendAudio, sendDocument
|
|
329
|
+
· sendVideo, sendVoice, sendSticker, sendLocation
|
|
330
|
+
· sendContact, sendPoll, sendDice, sendChatAction
|
|
331
|
+
· sendMediaGroup, forwardMessage, copyMessage
|
|
332
|
+
· editMessageText, editMessageCaption, editMessageMedia
|
|
333
|
+
· editMessageReplyMarkup, deleteMessage, pinChatMessage
|
|
334
|
+
· unpinChatMessage
|
|
335
|
+
|
|
336
|
+
Chat Methods
|
|
337
|
+
|
|
338
|
+
· getChat, getChatAdministrators, getChatMember
|
|
339
|
+
· getChatMembersCount, banChatMember, unbanChatMember
|
|
340
|
+
· restrictChatMember, promoteChatMember, setChatPermissions
|
|
341
|
+
· exportChatInviteLink, createChatInviteLink, revokeChatInviteLink
|
|
342
|
+
· approveChatJoinRequest, declineChatJoinRequest, setChatPhoto
|
|
343
|
+
· deleteChatPhoto, setChatTitle, setChatDescription
|
|
344
|
+
· setChatAdministratorCustomTitle, leaveChat
|
|
345
|
+
|
|
346
|
+
Callback & Inline Methods
|
|
347
|
+
|
|
348
|
+
· answerCallbackQuery, answerInlineQuery, answerWebAppQuery
|
|
349
|
+
|
|
350
|
+
Webhook Methods
|
|
351
|
+
|
|
352
|
+
· setWebhook, deleteWebhook, getWebhookInfo
|
|
353
|
+
|
|
354
|
+
Update Methods
|
|
355
|
+
|
|
356
|
+
· getUpdates
|
|
357
|
+
|
|
358
|
+
User & Bot Methods
|
|
359
|
+
|
|
360
|
+
· getMe, logOut, close
|
|
361
|
+
|
|
362
|
+
---
|
|
363
|
+
|
|
364
|
+
🎯 Common Options
|
|
365
|
+
|
|
366
|
+
Message Options
|
|
367
|
+
|
|
368
|
+
```ruby
|
|
369
|
+
{
|
|
370
|
+
parse_mode: "HTML" | "Markdown" | "MarkdownV2",
|
|
371
|
+
disable_web_page_preview: true,
|
|
372
|
+
disable_notification: true,
|
|
373
|
+
protect_content: true,
|
|
374
|
+
reply_to_message_id: 123,
|
|
375
|
+
allow_sending_without_reply: true,
|
|
376
|
+
reply_markup: keyboard_object
|
|
377
|
+
}
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
Media Options
|
|
381
|
+
|
|
382
|
+
```ruby
|
|
383
|
+
{
|
|
384
|
+
caption: "Description",
|
|
385
|
+
parse_mode: "HTML",
|
|
386
|
+
duration: 60,
|
|
387
|
+
width: 1920,
|
|
388
|
+
height: 1080,
|
|
389
|
+
thumb: input_file,
|
|
390
|
+
supports_streaming: true
|
|
391
|
+
}
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
Chat Member Options
|
|
395
|
+
|
|
396
|
+
```ruby
|
|
397
|
+
{
|
|
398
|
+
until_date: Time.now + 3600, # Unix timestamp
|
|
399
|
+
revoke_messages: true
|
|
400
|
+
}
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
---
|
|
404
|
+
|
|
405
|
+
❗ Error Types
|
|
406
|
+
|
|
407
|
+
```ruby
|
|
408
|
+
Telegem::Error # Base error
|
|
409
|
+
Telegem::APIError # Telegram API errors
|
|
410
|
+
Telegem::NetworkError # Network issues
|
|
411
|
+
Telegem::ValidationError # Invalid parameters
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
---
|
|
415
|
+
|
|
416
|
+
Last updated for Telegem v0.1.0
|
|
417
|
+
|
|
418
|
+
```
|
|
419
|
+
---
|