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.
- checksums.yaml +4 -4
- data/.rubocop.yml +57 -0
- data/CHANGELOG.md +121 -0
- data/Gemfile +1 -1
- data/README.md +147 -0
- data/bin/telegem-ssl +71 -25
- data/contributing.md +375 -0
- data/docs/api.md +663 -0
- data/docs/bot.md +332 -0
- data/docs/changelog.md +182 -0
- data/docs/context.md +554 -0
- data/docs/core_concepts.md +218 -0
- data/docs/deployment.md +702 -0
- data/docs/error_handling.md +435 -0
- data/docs/examples.md +752 -0
- data/docs/getting_started.md +151 -0
- data/docs/handlers.md +580 -0
- data/docs/keyboards.md +446 -0
- data/docs/middleware.md +536 -0
- data/docs/plugins.md +384 -0
- data/docs/scenes.md +517 -0
- data/docs/sessions.md +544 -0
- data/docs/testing.md +612 -0
- data/docs/troubleshooting.md +574 -0
- data/docs/types.md +538 -0
- data/docs/webhooks.md +456 -0
- data/lib/api/client.rb +38 -10
- data/lib/api/types.rb +433 -172
- data/lib/core/composer.rb +3 -3
- data/lib/core/context.rb +17 -11
- data/lib/plugins/cc +3 -0
- data/lib/plugins/file_extract.rb +2 -2
- data/lib/plugins/translate.rb +43 -0
- data/lib/session/memory_store.rb +90 -103
- data/lib/session/redis.rb +91 -0
- data/lib/telegem.rb +4 -4
- data/lib/webhook/server.rb +5 -3
- metadata +51 -35
- data/CHANGELOG +0 -95
- data/Contributing.md +0 -161
- data/Readme.md +0 -302
- data/examples/.gitkeep +0 -0
- data/public/.gitkeep +0 -0
data/docs/plugins.md
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
# Plugins
|
|
2
|
+
|
|
3
|
+
Telegem includes several plugins for common bot functionality. Plugins extend the bot with specialized features.
|
|
4
|
+
|
|
5
|
+
## FileExtract Plugin
|
|
6
|
+
|
|
7
|
+
Extract content from uploaded files automatically detecting file types.
|
|
8
|
+
|
|
9
|
+
### Supported Formats
|
|
10
|
+
|
|
11
|
+
- **PDF**: Text extraction with page count and metadata
|
|
12
|
+
- **JSON**: Full parsing of nested structures
|
|
13
|
+
- **HTML**: Tag stripping with title extraction
|
|
14
|
+
- **Text files**: Full content with line count (.txt, .md, .csv)
|
|
15
|
+
|
|
16
|
+
### Usage
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
require 'telegem/plugins/file_extract'
|
|
20
|
+
|
|
21
|
+
bot.document do |ctx|
|
|
22
|
+
doc = ctx.message.document
|
|
23
|
+
file_id = doc.file_id
|
|
24
|
+
|
|
25
|
+
extractor = Telegem::Plugins::FileExtract.new(ctx.bot, file_id)
|
|
26
|
+
|
|
27
|
+
result = extractor.extract
|
|
28
|
+
|
|
29
|
+
if result[:success]
|
|
30
|
+
ctx.reply("Extracted #{result[:type]} content:")
|
|
31
|
+
ctx.reply(result[:content][0..100] + "...") # First 100 chars
|
|
32
|
+
else
|
|
33
|
+
ctx.reply("Extraction failed: #{result[:error]}")
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Configuration Options
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
extractor = Telegem::Plugins::FileExtract.new(
|
|
42
|
+
bot,
|
|
43
|
+
file_id,
|
|
44
|
+
auto_delete: true, # Delete temp files after processing
|
|
45
|
+
max_file_size: 50_000_000, # 50MB limit
|
|
46
|
+
timeout: 60 # Processing timeout
|
|
47
|
+
)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Result Structure
|
|
51
|
+
|
|
52
|
+
**Success Response:**
|
|
53
|
+
```ruby
|
|
54
|
+
{
|
|
55
|
+
success: true,
|
|
56
|
+
type: :pdf, # :pdf, :json, :html, :text
|
|
57
|
+
content: "extracted text or parsed data",
|
|
58
|
+
metadata: {
|
|
59
|
+
size: 12345, # File size in bytes
|
|
60
|
+
# Format-specific metadata
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**Error Response:**
|
|
66
|
+
```ruby
|
|
67
|
+
{
|
|
68
|
+
success: false,
|
|
69
|
+
error: "Error message"
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Format-Specific Metadata
|
|
74
|
+
|
|
75
|
+
**PDF:**
|
|
76
|
+
```ruby
|
|
77
|
+
metadata: {
|
|
78
|
+
page_count: 10,
|
|
79
|
+
info: { Creator: "PDF Creator", Producer: "PDF Producer" }
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**JSON:**
|
|
84
|
+
```ruby
|
|
85
|
+
metadata: {
|
|
86
|
+
size: 1024,
|
|
87
|
+
keys: ["name", "age", "items"],
|
|
88
|
+
length: 25 # For arrays
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**HTML:**
|
|
93
|
+
```ruby
|
|
94
|
+
metadata: {
|
|
95
|
+
size: 2048,
|
|
96
|
+
title: "Page Title"
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**Text:**
|
|
101
|
+
```ruby
|
|
102
|
+
metadata: {
|
|
103
|
+
size: 512,
|
|
104
|
+
line_count: 15
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Error Handling
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
result = extractor.extract
|
|
112
|
+
|
|
113
|
+
case result[:error]
|
|
114
|
+
when "Unsupported file type"
|
|
115
|
+
# Handle unsupported format
|
|
116
|
+
when "Failed to extract PDF"
|
|
117
|
+
# Handle PDF extraction failure
|
|
118
|
+
when "Invalid JSON format"
|
|
119
|
+
# Handle JSON parsing error
|
|
120
|
+
else
|
|
121
|
+
# Handle other errors
|
|
122
|
+
end
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Advanced Usage
|
|
126
|
+
|
|
127
|
+
```ruby
|
|
128
|
+
# Process different file types differently
|
|
129
|
+
bot.document do |ctx|
|
|
130
|
+
extractor = Telegem::Plugins::FileExtract.new(ctx.bot, ctx.message.document.file_id)
|
|
131
|
+
result = extractor.extract
|
|
132
|
+
|
|
133
|
+
case result[:type]
|
|
134
|
+
when :pdf
|
|
135
|
+
# Handle PDF
|
|
136
|
+
page_count = result[:metadata][:page_count]
|
|
137
|
+
ctx.reply("PDF has #{page_count} pages")
|
|
138
|
+
|
|
139
|
+
when :json
|
|
140
|
+
# Handle JSON
|
|
141
|
+
data = result[:content]
|
|
142
|
+
if data.is_a?(Hash) && data['type'] == 'config'
|
|
143
|
+
# Process config file
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
when :text
|
|
147
|
+
# Handle text
|
|
148
|
+
lines = result[:content].split("\n")
|
|
149
|
+
# Process lines
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Translate Plugin
|
|
155
|
+
|
|
156
|
+
Translate text between languages using an external translation service.
|
|
157
|
+
|
|
158
|
+
### Usage
|
|
159
|
+
|
|
160
|
+
```ruby
|
|
161
|
+
require 'telegem/plugins/translate'
|
|
162
|
+
|
|
163
|
+
bot.command('translate') do |ctx|
|
|
164
|
+
text = ctx.command_args
|
|
165
|
+
if text
|
|
166
|
+
translator = Telegem::Plugins::Translate.new(text, 'en', 'es')
|
|
167
|
+
# Translation happens synchronously
|
|
168
|
+
ctx.reply("Translation result would be here")
|
|
169
|
+
else
|
|
170
|
+
ctx.reply("Usage: /translate <text>")
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Constructor
|
|
176
|
+
|
|
177
|
+
```ruby
|
|
178
|
+
translator = Telegem::Plugins::Translate.new(word, from_lang, to_lang)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
**Parameters:**
|
|
182
|
+
- `word` (String): Text to translate
|
|
183
|
+
- `from_lang` (String): Source language code ('en', 'es', 'fr', etc.)
|
|
184
|
+
- `to_lang` (String): Target language code
|
|
185
|
+
|
|
186
|
+
### Response
|
|
187
|
+
|
|
188
|
+
**Success:**
|
|
189
|
+
```ruby
|
|
190
|
+
{
|
|
191
|
+
error: "false",
|
|
192
|
+
translation: "translated text"
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
**Error:**
|
|
197
|
+
```ruby
|
|
198
|
+
{
|
|
199
|
+
error: "an error occurred",
|
|
200
|
+
code: "HTTP status code"
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Integration Example
|
|
205
|
+
|
|
206
|
+
```ruby
|
|
207
|
+
bot.hears(/^translate (.+) to (\w+)/) do |ctx|
|
|
208
|
+
text = ctx.match[1]
|
|
209
|
+
target_lang = ctx.match[2]
|
|
210
|
+
|
|
211
|
+
translator = Telegem::Plugins::Translate.new(text, 'auto', target_lang)
|
|
212
|
+
result = translator.start_translating
|
|
213
|
+
|
|
214
|
+
if result[:error] == "false"
|
|
215
|
+
ctx.reply("Translation: #{result[:translation]}")
|
|
216
|
+
else
|
|
217
|
+
ctx.reply("Translation failed")
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Creating Custom Plugins
|
|
223
|
+
|
|
224
|
+
### Plugin Structure
|
|
225
|
+
|
|
226
|
+
```ruby
|
|
227
|
+
module Telegem
|
|
228
|
+
module Plugins
|
|
229
|
+
class MyPlugin
|
|
230
|
+
def initialize(bot, *args, **options)
|
|
231
|
+
@bot = bot
|
|
232
|
+
@options = options
|
|
233
|
+
# Initialize plugin
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def do_something
|
|
237
|
+
# Plugin logic
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Usage in Bot
|
|
245
|
+
|
|
246
|
+
```ruby
|
|
247
|
+
require 'telegem/plugins/my_plugin'
|
|
248
|
+
|
|
249
|
+
bot.command('mycommand') do |ctx|
|
|
250
|
+
plugin = Telegem::Plugins::MyPlugin.new(ctx.bot, arg1, option: value)
|
|
251
|
+
result = plugin.do_something
|
|
252
|
+
ctx.reply(result)
|
|
253
|
+
end
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Async Plugin Example
|
|
257
|
+
|
|
258
|
+
```ruby
|
|
259
|
+
class AsyncPlugin
|
|
260
|
+
def initialize(bot, data)
|
|
261
|
+
@bot = bot
|
|
262
|
+
@data = data
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def process_async(&callback)
|
|
266
|
+
Async do
|
|
267
|
+
result = long_running_operation(@data)
|
|
268
|
+
callback.call(result)
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Usage
|
|
274
|
+
bot.command('async') do |ctx|
|
|
275
|
+
plugin = AsyncPlugin.new(ctx.bot, ctx.text)
|
|
276
|
+
plugin.process_async do |result|
|
|
277
|
+
ctx.reply("Result: #{result}")
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## Plugin Best Practices
|
|
283
|
+
|
|
284
|
+
### Error Handling
|
|
285
|
+
|
|
286
|
+
```ruby
|
|
287
|
+
class RobustPlugin
|
|
288
|
+
def extract
|
|
289
|
+
begin
|
|
290
|
+
# Plugin logic
|
|
291
|
+
{ success: true, data: result }
|
|
292
|
+
rescue => e
|
|
293
|
+
{ success: false, error: e.message }
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Configuration
|
|
300
|
+
|
|
301
|
+
```ruby
|
|
302
|
+
class ConfigurablePlugin
|
|
303
|
+
DEFAULTS = {
|
|
304
|
+
timeout: 30,
|
|
305
|
+
retries: 3
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
def initialize(bot, **options)
|
|
309
|
+
@options = DEFAULTS.merge(options)
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### Resource Management
|
|
315
|
+
|
|
316
|
+
```ruby
|
|
317
|
+
class ResourcePlugin
|
|
318
|
+
def initialize(bot)
|
|
319
|
+
@temp_files = []
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def process
|
|
323
|
+
# Create temp files
|
|
324
|
+
# Process
|
|
325
|
+
# Return result
|
|
326
|
+
ensure
|
|
327
|
+
cleanup_temp_files
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
private
|
|
331
|
+
|
|
332
|
+
def cleanup_temp_files
|
|
333
|
+
@temp_files.each do |file|
|
|
334
|
+
File.unlink(file) rescue nil
|
|
335
|
+
end
|
|
336
|
+
@temp_files.clear
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### Logging
|
|
342
|
+
|
|
343
|
+
```ruby
|
|
344
|
+
class LoggingPlugin
|
|
345
|
+
def initialize(bot, logger = nil)
|
|
346
|
+
@logger = logger || bot.logger
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
def do_work
|
|
350
|
+
@logger.info("Starting work")
|
|
351
|
+
result = perform_work
|
|
352
|
+
@logger.info("Work completed")
|
|
353
|
+
result
|
|
354
|
+
end
|
|
355
|
+
end
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
## Available Plugins Summary
|
|
359
|
+
|
|
360
|
+
| Plugin | Purpose | Dependencies |
|
|
361
|
+
|--------|---------|--------------|
|
|
362
|
+
| FileExtract | Extract content from files | pdf-reader |
|
|
363
|
+
| Translate | Translate text | httparty |
|
|
364
|
+
|
|
365
|
+
## Installing Plugin Dependencies
|
|
366
|
+
|
|
367
|
+
```ruby
|
|
368
|
+
# Gemfile
|
|
369
|
+
gem 'telegem'
|
|
370
|
+
gem 'pdf-reader' # For FileExtract
|
|
371
|
+
gem 'httparty' # For Translate
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
## Contributing Plugins
|
|
375
|
+
|
|
376
|
+
1. Create plugin in `lib/telegem/plugins/`
|
|
377
|
+
2. Add comprehensive documentation
|
|
378
|
+
3. Include tests
|
|
379
|
+
4. Follow naming conventions
|
|
380
|
+
5. Handle errors gracefully
|
|
381
|
+
6. Make configuration optional
|
|
382
|
+
|
|
383
|
+
Plugins extend Telegem's functionality, allowing developers to add specialized features without modifying the core framework.</content>
|
|
384
|
+
<parameter name="filePath">/home/slick/telegem/docs/plugins.md
|