telegem 2.0.7 → 2.0.9

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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/Test-Projects/Movie-tracker-bot/Gemfile +2 -0
  3. data/Test-Projects/Movie-tracker-bot/bot.rb +62 -0
  4. data/Test-Projects/Movie-tracker-bot/handlers/.gitkeep +0 -0
  5. data/Test-Projects/Movie-tracker-bot/handlers/add_1_.rb +160 -0
  6. data/Test-Projects/Movie-tracker-bot/handlers/add_2_.rb +139 -0
  7. data/Test-Projects/Movie-tracker-bot/handlers/premium.rb +13 -0
  8. data/Test-Projects/Movie-tracker-bot/handlers/report.rb +31 -0
  9. data/Test-Projects/Movie-tracker-bot/handlers/search.rb +150 -0
  10. data/Test-Projects/Movie-tracker-bot/handlers/sponsor.rb +14 -0
  11. data/Test-Projects/Movie-tracker-bot/handlers/start.rb +48 -0
  12. data/Test-Projects/Movie-tracker-bot/handlers/watch.rb +210 -0
  13. data/Test-Projects/Test-submitted-by-marvel/.gitkeep +0 -0
  14. data/Test-Projects/Test-submitted-by-marvel/Marvel-bot.md +3 -0
  15. data/docs-src/.gitkeep +0 -0
  16. data/docs-src/Bot-registration_.PNG +0 -0
  17. data/docs-src/bot.md +295 -0
  18. data/docs-src/context|ctx|.md +531 -0
  19. data/docs-src/getting-started.md +328 -0
  20. data/docs-src/keyboard_inline.md +413 -0
  21. data/docs-src/scene.md +509 -0
  22. data/docs-src/understanding-ctx.md +581 -0
  23. data/lib/api/client.rb +17 -16
  24. data/lib/core/bot.rb +19 -25
  25. data/lib/telegem.rb +1 -1
  26. data/lib/webhook/server.rb +1 -1
  27. metadata +26 -15
  28. data/docs/Api.md +0 -211
  29. data/docs/Cookbook(copy_paste).md +0 -644
  30. data/docs/Getting_started.md +0 -348
  31. data/docs/How_to_use.md +0 -571
  32. data/docs/QuickStart.md +0 -258
  33. data/docs/Understanding_Scene.md +0 -434
  34. data/docs/Usage.md +0 -717
  35. data/docs/webhook_setup.md +0 -199
  36. /data/{docs → Test-Projects/Movie-tracker-bot}/.gitkeep +0 -0
