telegem 1.0.5 → 1.0.6
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/docs/SETTING_WEBHOOK.md +367 -0
- data/lib/core/bot.rb +2 -2
- data/lib/telegem.rb +1 -1
- data/lib/webhook/server.rb +2 -2
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3777c7718d621ee9b2905ea456a9255c16cee07d1dbda297521602786088d046
|
|
4
|
+
data.tar.gz: ba5e53faa9da8b44218d709899383d6f932366aa6b9444405489e21062a60b4c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 909c70af6d30e6e780dabb647e320b4986eb518987dcf9740c4194c8941df317afabc0cc5a4ce5ec8be531c5febdfc4e7659c800cbdaf0744e59475d9a4bd0b8
|
|
7
|
+
data.tar.gz: 191ccb89c87d3e8b2584324de82836571f10443f32b79ba3bdce98af496de11e32600b3f4cd31d844dc0f19903bf6f3ab6cf42a5fcb9df47151a5c072e7d127f
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
Webhook Setup Guide for Telegem
|
|
2
|
+
|
|
3
|
+
📖 Overview
|
|
4
|
+
|
|
5
|
+
This guide covers how to set up and run your Telegram bot using webhooks with Telegem. Webhooks are the preferred method for production bots as they provide faster response times and better reliability compared to polling.
|
|
6
|
+
|
|
7
|
+
🚀 Quick Start (For Beginners)
|
|
8
|
+
|
|
9
|
+
Basic Webhook Setup
|
|
10
|
+
|
|
11
|
+
Here's the simplest way to get your bot running with webhooks:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
# 1. Create your bot
|
|
15
|
+
require 'telegem'
|
|
16
|
+
bot = Telegem::Core::Bot.new('YOUR_BOT_TOKEN')
|
|
17
|
+
|
|
18
|
+
# 2. Add your handlers
|
|
19
|
+
bot.command('start') do |ctx|
|
|
20
|
+
ctx.reply("Hello! I'm your bot.")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# 3. Start the webhook server
|
|
24
|
+
server = bot.webhook
|
|
25
|
+
server.run
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
That's it! Your bot is now:
|
|
29
|
+
|
|
30
|
+
· ✅ Listening for webhook requests
|
|
31
|
+
· ✅ Using the default port (3000)
|
|
32
|
+
· ✅ Available at /webhook/YOUR_BOT_TOKEN
|
|
33
|
+
|
|
34
|
+
Deploying to Render
|
|
35
|
+
|
|
36
|
+
For deploying to Render.com, here's your complete setup:
|
|
37
|
+
|
|
38
|
+
1. bot.rb or main.rb:
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
require 'telegem'
|
|
42
|
+
|
|
43
|
+
# Initialize bot
|
|
44
|
+
bot = Telegem::Core::Bot.new(ENV['BOT_TOKEN'])
|
|
45
|
+
|
|
46
|
+
# Add your handlers
|
|
47
|
+
bot.command('start') { |ctx| ctx.reply("Hello!") }
|
|
48
|
+
|
|
49
|
+
# Start webhook server
|
|
50
|
+
server = bot.webhook
|
|
51
|
+
server.run
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
2. Gemfile:
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
source 'https://rubygems.org'
|
|
58
|
+
gem 'telegem'
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
3. Render.com Settings:
|
|
62
|
+
|
|
63
|
+
· Start Command: bundle exec ruby bot.rb
|
|
64
|
+
· Environment Variables:
|
|
65
|
+
· BOT_TOKEN: Your Telegram bot token
|
|
66
|
+
· PORT: Automatically set by Render
|
|
67
|
+
|
|
68
|
+
⚙️ Configuration Options
|
|
69
|
+
|
|
70
|
+
Using a Configuration Block
|
|
71
|
+
|
|
72
|
+
Want to customize your webhook server? Use the block syntax:
|
|
73
|
+
|
|
74
|
+
```ruby
|
|
75
|
+
server = bot.webhook do |config|
|
|
76
|
+
config.port = 8080 # Custom port
|
|
77
|
+
config.host = '0.0.0.0' # Bind to all interfaces
|
|
78
|
+
config.logger = MyCustomLogger.new # Custom logger
|
|
79
|
+
# No need to set endpoint - it's automatic!
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
server.run
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Environment Variables
|
|
86
|
+
|
|
87
|
+
The server automatically reads these environment variables:
|
|
88
|
+
|
|
89
|
+
Variable Purpose Default
|
|
90
|
+
WEBHOOK_URL Full webhook URL for Telegram http://host:port/webhook/token
|
|
91
|
+
PORT Server port 3000
|
|
92
|
+
HOST Server host '0.0.0.0'
|
|
93
|
+
|
|
94
|
+
🔧 Advanced Configuration
|
|
95
|
+
|
|
96
|
+
Custom Webhook Path
|
|
97
|
+
|
|
98
|
+
Need a different webhook path? Here's how:
|
|
99
|
+
|
|
100
|
+
```ruby
|
|
101
|
+
# Method 1: Set WEBHOOK_URL environment variable
|
|
102
|
+
ENV['WEBHOOK_URL'] = 'https://your-domain.com/custom/path'
|
|
103
|
+
|
|
104
|
+
# Method 2: Configure Telegram manually
|
|
105
|
+
bot.set_webhook(
|
|
106
|
+
url: 'https://your-domain.com/custom/path',
|
|
107
|
+
max_connections: 40,
|
|
108
|
+
allowed_updates: ['message', 'callback_query']
|
|
109
|
+
)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
SSL/HTTPS Setup
|
|
113
|
+
|
|
114
|
+
For production with SSL (required by Telegram for webhooks):
|
|
115
|
+
|
|
116
|
+
```ruby
|
|
117
|
+
server = bot.webhook do |config|
|
|
118
|
+
config.port = 443
|
|
119
|
+
# SSL certificates (if running standalone)
|
|
120
|
+
# config.ssl_certificate = '/path/to/cert.pem'
|
|
121
|
+
# config.ssl_key = '/path/to/key.pem'
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Or use a reverse proxy (recommended):
|
|
125
|
+
# - Nginx
|
|
126
|
+
# - Cloudflare
|
|
127
|
+
# - Render's built-in HTTPS
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Integration with Web Frameworks
|
|
131
|
+
|
|
132
|
+
Integrate Telegem into your existing Rails or Sinatra app:
|
|
133
|
+
|
|
134
|
+
```ruby
|
|
135
|
+
# For Rails: config/application.rb or config.ru
|
|
136
|
+
require 'telegem'
|
|
137
|
+
|
|
138
|
+
bot = Telegem::Core::Bot.new(ENV['BOT_TOKEN'])
|
|
139
|
+
|
|
140
|
+
# Mount as middleware in config.ru:
|
|
141
|
+
# use Telegem::Webhook::Middleware, bot
|
|
142
|
+
|
|
143
|
+
# For Sinatra:
|
|
144
|
+
require 'sinatra'
|
|
145
|
+
require 'telegem'
|
|
146
|
+
|
|
147
|
+
bot = Telegem::Core::Bot.new(ENV['BOT_TOKEN'])
|
|
148
|
+
use Telegem::Webhook::Middleware, bot
|
|
149
|
+
|
|
150
|
+
get '/' do
|
|
151
|
+
"Main app is running!"
|
|
152
|
+
end
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
🚨 Troubleshooting
|
|
156
|
+
|
|
157
|
+
Common Issues & Solutions
|
|
158
|
+
|
|
159
|
+
1. "Undefined method `run'" Error
|
|
160
|
+
|
|
161
|
+
```ruby
|
|
162
|
+
# ❌ WRONG - Returns Middleware, not Server
|
|
163
|
+
server = bot.webhook(app: something)
|
|
164
|
+
server.run # ERROR!
|
|
165
|
+
|
|
166
|
+
# ✅ CORRECT - Returns Server
|
|
167
|
+
server = bot.webhook # No arguments
|
|
168
|
+
server.run # WORKS!
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
2. Webhook Not Receiving Updates
|
|
172
|
+
|
|
173
|
+
```ruby
|
|
174
|
+
# Check if webhook is set
|
|
175
|
+
info = bot.get_webhook_info
|
|
176
|
+
puts "Webhook URL: #{info.url}"
|
|
177
|
+
puts "Has pending updates: #{info.pending_update_count}"
|
|
178
|
+
|
|
179
|
+
# Set webhook if needed
|
|
180
|
+
bot.set_webhook(url: 'https://your-domain.com/webhook/' + bot.token)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
3. Port Already in Use
|
|
184
|
+
|
|
185
|
+
```ruby
|
|
186
|
+
# Use a different port
|
|
187
|
+
server = bot.webhook do |config|
|
|
188
|
+
config.port = ENV['PORT'] || 3000 # Use Render's PORT
|
|
189
|
+
end
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
4. Slow Response to Telegram
|
|
193
|
+
|
|
194
|
+
```ruby
|
|
195
|
+
# The server responds immediately (200 OK)
|
|
196
|
+
# then processes updates in background threads
|
|
197
|
+
# This is NORMAL and by design!
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Debug Mode
|
|
201
|
+
|
|
202
|
+
Enable detailed logging for troubleshooting:
|
|
203
|
+
|
|
204
|
+
```ruby
|
|
205
|
+
require 'logger'
|
|
206
|
+
|
|
207
|
+
logger = Logger.new(STDOUT)
|
|
208
|
+
logger.level = Logger::DEBUG
|
|
209
|
+
|
|
210
|
+
bot = Telegem::Core::Bot.new('TOKEN', logger: logger)
|
|
211
|
+
server = bot.webhook do |config|
|
|
212
|
+
config.logger = logger
|
|
213
|
+
end
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
📊 Production Best Practices
|
|
217
|
+
|
|
218
|
+
1. Health Checks
|
|
219
|
+
|
|
220
|
+
The webhook server includes a built-in health endpoint:
|
|
221
|
+
|
|
222
|
+
```
|
|
223
|
+
GET /health
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Returns 200 OK - use this for monitoring.
|
|
227
|
+
|
|
228
|
+
2. Error Handling
|
|
229
|
+
|
|
230
|
+
```ruby
|
|
231
|
+
bot.error do |error, ctx|
|
|
232
|
+
# Log to your error tracking service
|
|
233
|
+
Sentry.capture_exception(error)
|
|
234
|
+
|
|
235
|
+
# Optional: notify admin
|
|
236
|
+
bot.api.send_message(
|
|
237
|
+
chat_id: ADMIN_ID,
|
|
238
|
+
text: "Bot error: #{error.message}"
|
|
239
|
+
)
|
|
240
|
+
end
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
3. Rate Limiting
|
|
244
|
+
|
|
245
|
+
Consider adding rate limiting middleware:
|
|
246
|
+
|
|
247
|
+
```ruby
|
|
248
|
+
class RateLimiter
|
|
249
|
+
def initialize(bot, requests_per_minute: 60)
|
|
250
|
+
@bot = bot
|
|
251
|
+
@limits = {}
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def call(ctx)
|
|
255
|
+
user_id = ctx.from.id
|
|
256
|
+
# Implement your rate limiting logic
|
|
257
|
+
# ...
|
|
258
|
+
yield # Continue to next middleware/handler
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
bot.use RateLimiter
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
4. Monitoring
|
|
266
|
+
|
|
267
|
+
Monitor these key metrics:
|
|
268
|
+
|
|
269
|
+
· Response time to Telegram (< 1 second)
|
|
270
|
+
· Error rate (< 1%)
|
|
271
|
+
· Queue length (pending updates)
|
|
272
|
+
|
|
273
|
+
🔄 Webhook vs Polling
|
|
274
|
+
|
|
275
|
+
Aspect Webhook Polling
|
|
276
|
+
Performance Faster (real-time) Slower (up to 30s delay)
|
|
277
|
+
Resource Use Efficient Constant requests
|
|
278
|
+
Setup More complex Simple
|
|
279
|
+
Production ✅ Recommended Only for dev/testing
|
|
280
|
+
Render.com Web Service Background Worker
|
|
281
|
+
|
|
282
|
+
```ruby
|
|
283
|
+
# Switching between methods is easy:
|
|
284
|
+
if ENV['RACK_ENV'] == 'production'
|
|
285
|
+
# Use webhooks in production
|
|
286
|
+
bot.webhook.run
|
|
287
|
+
else
|
|
288
|
+
# Use polling in development
|
|
289
|
+
bot.start_polling
|
|
290
|
+
sleep # Keep process alive
|
|
291
|
+
end
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
🎯 Example: Complete Production Setup
|
|
295
|
+
|
|
296
|
+
```ruby
|
|
297
|
+
# production_bot.rb
|
|
298
|
+
require 'telegem'
|
|
299
|
+
require 'logger'
|
|
300
|
+
|
|
301
|
+
# Configure bot
|
|
302
|
+
bot = Telegem::Core::Bot.new(
|
|
303
|
+
ENV['BOT_TOKEN'],
|
|
304
|
+
logger: Logger.new('logs/bot.log', 'daily')
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
# Add middleware
|
|
308
|
+
bot.use MyAuthMiddleware
|
|
309
|
+
bot.use RateLimiter
|
|
310
|
+
|
|
311
|
+
# Add handlers
|
|
312
|
+
bot.command('start') { |ctx| ctx.reply("Welcome!") }
|
|
313
|
+
# ... more handlers
|
|
314
|
+
|
|
315
|
+
# Error handling
|
|
316
|
+
bot.error do |error, ctx|
|
|
317
|
+
logger.error("Error: #{error.message}")
|
|
318
|
+
ctx&.reply("Sorry, something went wrong!")
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# Get Render's port or use default
|
|
322
|
+
port = ENV['PORT'] || 3000
|
|
323
|
+
|
|
324
|
+
# Start server
|
|
325
|
+
server = bot.webhook do |config|
|
|
326
|
+
config.port = port
|
|
327
|
+
config.host = '0.0.0.0'
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
puts "🚀 Starting bot on port #{port}"
|
|
331
|
+
puts "📡 Webhook URL: #{server.webhook_url}"
|
|
332
|
+
server.run
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
❓ FAQ
|
|
336
|
+
|
|
337
|
+
Q: What port should I use?
|
|
338
|
+
A: Use ENV['PORT'] on Render/Heroku, or 3000 for local development.
|
|
339
|
+
|
|
340
|
+
Q: Do I need to call set_webhook?
|
|
341
|
+
A: Not with Telegem! It sets the webhook automatically when you call server.webhook_url.
|
|
342
|
+
|
|
343
|
+
Q: Can I run multiple bots?
|
|
344
|
+
A: Yes! Each needs its own server instance on a different port.
|
|
345
|
+
|
|
346
|
+
Q: How do I handle bot restarts?
|
|
347
|
+
A: The server gracefully shuts down on SIGTERM. Set restart: always in your deployment config.
|
|
348
|
+
|
|
349
|
+
Q: My bot stops after some time?
|
|
350
|
+
A: On Render's free tier, services sleep after inactivity. Upgrade to a paid plan for 24/7 uptime.
|
|
351
|
+
|
|
352
|
+
📚 Additional Resources
|
|
353
|
+
|
|
354
|
+
· Telegram Bot API Documentation
|
|
355
|
+
· Render.com Deployment Guide
|
|
356
|
+
· Telegem GitHub Repository
|
|
357
|
+
· Webhook vs Polling Explained
|
|
358
|
+
|
|
359
|
+
---
|
|
360
|
+
|
|
361
|
+
Need Help?
|
|
362
|
+
|
|
363
|
+
· Check the server logs: tail -f logs/bot.log
|
|
364
|
+
· Verify webhook URL with bot.get_webhook_info
|
|
365
|
+
· Test locally with ngrok: ngrok http 3000
|
|
366
|
+
|
|
367
|
+
Happy bot building! 🤖
|
data/lib/core/bot.rb
CHANGED
|
@@ -79,7 +79,7 @@ module Telegem
|
|
|
79
79
|
self
|
|
80
80
|
end
|
|
81
81
|
|
|
82
|
-
def webhook(app = nil, &block)
|
|
82
|
+
def webhook(app = nil, port: nil, host: '0.0.0.0', logger: nil, &block)
|
|
83
83
|
require 'webhook/server'
|
|
84
84
|
|
|
85
85
|
if block_given?
|
|
@@ -87,7 +87,7 @@ module Telegem
|
|
|
87
87
|
elsif app
|
|
88
88
|
Webhook::Middleware.new(self, app)
|
|
89
89
|
else
|
|
90
|
-
Webhook::Server.new(self)
|
|
90
|
+
Webhook::Server.new(self, port: port, host: host, logger: logger)
|
|
91
91
|
end
|
|
92
92
|
end
|
|
93
93
|
|
data/lib/telegem.rb
CHANGED
data/lib/webhook/server.rb
CHANGED
|
@@ -7,9 +7,9 @@ module Telegem
|
|
|
7
7
|
class Server
|
|
8
8
|
attr_reader :bot, :port, :host, :logger, :server, :running
|
|
9
9
|
|
|
10
|
-
def initialize(bot, port:
|
|
10
|
+
def initialize(bot, port: nil, host: '0.0.0.0', logger: nil)
|
|
11
11
|
@bot = bot
|
|
12
|
-
@port = port
|
|
12
|
+
@port = port || ENV['PORT'] || 1000
|
|
13
13
|
@host = host
|
|
14
14
|
@logger = logger || Logger.new($stdout)
|
|
15
15
|
@server = nil
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: telegem
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Your Name
|
|
@@ -87,6 +87,7 @@ files:
|
|
|
87
87
|
- docs/Cookbook.md
|
|
88
88
|
- docs/How_to_use.md
|
|
89
89
|
- docs/QuickStart.md
|
|
90
|
+
- docs/SETTING_WEBHOOK.md
|
|
90
91
|
- docs/UNDERSTANDING-WEBHOOK-n-POLLING.md
|
|
91
92
|
- docs/Understanding_Scene.md
|
|
92
93
|
- docs/Usage.md
|