telegem 3.3.0 → 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.
data/docs/bot.md ADDED
@@ -0,0 +1,332 @@
1
+ # Bot Configuration
2
+
3
+ The `Telegem::Core::Bot` class is the main entry point for creating and configuring Telegram bots.
4
+
5
+ ## Initialization
6
+
7
+ ```ruby
8
+ bot = Telegem.new(token, **options)
9
+ ```
10
+
11
+ ### Parameters
12
+
13
+ - `token` (String): Your bot token from @BotFather
14
+ - `options` (Hash): Configuration options
15
+
16
+ ### Options
17
+
18
+ | Option | Type | Default | Description |
19
+ |--------|------|---------|-------------|
20
+ | `logger` | Logger | STDOUT logger | Logger instance for bot output |
21
+ | `timeout` | Integer | 30 | HTTP request timeout in seconds |
22
+ | `session_store` | Store | MemoryStore | Session storage backend |
23
+
24
+ ## Basic Configuration
25
+
26
+ ```ruby
27
+ require 'telegem'
28
+
29
+ bot = Telegem.new(
30
+ 'YOUR_BOT_TOKEN',
31
+ logger: Logger.new('bot.log'),
32
+ timeout: 60,
33
+ session_store: Telegem::Session::RedisStore.new(redis_client)
34
+ )
35
+ ```
36
+
37
+ ## Handler Registration
38
+
39
+ ### Commands
40
+
41
+ ```ruby
42
+ bot.command('start') do |ctx|
43
+ ctx.reply("Welcome!")
44
+ end
45
+
46
+ bot.command('help') do |ctx|
47
+ ctx.reply("Help message")
48
+ end
49
+ ```
50
+
51
+ Commands automatically match `/command` or `/command@botname`.
52
+
53
+ ### Text Patterns
54
+
55
+ ```ruby
56
+ # Exact match
57
+ bot.hears('hello') do |ctx|
58
+ ctx.reply("Hi there!")
59
+ end
60
+
61
+ # Regular expressions
62
+ bot.hears(/^hello/i) do |ctx|
63
+ ctx.reply("Hello to you too!")
64
+ end
65
+
66
+ # Any text
67
+ bot.hears(/.+/) do |ctx|
68
+ ctx.reply("You said: #{ctx.message.text}")
69
+ end
70
+ ```
71
+
72
+ ### Update Type Handlers
73
+
74
+ ```ruby
75
+ # Callback queries
76
+ bot.callback_query do |ctx|
77
+ # Handle button presses
78
+ end
79
+
80
+ # Inline queries
81
+ bot.inline_query do |ctx|
82
+ # Handle inline search
83
+ end
84
+
85
+ # Media messages
86
+ bot.photo do |ctx|
87
+ ctx.reply("Nice photo!")
88
+ end
89
+
90
+ bot.document do |ctx|
91
+ ctx.reply("Received document")
92
+ end
93
+
94
+ # Location messages
95
+ bot.location do |ctx|
96
+ latitude = ctx.message.location.latitude
97
+ longitude = ctx.message.location.longitude
98
+ ctx.reply("Location: #{latitude}, #{longitude}")
99
+ end
100
+
101
+ # Contact messages
102
+ bot.contact do |ctx|
103
+ contact = ctx.message.contact
104
+ ctx.reply("Contact: #{contact.first_name} #{contact.last_name}")
105
+ end
106
+ ```
107
+
108
+ ### Generic Handlers
109
+
110
+ ```ruby
111
+ # Handle specific update types
112
+ bot.on(:message) do |ctx|
113
+ # Handle all messages
114
+ end
115
+
116
+ bot.on(:callback_query) do |ctx|
117
+ # Handle callback queries
118
+ end
119
+
120
+ # With filters
121
+ bot.on(:message, chat_type: 'private') do |ctx|
122
+ # Only private messages
123
+ end
124
+
125
+ bot.on(:message, text: /^\/admin/) do |ctx|
126
+ # Messages starting with /admin
127
+ end
128
+ ```
129
+
130
+ ## Middleware
131
+
132
+ ```ruby
133
+ # Add middleware
134
+ bot.use(MyMiddleware.new)
135
+
136
+ # Multiple middleware
137
+ bot.use AuthenticationMiddleware.new
138
+ bot.use RateLimitMiddleware.new(limit: 10)
139
+ bot.use LoggingMiddleware.new
140
+
141
+ # Inline middleware
142
+ bot.use do |ctx, next_middleware|
143
+ puts "Processing: #{ctx.update.type}"
144
+ next_middleware.call(ctx)
145
+ end
146
+ ```
147
+
148
+ ## Scenes
149
+
150
+ ```ruby
151
+ bot.scene :registration do
152
+ step :ask_name do |ctx|
153
+ ctx.reply("What's your name?")
154
+ end
155
+
156
+ step :save_name do |ctx|
157
+ ctx.session[:name] = ctx.message.text
158
+ ctx.reply("Hi #{ctx.session[:name]}!")
159
+ ctx.leave_scene
160
+ end
161
+ end
162
+
163
+ # Enter scene
164
+ bot.command('register') do |ctx|
165
+ ctx.enter_scene(:registration)
166
+ end
167
+ ```
168
+
169
+ ## Error Handling
170
+
171
+ ```ruby
172
+ bot.error do |error, ctx|
173
+ puts "Error: #{error.message}"
174
+ ctx&.reply("Sorry, something went wrong!")
175
+ end
176
+ ```
177
+
178
+ ## Starting the Bot
179
+
180
+ ### Polling Mode (Development)
181
+
182
+ ```ruby
183
+ bot.start_polling(
184
+ timeout: 30, # Long polling timeout
185
+ limit: 100, # Max updates per request
186
+ allowed_updates: nil # Update types to receive
187
+ )
188
+ ```
189
+
190
+ ### Webhook Mode (Production)
191
+
192
+ ```ruby
193
+ server = bot.webhook(
194
+ port: 3000,
195
+ host: '0.0.0.0'
196
+ )
197
+
198
+ server.run
199
+ ```
200
+
201
+ ## Bot Methods
202
+
203
+ ### Information
204
+
205
+ ```ruby
206
+ bot.token # Bot token
207
+ bot.api # API client instance
208
+ bot.logger # Logger instance
209
+ bot.running? # Is bot running?
210
+ ```
211
+
212
+ ### Control
213
+
214
+ ```ruby
215
+ bot.start_polling # Start polling
216
+ bot.shutdown # Stop bot gracefully
217
+ ```
218
+
219
+ ### Webhook Management
220
+
221
+ ```ruby
222
+ bot.set_webhook(url: 'https://example.com/webhook')
223
+ bot.delete_webhook
224
+ bot.get_webhook_info
225
+ ```
226
+
227
+ ### Dynamic Handler Registration
228
+
229
+ ```ruby
230
+ # Add handlers at runtime
231
+ bot.command('dynamic') do |ctx|
232
+ ctx.reply("Dynamic command!")
233
+ end
234
+
235
+ # Remove handlers (not directly supported, recreate bot)
236
+ ```
237
+
238
+ ## Configuration Examples
239
+
240
+ ### Development Setup
241
+
242
+ ```ruby
243
+ bot = Telegem.new(
244
+ ENV['BOT_TOKEN'],
245
+ logger: Logger.new(STDOUT),
246
+ session_store: Telegem::Session::MemoryStore.new
247
+ )
248
+ ```
249
+
250
+ ### Production Setup
251
+
252
+ ```ruby
253
+ require 'redis'
254
+
255
+ redis = Redis.new(url: ENV['REDIS_URL'])
256
+ bot = Telegem.new(
257
+ ENV['BOT_TOKEN'],
258
+ logger: Logger.new('log/bot.log'),
259
+ timeout: 60,
260
+ session_store: Telegem::Session::RedisStore.new(redis)
261
+ )
262
+ ```
263
+
264
+ ### Custom Logger
265
+
266
+ ```ruby
267
+ logger = Logger.new('bot.log')
268
+ logger.level = Logger::INFO
269
+
270
+ bot = Telegem.new(
271
+ ENV['BOT_TOKEN'],
272
+ logger: logger
273
+ )
274
+ ```
275
+
276
+ ## Edge Cases and Considerations
277
+
278
+ ### Bot Token Validation
279
+
280
+ - Token must be in format: `123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11`
281
+ - Invalid tokens will cause API errors on first request
282
+
283
+ ### Handler Conflicts
284
+
285
+ ```ruby
286
+ # This will match both handlers
287
+ bot.hears(/.+/) do |ctx|
288
+ # Matches everything
289
+ end
290
+
291
+ bot.command('start') do |ctx|
292
+ # Never reached because regex matches first
293
+ end
294
+ ```
295
+
296
+ Solution: Order matters, more specific handlers first.
297
+
298
+ ### Memory Usage
299
+
300
+ - Long-running bots accumulate session data
301
+ - Use TTL on session stores
302
+ - Monitor memory usage in production
303
+
304
+ ### Rate Limiting
305
+
306
+ Telegram has rate limits:
307
+
308
+ - 30 messages/second for broadcasting
309
+ - 20 callbacks/second
310
+ - Implement middleware for rate limiting
311
+
312
+ ### Update Processing
313
+
314
+ - Updates are processed sequentially
315
+ - Long-running handlers block subsequent updates
316
+ - Use async operations for I/O
317
+
318
+ ### Error Recovery
319
+
320
+ - Network errors cause polling retries
321
+ - Handler errors don't stop the bot
322
+ - Use error handlers for graceful degradation
323
+
324
+ ## Best Practices
325
+
326
+ 1. **Environment Variables**: Store tokens in environment variables
327
+ 2. **Logging**: Configure appropriate log levels
328
+ 3. **Error Handling**: Always implement error handlers
329
+ 4. **Testing**: Test handlers with different update types
330
+ 5. **Monitoring**: Monitor bot health and performance
331
+ 6. **Security**: Validate inputs and use HTTPS for webhooks</content>
332
+ <parameter name="filePath">/home/slick/telegem/docs/bot.md
data/docs/changelog.md ADDED
@@ -0,0 +1,182 @@
1
+ # Changelog
2
+
3
+ All notable changes to Telegem will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+ - Comprehensive documentation covering all features and edge cases
12
+ - Error handling guide with recovery strategies
13
+ - Deployment guides for various platforms (Heroku, Docker, AWS)
14
+ - Testing framework with examples and best practices
15
+ - Troubleshooting guide for common issues
16
+ - Performance optimization tips
17
+ - Security best practices
18
+ - Plugin development guide
19
+ - Async operation handling
20
+ - Rate limiting middleware
21
+ - Health check endpoints
22
+ - Structured logging support
23
+ - Connection pooling for better performance
24
+ - Graceful degradation features
25
+ - Input validation middleware
26
+ - Monitoring and metrics collection
27
+
28
+ ### Changed
29
+ - Improved error messages and user feedback
30
+ - Enhanced session management with TTL support
31
+ - Better async operation handling
32
+ - More robust webhook server implementation
33
+ - Improved plugin architecture
34
+
35
+ ### Fixed
36
+ - Memory leaks in long-running processes
37
+ - Race conditions in concurrent operations
38
+ - File upload size validation
39
+ - SSL certificate handling
40
+ - Session persistence issues
41
+
42
+ ## [1.0.0] - 2024-01-15
43
+
44
+ ### Added
45
+ - Initial release of Telegem framework
46
+ - Core bot functionality with command and hears handlers
47
+ - Telegram API client with automatic retries
48
+ - Session management with memory and Redis stores
49
+ - Scene system for multi-step conversations
50
+ - Inline and reply keyboard DSL
51
+ - Webhook and polling support
52
+ - FileExtract plugin for document processing
53
+ - Translate plugin for text translation
54
+ - Middleware system for request processing
55
+ - Type-safe API response handling
56
+ - Comprehensive error handling
57
+ - Async I/O support with Async gem
58
+ - Rate limiting and timeout handling
59
+ - SSL/TLS support for webhooks
60
+
61
+ ### Technical Details
62
+ - Ruby 3.0+ requirement
63
+ - Async gem for concurrency
64
+ - Modular architecture with plugins
65
+ - RESTful API design
66
+ - Comprehensive test coverage
67
+ - Production-ready deployment options
68
+
69
+ ---
70
+
71
+ ## Version History
72
+
73
+ ### Pre-1.0 Releases
74
+
75
+ #### [0.5.0] - 2023-12-01
76
+ - Beta release with core functionality
77
+ - Basic bot operations
78
+ - API client implementation
79
+ - Handler system
80
+ - Session management
81
+
82
+ #### [0.3.0] - 2023-11-15
83
+ - Alpha release
84
+ - Basic Telegram API integration
85
+ - Command parsing
86
+ - Message handling
87
+
88
+ #### [0.1.0] - 2023-10-01
89
+ - Initial prototype
90
+ - Basic bot framework structure
91
+ - Proof of concept implementation
92
+
93
+ ## Migration Guide
94
+
95
+ ### Upgrading from 0.x to 1.0
96
+
97
+ #### Breaking Changes
98
+ 1. **Handler Registration:**
99
+ ```ruby
100
+ # Old
101
+ bot.on('/start') { |ctx| ... }
102
+
103
+ # New
104
+ bot.command('start') { |ctx| ... }
105
+ ```
106
+
107
+ 2. **Session Store:**
108
+ ```ruby
109
+ # Old
110
+ bot.session_store = Redis.new
111
+
112
+ # New
113
+ bot.session_store = Telegem::Session::RedisStore.new(ENV['REDIS_URL'])
114
+ ```
115
+
116
+ 3. **Middleware:**
117
+ ```ruby
118
+ # Old
119
+ bot.middleware.add(MyMiddleware)
120
+
121
+ # New
122
+ bot.use MyMiddleware.new
123
+ ```
124
+
125
+ #### New Features
126
+ - Async operations support
127
+ - Plugin system
128
+ - Enhanced error handling
129
+ - Webhook SSL support
130
+ - Scene system improvements
131
+
132
+ #### Migration Steps
133
+ 1. Update handler registrations
134
+ 2. Configure new session store format
135
+ 3. Update middleware usage
136
+ 4. Add error handling
137
+ 5. Test thoroughly
138
+
139
+ ## Future Plans
140
+
141
+ ### Version 1.1.0 (Planned)
142
+ - [ ] Database session store
143
+ - [ ] Message queue integration
144
+ - [ ] Advanced rate limiting
145
+ - [ ] Bot analytics dashboard
146
+ - [ ] Multi-language support
147
+
148
+ ### Version 1.2.0 (Planned)
149
+ - [ ] Voice message handling
150
+ - [ ] Location services integration
151
+ - [ ] Payment processing
152
+ - [ ] Bot-to-bot communication
153
+ - [ ] Advanced scene workflows
154
+
155
+ ### Version 2.0.0 (Planned)
156
+ - [ ] GraphQL API support
157
+ - [ ] Real-time updates with WebSocket
158
+ - [ ] Machine learning integrations
159
+ - [ ] Advanced NLP features
160
+ - [ ] Multi-platform bot support
161
+
162
+ ## Contributing
163
+
164
+ When contributing to Telegem, please:
165
+ 1. Update the changelog with your changes
166
+ 2. Follow the existing format
167
+ 3. Add entries under [Unreleased] section
168
+ 4. Categorize changes as Added, Changed, Fixed, or Removed
169
+
170
+ ## Categories
171
+
172
+ - **Added** for new features
173
+ - **Changed** for changes in existing functionality
174
+ - **Deprecated** for soon-to-be removed features
175
+ - **Removed** for now removed features
176
+ - **Fixed** for any bug fixes
177
+ - **Security** in case of vulnerabilities
178
+
179
+ ---
180
+
181
+ This changelog follows the principles of [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and is automatically updated with each release.</content>
182
+ <parameter name="filePath">/home/slick/telegem/docs/changelog.md