data/docs-src/scene.md ADDED
@@ -0,0 +1,509 @@
1
+
2
+ # Scenes: Multi-Step Conversations
3
+
4
+ ## What Are Scenes?
5
+
6
+ Think of a scene as a **conversation flow** or **wizard**. It's like when you call customer service and they guide you through steps:
7
+
8
+ 1. "Press 1 for sales"
9
+ 2. "Enter your account number"
10
+ 3. "Describe your issue"
11
+ 4. "Confirm details"
12
+
13
+ Each step depends on the previous one. That's what scenes do for your bot.
14
+
15
+ ## Why Use Scenes?
16
+
17
+ Without scenes, handling multi-step conversations looks like this:
18
+ ```ruby
19
+ # ❌ Messy without scenes
20
+ if ctx.session[:step] == "ask_name"
21
+ # Handle name
22
+ elsif ctx.session[:step] == "ask_age"
23
+ # Handle age
24
+ elsif ctx.session[:step] == "ask_email"
25
+ # Handle email
26
+ # ... gets messy fast!
27
+ ```
28
+
29
+ With scenes, it's organized:
30
+
31
+ ```ruby
32
+ # ✅ Clean with scenes
33
+ scene.step(:ask_name) { ... }
34
+ scene.step(:ask_age) { ... }
35
+ scene.step(:ask_email) { ... }
36
+ ```
37
+
38
+ Real-World Example: Restaurant Order
39
+
40
+ Without scene: Chaotic back-and-forth
41
+ With scene: Guided experience:
42
+
43
+ 1. Welcome → Choose food type
44
+ 2. Choose size → Choose toppings
45
+ 3. Enter address → Confirm order
46
+ 4. Payment → Receipt
47
+
48
+ Creating Your First Scene
49
+
50
+ ```ruby
51
+ # Define a scene called :survey
52
+ bot.scene(:survey) do |scene|
53
+ # Step 1: Ask for name
54
+ scene.step(:ask_name) do |ctx|
55
+ ctx.reply("What's your name?")
56
+ # Move to next step automatically
57
+ end
58
+
59
+ # Step 2: Ask for age (runs after user responds)
60
+ scene.step(:ask_age) do |ctx|
61
+ # Previous response is in ctx.message.text
62
+ name = ctx.message.text
63
+ ctx.session[:name] = name
64
+
65
+ ctx.reply("Nice to meet you, #{name}! How old are you?")
66
+ end
67
+
68
+ # Step 3: Ask for favorite color
69
+ scene.step(:ask_color) do |ctx|
70
+ age = ctx.message.text
71
+ ctx.session[:age] = age
72
+
73
+ # Show inline keyboard for colors
74
+ keyboard = Telegem.inline do
75
+ row button "🔴 Red", callback_data: "color_red"
76
+ row button "🔵 Blue", callback_data: "color_blue"
77
+ row button "🟢 Green", callback_data: "color_green"
78
+ end
79
+
80
+ ctx.reply("Choose your favorite color:", reply_markup: keyboard)
81
+ end
82
+
83
+ # Step 4: Show results
84
+ scene.step(:show_results) do |ctx|
85
+ color = ctx.data.split("_").last # "red", "blue", or "green"
86
+ ctx.session[:color] = color
87
+
88
+ # Gather all answers
89
+ name = ctx.session[:name]
90
+ age = ctx.session[:age]
91
+
92
+ ctx.reply(<<~RESULTS)
93
+ 📝 Survey Complete!
94
+
95
+ Name: #{name}
96
+ Age: #{age}
97
+ Favorite Color: #{color.capitalize}
98
+
99
+ Thanks for participating! 🎉
100
+ RESULTS
101
+
102
+ # Exit the scene
103
+ ctx.leave_scene
104
+ end
105
+ end
106
+ ```
107
+
108
+ Starting a Scene
109
+
110
+ ```ruby
111
+ # Start the survey when user sends /survey
112
+ bot.command("survey") do |ctx|
113
+ ctx.enter_scene(:survey)
114
+ end
115
+ ```
116
+
117
+ How Scenes Work Under the Hood
118
+
119
+ 1. Enter scene: ctx.enter_scene(:survey) sets current scene
120
+ 2. First step runs: Scene executes :ask_name step
121
+ 3. User responds: Bot receives their name
122
+ 4. Auto-advance: Scene automatically moves to :ask_age
123
+ 5. Continue: Process repeats through all steps
124
+ 6. Exit: ctx.leave_scene ends the conversation
125
+
126
+ Scene Events
127
+
128
+ on_enter (When Scene Starts)
129
+
130
+ ```ruby
131
+ bot.scene(:order) do |scene|
132
+ scene.on_enter do |ctx|
133
+ ctx.reply("🍕 Welcome to PizzaBot!")
134
+ ctx.reply("Let's create your perfect pizza...")
135
+ end
136
+
137
+ # ... steps here
138
+ end
139
+ ```
140
+
141
+ on_leave (When Scene Ends)
142
+
143
+ ```ruby
144
+ bot.scene(:order) do |scene|
145
+ # ... steps here
146
+
147
+ scene.on_leave do |ctx|
148
+ ctx.reply("Thanks for ordering! 🎉")
149
+ ctx.reply("Your pizza will arrive in 30 minutes!")
150
+ end
151
+ end
152
+ ```
153
+
154
+ Managing Scene Flow
155
+
156
+ Moving Between Steps
157
+
158
+ ```ruby
159
+ bot.scene(:quiz) do |scene|
160
+ scene.step(:question1) do |ctx|
161
+ ctx.reply("What's 2 + 2?")
162
+
163
+ # You DON'T need to manually move to next step
164
+ # It happens automatically after user responds
165
+ end
166
+
167
+ scene.step(:question2) do |ctx|
168
+ # ctx.message.text contains answer to question1
169
+ answer1 = ctx.message.text
170
+
171
+ if answer1 == "4"
172
+ ctx.reply("✅ Correct! Next question...")
173
+ ctx.session[:score] += 1
174
+ else
175
+ ctx.reply("❌ Wrong. The answer is 4. Next question...")
176
+ end
177
+
178
+ ctx.reply("What's the capital of France?")
179
+ end
180
+ end
181
+ ```
182
+
183
+ Conditional Steps
184
+
185
+ ```ruby
186
+ bot.scene(:registration) do |scene|
187
+ scene.step(:ask_if_adult) do |ctx|
188
+ keyboard = Telegem.inline do
189
+ row button "✅ Yes, I'm 18+", callback_data: "adult_yes"
190
+ row button "❌ No, I'm under 18", callback_data: "adult_no"
191
+ end
192
+
193
+ ctx.reply("Are you 18 or older?", reply_markup: keyboard)
194
+ end
195
+
196
+ scene.step(:adult_path) do |ctx|
197
+ # Only runs if user selected "Yes"
198
+ ctx.reply("Great! Let's continue with registration...")
199
+ # ... adult registration steps
200
+ end
201
+
202
+ scene.step(:minor_path) do |ctx|
203
+ # Only runs if user selected "No"
204
+ ctx.reply("Sorry, you must be 18 or older to register.")
205
+ ctx.leave_scene
206
+ end
207
+
208
+ # Route based on choice
209
+ scene.on_enter do |ctx|
210
+ # Set up routing
211
+ end
212
+ end
213
+ ```
214
+
215
+ Scene Persistence
216
+
217
+ Scenes remember where users are, even if they stop and come back later.
218
+
219
+ ```ruby
220
+ # User starts survey
221
+ # -> Step 1: Asks name
222
+ # User disappears for 3 hours
223
+ # User comes back, types anything
224
+ # -> Step 2: Asks age (remembers they're in survey!)
225
+
226
+ bot.on(:message) do |ctx|
227
+ if ctx.scene # User is in a scene
228
+ # Scene automatically continues from where they left off
229
+ # You don't need to handle this manually!
230
+ end
231
+ end
232
+ ```
233
+
234
+ Practical Examples
235
+
236
+ Example 1: Pizza Order Scene
237
+
238
+ ```ruby
239
+ bot.scene(:pizza_order) do |scene|
240
+ scene.on_enter do |ctx|
241
+ ctx.reply("Let's order a pizza! 🍕")
242
+ end
243
+
244
+ scene.step(:choose_size) do |ctx|
245
+ keyboard = Telegem.keyboard do
246
+ row "Small", "Medium", "Large"
247
+ end
248
+
249
+ ctx.reply("Choose pizza size:", reply_markup: keyboard)
250
+ end
251
+
252
+ scene.step(:choose_toppings) do |ctx|
253
+ ctx.session[:size] = ctx.message.text
254
+
255
+ keyboard = Telegem.inline do
256
+ row button "Cheese", callback_data: "topping_cheese"
257
+ row button "Pepperoni", callback_data: "topping_pepperoni"
258
+ row button "Veggie", callback_data: "topping_veggie"
259
+ end
260
+
261
+ ctx.reply("Choose toppings:", reply_markup: keyboard)
262
+ end
263
+
264
+ scene.step(:confirm) do |ctx|
265
+ ctx.session[:topping] = ctx.data.split("_").last
266
+
267
+ ctx.reply(<<~CONFIRM
268
+ ✅ Order Summary:
269
+
270
+ Size: #{ctx.session[:size]}
271
+ Topping: #{ctx.session[:topping]}
272
+
273
+ Type CONFIRM to place order or CANCEL to stop.
274
+ CONFIRM
275
+ )
276
+ end
277
+
278
+ scene.step(:finalize) do |ctx|
279
+ if ctx.message.text == "CONFIRM"
280
+ ctx.reply("🎉 Order placed! Arriving in 30 minutes.")
281
+ # Save order to database...
282
+ else
283
+ ctx.reply("Order cancelled.")
284
+ end
285
+
286
+ ctx.leave_scene
287
+ end
288
+
289
+ scene.on_leave do |ctx|
290
+ ctx.reply("Thanks for using PizzaBot! 🍕")
291
+ end
292
+ end
293
+ ```
294
+
295
+ Example 2: Support Ticket Scene
296
+
297
+ ```ruby
298
+ bot.scene(:support_ticket) do |scene|
299
+ scene.step(:describe_issue) do |ctx|
300
+ ctx.reply("Please describe your issue:")
301
+ end
302
+
303
+ scene.step(:ask_priority) do |ctx|
304
+ ctx.session[:issue] = ctx.message.text
305
+
306
+ keyboard = Telegem.inline do
307
+ row button "🚨 Urgent", callback_data: "priority_high"
308
+ row button "⚠️ Medium", callback_data: "priority_medium"
309
+ row button "✅ Low", callback_data: "priority_low"
310
+ end
311
+
312
+ ctx.reply("How urgent is this?", reply_markup: keyboard)
313
+ end
314
+
315
+ scene.step(:ask_contact) do |ctx|
316
+ ctx.session[:priority] = ctx.data.split("_").last
317
+
318
+ keyboard = Telegem.keyboard do
319
+ request_contact("📞 Share Phone Number")
320
+ end
321
+
322
+ ctx.reply("Please share your contact:", reply_markup: keyboard)
323
+ end
324
+
325
+ scene.step(:finish) do |ctx|
326
+ ctx.session[:contact] = ctx.message.contact.phone_number
327
+
328
+ # Create ticket in database
329
+ ticket_id = create_ticket(
330
+ issue: ctx.session[:issue],
331
+ priority: ctx.session[:priority],
332
+ contact: ctx.session[:contact]
333
+ )
334
+
335
+ ctx.reply("Ticket ##{ticket_id} created! We'll contact you soon.")
336
+ ctx.leave_scene
337
+ end
338
+ end
339
+ ```
340
+
341
+ Best Practices
342
+
343
+ 1. Keep Steps Focused
344
+
345
+ ```ruby
346
+ # ❌ Don't do too much in one step
347
+ scene.step(:collect_all_info) do |ctx|
348
+ ctx.reply("What's your name, age, email, and address?")
349
+ # User will get confused!
350
+ end
351
+
352
+ # ✅ Split into focused steps
353
+ scene.step(:ask_name) { ctx.reply("Name?") }
354
+ scene.step(:ask_age) { ctx.reply("Age?") }
355
+ scene.step(:ask_email) { ctx.reply("Email?") }
356
+ ```
357
+
358
+ 2. Clear Exit Points
359
+
360
+ ```ruby
361
+ bot.scene(:shopping) do |scene|
362
+ # Allow exit at any point
363
+ scene.step(:browse) do |ctx|
364
+ if ctx.message.text == "/cancel"
365
+ ctx.reply("Shopping cancelled.")
366
+ ctx.leave_scene
367
+ return
368
+ end
369
+ # ... continue shopping
370
+ end
371
+ end
372
+ ```
373
+
374
+ 3. Use Session Wisely
375
+
376
+ ```ruby
377
+ bot.scene(:form) do |scene|
378
+ scene.on_enter do |ctx|
379
+ # Initialize session
380
+ ctx.session[:form_data] = {}
381
+ end
382
+
383
+ scene.step(:step1) do |ctx|
384
+ # Store in session
385
+ ctx.session[:form_data][:name] = ctx.message.text
386
+ end
387
+
388
+ scene.on_leave do |ctx|
389
+ # Save complete form
390
+ save_form(ctx.session[:form_data])
391
+ # Clear session
392
+ ctx.session.delete(:form_data)
393
+ end
394
+ end
395
+ ```
396
+
397
+ Common Patterns
398
+
399
+ Pattern 1: Branching Scenes
400
+
401
+ ```ruby
402
+ # Main menu scene
403
+ bot.scene(:main_menu) do |scene|
404
+ scene.step(:show_options) do |ctx|
405
+ keyboard = Telegem.keyboard do
406
+ row "Order Food", "Book Table"
407
+ row "Contact Support", "Leave Feedback"
408
+ end
409
+ ctx.reply("Main Menu:", reply_markup: keyboard)
410
+ end
411
+
412
+ scene.step(:handle_choice) do |ctx|
413
+ case ctx.message.text
414
+ when "Order Food"
415
+ ctx.leave_scene
416
+ ctx.enter_scene(:food_order)
417
+ when "Book Table"
418
+ ctx.leave_scene
419
+ ctx.enter_scene(:table_booking)
420
+ # ... other choices
421
+ end
422
+ end
423
+ end
424
+ ```
425
+
426
+ Pattern 2: Timeout Handling
427
+
428
+ ```ruby
429
+ bot.scene(:quick_quiz) do |scene|
430
+ scene.on_enter do |ctx|
431
+ # Set timeout (user has 60 seconds to complete)
432
+ ctx.session[:quiz_start] = Time.now
433
+ end
434
+
435
+ # In each step
436
+ scene.step(:question) do |ctx|
437
+ if Time.now - ctx.session[:quiz_start] > 60
438
+ ctx.reply("⏰ Time's up! Quiz expired.")
439
+ ctx.leave_scene
440
+ return
441
+ end
442
+ # Continue quiz
443
+ end
444
+ end
445
+ ```
446
+
447
+ Troubleshooting
448
+
449
+ Scene Not Starting?
450
+
451
+ ```ruby
452
+ # Make sure you're entering the scene correctly
453
+ bot.command("start_quiz") do |ctx|
454
+ ctx.enter_scene(:quiz) # ✅ Correct
455
+ # NOT: bot.scene(:quiz) ❌
456
+ end
457
+ ```
458
+
459
+ Scene Stuck?
460
+
461
+ ```ruby
462
+ # Check if user is in a scene
463
+ bot.command("status") do |ctx|
464
+ if ctx.scene
465
+ ctx.reply("You're in scene: #{ctx.scene}")
466
+ else
467
+ ctx.reply("No active scene")
468
+ end
469
+ end
470
+
471
+ # Force leave scene
472
+ bot.command("cancel") do |ctx|
473
+ ctx.leave_scene
474
+ ctx.reply("Scene cancelled.")
475
+ end
476
+ ```
477
+
478
+ When to Use Scenes
479
+
480
+ ✅ Perfect for:
481
+
482
+ · Multi-step forms (registration, surveys)
483
+ · Order flows (e-commerce, food)
484
+ · Onboarding processes
485
+ · Complex configuration
486
+ · Interactive tutorials
487
+
488
+ ❌ Not needed for:
489
+
490
+ · Simple commands (/start, /help)
491
+ · One-time responses
492
+ · Broadcast messages
493
+ · Simple Q&A
494
+
495
+ Summary
496
+
497
+ Scenes turn chaotic conversations into guided experiences. They:
498
+
499
+ 1. Organize multi-step flows
500
+ 2. Remember where users left off
501
+ 3. Automate step progression
502
+ 4. Clean up after completion
503
+
504
+ Start with simple 2-3 step scenes, then build more complex ones as you get comfortable!
505
+
506
+ Next: Learn about Session Management to store user data between conversations.
507
+
508
+ ```
509
+ ```