telegem 3.3.1 → 3.4.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,218 @@
1
+ # Core Concepts
2
+
3
+ Understanding Telegem's architecture and key concepts will help you build better bots.
4
+
5
+ ## Architecture Overview
6
+
7
+ Telegem follows a middleware-based architecture inspired by Express.js and Telegraf.js:
8
+
9
+ ```
10
+ Update Received → Middleware Chain → Handler Matching → Response
11
+ ```
12
+
13
+ ## Key Components
14
+
15
+ ### 1. Bot
16
+
17
+ The main bot instance that manages:
18
+
19
+ - Telegram API communication
20
+ - Handler registration
21
+ - Middleware pipeline
22
+ - Scene management
23
+ - Session storage
24
+
25
+ ### 2. Context (ctx)
26
+
27
+ The context object passed to all handlers containing:
28
+
29
+ - Current update data
30
+ - Bot instance
31
+ - User/chat information
32
+ - Session data
33
+ - Response methods
34
+
35
+ ### 3. Handlers
36
+
37
+ Functions that process specific types of updates:
38
+
39
+ - Commands (`/start`, `/help`)
40
+ - Text patterns
41
+ - Callback queries
42
+ - Inline queries
43
+ - Media messages
44
+
45
+ ### 4. Middleware
46
+
47
+ Functions that process updates before handlers:
48
+
49
+ - Authentication
50
+ - Logging
51
+ - Rate limiting
52
+ - Session loading
53
+
54
+ ### 5. Scenes
55
+
56
+ Multi-step conversation flows for complex interactions.
57
+
58
+ ## Update Processing Flow
59
+
60
+ 1. **Update Received**: Telegram sends an update via webhook or polling
61
+ 2. **Middleware Execution**: Each middleware processes the update in order
62
+ 3. **Handler Matching**: Bot finds matching handlers for the update type
63
+ 4. **Handler Execution**: Matching handlers are called with the context
64
+ 5. **Response**: Handlers send responses back to Telegram
65
+
66
+ ## Async Architecture
67
+
68
+ Telegem uses Ruby's `async` gem for true asynchronous I/O:
69
+
70
+ - Non-blocking HTTP requests
71
+ - Concurrent update processing
72
+ - Efficient resource usage
73
+ - No thread blocking
74
+
75
+ ## Type System
76
+
77
+ Telegem provides Ruby classes for all Telegram API objects:
78
+
79
+ - `User` - Telegram user information
80
+ - `Chat` - Chat/channel/group data
81
+ - `Message` - Message content and metadata
82
+ - `Update` - Telegram update wrapper
83
+
84
+ These classes provide:
85
+
86
+ - Type-safe access to properties
87
+ - Automatic data conversion
88
+ - Helper methods
89
+ - Snake_case attribute access
90
+
91
+ ## Session Management
92
+
93
+ Sessions persist data between updates:
94
+
95
+ - User-specific storage
96
+ - Multiple backends (memory, Redis, custom)
97
+ - Automatic loading/saving
98
+ - TTL support
99
+
100
+ ## Error Handling
101
+
102
+ Comprehensive error handling at multiple levels:
103
+
104
+ - Network errors
105
+ - API errors
106
+ - Handler exceptions
107
+ - Timeout handling
108
+ - Graceful degradation
109
+
110
+ ## Threading Model
111
+
112
+ Telegem is single-threaded by default but async:
113
+
114
+ - One event loop handles all updates
115
+ - Concurrent I/O operations
116
+ - No shared mutable state issues
117
+ - Predictable execution order
118
+
119
+ ## Memory Management
120
+
121
+ Efficient memory usage through:
122
+
123
+ - Lazy loading of update data
124
+ - Automatic cleanup of temporary files
125
+ - Session TTL and cleanup
126
+ - Minimal object retention
127
+
128
+ ## Configuration
129
+
130
+ Flexible configuration options:
131
+
132
+ - Bot token and API settings
133
+ - Session store configuration
134
+ - Webhook server options
135
+ - Logging and debugging
136
+ - SSL/TLS settings
137
+
138
+ ## Extensibility
139
+
140
+ Telegem is designed to be extended:
141
+
142
+ - Custom middleware
143
+ - Plugin system
144
+ - Custom session stores
145
+ - Type extensions
146
+ - Handler extensions
147
+
148
+ ## Best Practices
149
+
150
+ ### Handler Design
151
+
152
+ - Keep handlers small and focused
153
+ - Use middleware for cross-cutting concerns
154
+ - Handle errors gracefully
155
+ - Avoid blocking operations
156
+
157
+ ### Session Usage
158
+
159
+ - Store minimal data
160
+ - Set appropriate TTL
161
+ - Clean up when done
162
+ - Handle session errors
163
+
164
+ ### Performance
165
+
166
+ - Use webhooks in production
167
+ - Implement rate limiting
168
+ - Monitor memory usage
169
+ - Profile slow handlers
170
+
171
+ ### Security
172
+
173
+ - Validate user input
174
+ - Use HTTPS for webhooks
175
+ - Store sensitive data securely
176
+ - Implement authentication when needed
177
+
178
+ ## Common Patterns
179
+
180
+ ### Command Processing
181
+
182
+ ```ruby
183
+ bot.command('process') do |ctx|
184
+ # Validate input
185
+ # Process data
186
+ # Send response
187
+ end
188
+ ```
189
+
190
+ ### Middleware Chain
191
+
192
+ ```ruby
193
+ bot.use AuthenticationMiddleware
194
+ bot.use RateLimitMiddleware
195
+ bot.use LoggingMiddleware
196
+ ```
197
+
198
+ ### Scene Flow
199
+
200
+ ```ruby
201
+ bot.scene :order do
202
+ step :select_item
203
+ step :confirm_order
204
+ step :process_payment
205
+ end
206
+ ```
207
+
208
+ ### Error Handling
209
+
210
+ ```ruby
211
+ bot.error do |error, ctx|
212
+ logger.error("Handler error: #{error}")
213
+ ctx.reply("Sorry, something went wrong!")
214
+ end
215
+ ```
216
+
217
+ Understanding these concepts will help you build robust, scalable Telegram bots with Telegem.</content>
218
+ <parameter name="filePath">/home/slick/telegem/docs/core_concepts.md