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.
@@ -0,0 +1,571 @@
1
+ ๐ŸŽ“ How to Use Telegem: Your Friendly Guide
2
+
3
+ Welcome! This guide will walk you through everything Telegem can do, step by step. No prior bot experience needed!
4
+
5
+ ๐Ÿ“š How This Guide Works
6
+
7
+ We'll learn by doing! Each section has:
8
+
9
+ ยท ๐Ÿงช Try This - Hands-on examples in IRB (Ruby's interactive console)
10
+ ยท ๐Ÿ’ก What's Happening - Plain English explanations
11
+ ยท ๐Ÿ”ง Real Example - Practical bot code you can use
12
+ ยท ๐Ÿ“ Key Points - Important things to remember
13
+
14
+ Ready? Let's begin our bot-building journey! ๐Ÿš€
15
+
16
+ ---
17
+
18
+ ๐Ÿ Part 1: Your First Bot (5 Minutes)
19
+
20
+ ๐Ÿงช Try This First:
21
+
22
+ Open your terminal and type:
23
+
24
+ ```bash
25
+ irb
26
+ ```
27
+
28
+ Then type (or copy-paste):
29
+
30
+ ```ruby
31
+ require 'telegem'
32
+ puts "โœ… Telegem loaded! Version: #{Telegem::VERSION}"
33
+ ```
34
+
35
+ You should see: โœ… Telegem loaded! Version: 0.1.2
36
+
37
+ ๐Ÿ’ก What's Happening:
38
+
39
+ You just loaded the Telegem library! This is like opening a toolbox before building something. The library is now ready to use in your Ruby session.
40
+
41
+ ๐Ÿ”ง Real Example:
42
+
43
+ Let's create the simplest possible bot:
44
+
45
+ ```ruby
46
+ # simplest_bot.rb
47
+ require 'telegem'
48
+
49
+ # Create your bot (replace with real token)
50
+ bot = Telegem.new('YOUR_TOKEN_HERE')
51
+
52
+ # Add a command
53
+ bot.command('start') do |ctx|
54
+ ctx.reply "Hello! I'm alive! ๐ŸŽ‰"
55
+ end
56
+
57
+ # Start listening for messages
58
+ bot.start_polling
59
+ ```
60
+
61
+ ๐Ÿ“ Key Points:
62
+
63
+ 1. Every bot needs a token (from @BotFather on Telegram)
64
+ 2. Commands start with / like /start, /help
65
+ 3. ctx means "context" - it's your control panel for that chat
66
+ 4. ctx.reply sends a message back
67
+
68
+ ---
69
+
70
+ ๐Ÿงฉ Part 2: Understanding the "Context" (ctx)
71
+
72
+ Think of ctx as your bot's hands and eyes in a conversation. It can:
73
+
74
+ ยท See the message
75
+ ยท Know who sent it
76
+ ยท Send replies
77
+ ยท Remember things
78
+
79
+ ๐Ÿงช Try This in IRB:
80
+
81
+ ```ruby
82
+ # Let's explore what ctx can do
83
+ class MockContext
84
+ def from
85
+ OpenStruct.new(first_name: "Alex", id: 12345)
86
+ end
87
+
88
+ def chat
89
+ OpenStruct.new(id: 999, type: "private")
90
+ end
91
+
92
+ def message
93
+ OpenStruct.new(text: "/start", command?: true)
94
+ end
95
+ end
96
+
97
+ ctx = MockContext.new
98
+ puts "User: #{ctx.from.first_name}"
99
+ puts "Chat ID: #{ctx.chat.id}"
100
+ puts "Is command? #{ctx.message.command?}"
101
+ ```
102
+
103
+ ๐Ÿ’ก What ctx Contains:
104
+
105
+ ยท ctx.from - Who sent the message (name, ID, username)
106
+ ยท ctx.chat - Where it was sent (private chat, group, channel)
107
+ ยท ctx.message - The actual message text
108
+ ยท ctx.reply - Method to send messages back
109
+ ยท ctx.session - Memory for this user (more on this later!)
110
+
111
+ ๐Ÿ”ง Real Example - Personal Greeting:
112
+
113
+ ```ruby
114
+ bot.command('hello') do |ctx|
115
+ # Use ctx.from to personalize
116
+ name = ctx.from.first_name
117
+ ctx.reply "Hi #{name}! ๐Ÿ‘‹ Nice to meet you!"
118
+
119
+ # Use ctx.chat to know where we are
120
+ if ctx.chat.type == "group"
121
+ ctx.reply "Thanks for adding me to this group!"
122
+ end
123
+ end
124
+ ```
125
+
126
+ ---
127
+
128
+ ๐Ÿ—ฃ๏ธ Part 3: Handling Different Message Types
129
+
130
+ Bots can handle more than just commands! Here's what you can listen for:
131
+
132
+ 1. Commands (Start with /)
133
+
134
+ ```ruby
135
+ bot.command('help') do |ctx|
136
+ ctx.reply "I'm here to help!"
137
+ end
138
+ ```
139
+
140
+ 2. Text Messages (Any text)
141
+
142
+ ```ruby
143
+ bot.on(:message) do |ctx|
144
+ # Skip commands (they're handled above)
145
+ next if ctx.message.command?
146
+
147
+ user_text = ctx.message.text
148
+ ctx.reply "You said: #{user_text}"
149
+ end
150
+ ```
151
+
152
+ 3. Button Clicks (Inline keyboard buttons)
153
+
154
+ ```ruby
155
+ bot.on(:callback_query) do |ctx|
156
+ # ctx.data contains what button was clicked
157
+ button_data = ctx.data
158
+ ctx.reply "You clicked: #{button_data}"
159
+ end
160
+ ```
161
+
162
+ ๐Ÿงช Try This Pattern:
163
+
164
+ ```ruby
165
+ bot.on(:message) do |ctx|
166
+ if ctx.message.command?
167
+ puts "It's a command!"
168
+ elsif ctx.message.text.include?("?")
169
+ puts "It's a question!"
170
+ else
171
+ puts "Regular message"
172
+ end
173
+ end
174
+ ```
175
+
176
+ ---
177
+
178
+ ๐ŸŽจ Part 4: Making Interactive Bots (Keyboards!)
179
+
180
+ Why Keyboards?
181
+
182
+ Instead of typing commands, users can click buttons! Two types:
183
+
184
+ 1. Reply Keyboard (Appears at bottom)
185
+
186
+ ```ruby
187
+ keyboard = Telegem::Markup.keyboard do
188
+ row "Yes", "No", "Maybe"
189
+ row "Cancel"
190
+ end
191
+
192
+ ctx.reply "Do you agree?", reply_markup: keyboard
193
+ ```
194
+
195
+ 2. Inline Keyboard (Appears in message)
196
+
197
+ ```ruby
198
+ inline = Telegem::Markup.inline do
199
+ row callback("๐Ÿ‘ Like", "like_123"),
200
+ callback("๐Ÿ‘Ž Dislike", "dislike_123")
201
+ row url("๐ŸŒ Website", "https://example.com")
202
+ end
203
+
204
+ ctx.reply "Rate this:", reply_markup: inline
205
+ ```
206
+
207
+ ๐Ÿงช Try Building a Menu:
208
+
209
+ ```ruby
210
+ # In IRB, try building this menu
211
+ require 'telegem'
212
+
213
+ menu = Telegem::Markup.keyboard do
214
+ row "๐Ÿ• Pizza", "๐Ÿ” Burger", "๐ŸŒฎ Taco"
215
+ row "๐Ÿฅค Drinks", "๐Ÿฐ Dessert"
216
+ row "๐Ÿ“ž Call Staff", "โŒ Cancel"
217
+ end
218
+
219
+ puts "Your menu looks like:"
220
+ puts menu.to_h
221
+ ```
222
+
223
+ ---
224
+
225
+ ๐Ÿ’พ Part 5: Remembering Things (Sessions)
226
+
227
+ Bots have short-term memory! This is called "session."
228
+
229
+ The Magic of ctx.session:
230
+
231
+ ```ruby
232
+ bot.command('counter') do |ctx|
233
+ # Initialize if first time
234
+ ctx.session[:count] ||= 0
235
+
236
+ # Increase counter
237
+ ctx.session[:count] += 1
238
+
239
+ ctx.reply "You've used this command #{ctx.session[:count]} times!"
240
+ end
241
+ ```
242
+
243
+ What Can You Store?
244
+
245
+ ยท Preferences (language, theme)
246
+ ยท Shopping carts (items being purchased)
247
+ ยท Game scores (points, levels)
248
+ ยท Form data (half-filled applications)
249
+
250
+ ๐Ÿงช Try Session in IRB:
251
+
252
+ ```ruby
253
+ # Simulate how session works
254
+ session = {}
255
+
256
+ # First visit
257
+ session[:visits] ||= 0
258
+ session[:visits] += 1
259
+ puts "Visit #{session[:visits]}"
260
+
261
+ # Second "visit"
262
+ session[:visits] += 1
263
+ puts "Visit #{session[:visits]}"
264
+
265
+ # It remembers!
266
+ ```
267
+
268
+ ---
269
+
270
+ ๐Ÿง™ Part 6: Multi-Step Conversations (Scenes)
271
+
272
+ Sometimes you need back-and-forth conversations. That's where Scenes shine!
273
+
274
+ Real World Example - Pizza Order:
275
+
276
+ 1. Bot: "What pizza do you want?"
277
+ 2. User: "Pepperoni"
278
+ 3. Bot: "What size?"
279
+ 4. User: "Large"
280
+ 5. Bot: "Confirm order?"
281
+
282
+ Each step is a Scene Step!
283
+
284
+ ๐Ÿ”ง Scene Example:
285
+
286
+ ```ruby
287
+ bot.scene :order_pizza do
288
+ step :ask_type do |ctx|
289
+ ctx.reply "What pizza do you want? (Margherita/Pepperoni/Veggie)"
290
+ end
291
+
292
+ step :save_type do |ctx|
293
+ # Save their choice
294
+ ctx.session[:pizza_type] = ctx.message.text
295
+ ctx.reply "Great! Now what size? (Small/Medium/Large)"
296
+ end
297
+
298
+ step :save_size do |ctx|
299
+ ctx.session[:size] = ctx.message.text
300
+ ctx.reply "Order confirmed: #{ctx.session[:size]} #{ctx.session[:pizza_type]}!"
301
+ ctx.leave_scene # Conversation done!
302
+ end
303
+ end
304
+
305
+ # Start the scene
306
+ bot.command('order') do |ctx|
307
+ ctx.enter_scene(:order_pizza)
308
+ end
309
+ ```
310
+
311
+ ๐Ÿ’ก Scene Thinking:
312
+
313
+ Scenes are like mini-programs inside your bot. Each has:
314
+
315
+ 1. Steps - Questions to ask
316
+ 2. Memory - Remembers answers
317
+ 3. Flow - Goes from step to step automatically
318
+
319
+ ---
320
+
321
+ ๐Ÿ”Œ Part 7: Adding Superpowers (Middleware)
322
+
323
+ Middleware are plugins that run on every message. Think of them as filters or enhancers.
324
+
325
+ Simple Logger Middleware:
326
+
327
+ ```ruby
328
+ bot.use do |ctx, next_middleware|
329
+ # Runs BEFORE handling the message
330
+ puts "#{Time.now} - #{ctx.from.first_name}: #{ctx.message&.text}"
331
+
332
+ # Pass to next middleware (or the command handler)
333
+ next_middleware.call(ctx)
334
+
335
+ # Runs AFTER handling
336
+ puts "Message handled!"
337
+ end
338
+ ```
339
+
340
+ Built-in Middleware You Get:
341
+
342
+ 1. Session Middleware - Automatic ctx.session
343
+ 2. Error Handler - Catch and report errors
344
+ 3. Rate Limiter (coming soon!) - Prevent spam
345
+
346
+ ---
347
+
348
+ ๐ŸŒ Part 8: Going Live (Webhooks vs Polling)
349
+
350
+ Two Ways to Run Your Bot:
351
+
352
+ 1. Polling (Easy - for development)
353
+
354
+ ```ruby
355
+ bot.start_polling # Checks for messages every second
356
+ ```
357
+
358
+ Like checking your mailbox every minute
359
+
360
+ 2. Webhooks (Better - for production)
361
+
362
+ ```ruby
363
+ server = bot.webhook_server(port: 3000)
364
+ server.run # Telegram sends messages to you
365
+ ```
366
+
367
+ Like having a mail slot - messages arrive immediately
368
+
369
+ ๐Ÿงช Understanding the Difference:
370
+
371
+ ```ruby
372
+ # Polling (YOU ask Telegram)
373
+ # Bot: "Any new messages?"
374
+ # Telegram: "No"
375
+ # Bot: "How about now?"
376
+ # Telegram: "Still no"
377
+ # ... (every second)
378
+
379
+ # Webhook (Telegram tells YOU)
380
+ # *Message arrives*
381
+ # Telegram: "Hey bot, here's a message!"
382
+ # Bot: "Thanks, I'll handle it!"
383
+ ```
384
+
385
+ ---
386
+
387
+ ๐Ÿšจ Part 9: Handling Problems (Error Handling)
388
+
389
+ When Things Go Wrong:
390
+
391
+ ```ruby
392
+ bot.error do |error, ctx|
393
+ # Log the error
394
+ puts "ERROR: #{error.class}: #{error.message}"
395
+
396
+ # Tell user (politely!)
397
+ ctx.reply "Oops! Something went wrong. Try again? ๐Ÿค”"
398
+
399
+ # Or notify admin
400
+ ctx.api.call('sendMessage',
401
+ chat_id: ADMIN_ID,
402
+ text: "Bot error: #{error.message}"
403
+ )
404
+ end
405
+ ```
406
+
407
+ Common Errors & Fixes:
408
+
409
+ 1. Invalid Token - Check @BotFather for correct token
410
+ 2. Network Issues - Bot can't reach Telegram servers
411
+ 3. Rate Limits - Too many messages too fast
412
+ 4. Bad Parameters - Sending wrong data to API
413
+
414
+ ---
415
+
416
+ ๐ŸŽฏ Part 10: Your First Real Project
417
+
418
+ Let's build a Mood Tracker Bot:
419
+
420
+ ```ruby
421
+ require 'telegem'
422
+
423
+ bot = Telegem.new(ENV['BOT_TOKEN'])
424
+
425
+ bot.command('start') do |ctx|
426
+ ctx.reply "Welcome to MoodTracker! ๐Ÿ“Š"
427
+ ctx.reply "How are you feeling?"
428
+
429
+ keyboard = Telegem::Markup.keyboard do
430
+ row "๐Ÿ˜Š Happy", "๐Ÿ˜ข Sad", "๐Ÿ˜ก Angry"
431
+ row "๐Ÿ˜ด Tired", "๐Ÿค” Thoughtful", "๐ŸŽ‰ Excited"
432
+ end
433
+
434
+ ctx.reply "Choose your mood:", reply_markup: keyboard
435
+ end
436
+
437
+ bot.on(:message) do |ctx|
438
+ next if ctx.message.command?
439
+
440
+ mood = ctx.message.text
441
+ ctx.session[:moods] ||= []
442
+ ctx.session[:moods] << { mood: mood, time: Time.now }
443
+
444
+ ctx.reply "Noted: #{mood}"
445
+ ctx.reply "You've logged #{ctx.session[:moods].size} moods today!"
446
+ end
447
+
448
+ bot.command('stats') do |ctx|
449
+ moods = ctx.session[:moods] || []
450
+
451
+ if moods.empty?
452
+ ctx.reply "No moods logged yet!"
453
+ else
454
+ # Count each mood
455
+ counts = moods.group_by { |m| m[:mood] }
456
+ .transform_values(&:size)
457
+
458
+ stats = counts.map { |mood, count| "#{mood}: #{count}x" }.join("\n")
459
+ ctx.reply "Your mood stats:\n#{stats}"
460
+ end
461
+ end
462
+
463
+ bot.start_polling
464
+ ```
465
+
466
+ What this bot teaches you:
467
+
468
+ ยท โœ… Commands (/start, /stats)
469
+ ยท โœ… Keyboards (mood selection)
470
+ ยท โœ… Sessions (storing mood history)
471
+ ยท โœ… Data processing (counting moods)
472
+
473
+ ---
474
+
475
+ ๐Ÿ“š Part 11: Where to Go From Here
476
+
477
+ You've Learned:
478
+
479
+ ยท โœ… Basics - Creating bots, sending messages
480
+ ยท โœ… Interaction - Keyboards, buttons, commands
481
+ ยท โœ… Memory - Sessions, multi-step conversations
482
+ ยท โœ… Structure - Middleware, error handling
483
+ ยท โœ… Deployment - Polling vs webhooks
484
+
485
+ Next Steps:
486
+
487
+ Level 1: Beginner (You are here!)
488
+
489
+ ยท Build the Mood Tracker bot above
490
+ ยท Add 2 more commands
491
+ ยท Try adding a keyboard
492
+
493
+ Level 2: Intermediate
494
+
495
+ ยท Create a scene for something (quiz, survey, game)
496
+ ยท Add file upload support (photos, documents)
497
+ ยท Store data in a database (SQLite, PostgreSQL)
498
+
499
+ Level 3: Advanced
500
+
501
+ ยท Create your own middleware
502
+ ยท Add webhook support with SSL
503
+ ยท Build a plugin system
504
+
505
+ ๐Ÿ“– Recommended Learning Path:
506
+
507
+ 1. Week 1: Build 3 simple bots (echo, counter, menu)
508
+ 2. Week 2: Add sessions to remember things
509
+ 3. Week 3: Create a scene (quiz or order form)
510
+ 4. Week 4: Deploy to a server (Heroku, Render)
511
+
512
+ ---
513
+
514
+ ๐Ÿ†˜ Getting Help
515
+
516
+ When You're Stuck:
517
+
518
+ 1. Check the error message - It often tells you what's wrong
519
+ 2. Add puts statements - See what's happening step by step
520
+ 3. Simplify - Remove code until it works, then add back
521
+ 4. Ask for help - Community is there for you!
522
+
523
+ Resources:
524
+
525
+ ยท This guide - Bookmark it!
526
+ ยท API Reference - For all methods and options
527
+ ยท Example bots - In the examples/ folder
528
+ ยท GitLab Issues - Ask questions, report bugs
529
+
530
+ ---
531
+
532
+ โœจ Final Words of Encouragement
533
+
534
+ Remember:
535
+
536
+ 1. Every expert was once a beginner - The Telegraf.js creator started just like you
537
+ 2. Bugs are normal - Even big projects have them
538
+ 3. Progress, not perfection - Each bot you build makes you better
539
+ 4. You found this - That means you can definitely use it! ๐ŸŽ‰
540
+
541
+ Your First Mission:
542
+
543
+ ```ruby
544
+ # Right now, create this file and run it
545
+ # bot_mission.rb
546
+ require 'telegem'
547
+
548
+ bot = Telegem.new('YOUR_TOKEN')
549
+
550
+ bot.command('mission') do |ctx|
551
+ ctx.reply "๐ŸŽฏ Mission accomplished!"
552
+ ctx.reply "You just built and ran your first bot!"
553
+ ctx.reply "Next: Try changing the message above."
554
+ end
555
+
556
+ bot.start_polling
557
+ ```
558
+
559
+ Run it. See it work. You're a bot developer now! ๐ŸŽŠ
560
+
561
+ ---
562
+
563
+ ๐Ÿ“˜ Ready for More?
564
+
565
+ For advanced topics, API details, and complex examples:
566
+
567
+ ๐Ÿ‘‰ Check out USAGE.md for advanced patterns!
568
+
569
+ ---
570
+
571
+ Happy bot building! Remember: Every great bot started with a single /start command. Your journey has begun! ๐Ÿš€