telegem 1.0.6 → 2.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.
- checksums.yaml +4 -4
- data/docs/Api.md +146 -354
- data/docs/Cookbook(copy_paste).md +644 -0
- data/docs/Getting_started.md +348 -0
- data/docs/webhook_setup.md +199 -0
- data/lib/api/client.rb +123 -49
- data/lib/api/types.rb +283 -67
- data/lib/core/bot.rb +91 -56
- data/lib/core/context.rb +96 -110
- data/lib/markup/.gitkeep +0 -0
- data/lib/markup/keyboard.rb +53 -38
- data/lib/telegem.rb +15 -5
- data/lib/webhook/server.rb +246 -112
- metadata +108 -17
- data/docs/Cookbook.md +0 -407
- data/docs/SETTING_WEBHOOK.md +0 -367
- data/docs/UNDERSTANDING-WEBHOOK-n-POLLING.md +0 -241
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
|
|
2
|
+
# 🚀 Getting Started with Telegem
|
|
3
|
+
|
|
4
|
+
Welcome to Telegem! This guide will help you build your first Telegram bot in minutes.
|
|
5
|
+
|
|
6
|
+
## 📦 Installation
|
|
7
|
+
|
|
8
|
+
Add to your `Gemfile`:
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'telegem'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or install directly:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
gem install telegem
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
🤖 Create Your First Bot
|
|
20
|
+
|
|
21
|
+
1. Get a Bot Token
|
|
22
|
+
|
|
23
|
+
1. Open Telegram, search for @BotFather
|
|
24
|
+
2. Send /newbot and follow instructions
|
|
25
|
+
3. Copy the token (looks like: 1234567890:ABCdefGHIjklMNOpqrsTUVwxyz)
|
|
26
|
+
|
|
27
|
+
2. Basic Bot Setup
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
require 'telegem'
|
|
31
|
+
|
|
32
|
+
# Create bot instance
|
|
33
|
+
bot = Telegem.new("YOUR_BOT_TOKEN")
|
|
34
|
+
|
|
35
|
+
# Echo bot - replies with same message
|
|
36
|
+
bot.on(:message) do |ctx|
|
|
37
|
+
ctx.reply("You said: #{ctx.message.text}")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Start polling (for development)
|
|
41
|
+
bot.start_polling
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Save as bot.rb and run:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
ruby bot.rb
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
📝 Core Concepts
|
|
51
|
+
|
|
52
|
+
Context (ctx)
|
|
53
|
+
|
|
54
|
+
Every handler receives a Context object with everything you need:
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
bot.on(:message) do |ctx|
|
|
58
|
+
ctx.reply("Hello!") # Send message
|
|
59
|
+
ctx.chat.id # Chat ID: 123456789
|
|
60
|
+
ctx.from.username # User: @username
|
|
61
|
+
ctx.message.text # Message text
|
|
62
|
+
ctx.message.command? # Is it a command?
|
|
63
|
+
ctx.message.command_name # Command without "/"
|
|
64
|
+
end
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Message Types
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
# Handle different update types
|
|
71
|
+
bot.on(:message) { |ctx| } # Messages
|
|
72
|
+
bot.on(:callback_query) { |ctx| } # Button clicks
|
|
73
|
+
bot.on(:inline_query) { |ctx| } # Inline queries
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
🎯 Common Patterns
|
|
77
|
+
|
|
78
|
+
1. Command Handler
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
bot.command("start") do |ctx|
|
|
82
|
+
ctx.reply("Welcome! Use /help to see commands.")
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
bot.command("help") do |ctx|
|
|
86
|
+
help_text = <<~TEXT
|
|
87
|
+
Available commands:
|
|
88
|
+
/start - Start bot
|
|
89
|
+
/help - Show this help
|
|
90
|
+
/echo [text] - Echo text
|
|
91
|
+
TEXT
|
|
92
|
+
ctx.reply(help_text)
|
|
93
|
+
end
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
2. Text Matching
|
|
97
|
+
|
|
98
|
+
```ruby
|
|
99
|
+
# Regex pattern
|
|
100
|
+
bot.hears(/hello|hi|hey/i) do |ctx|
|
|
101
|
+
ctx.reply("Hello there! 👋")
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# String contains
|
|
105
|
+
bot.on(:message, text: "ping") do |ctx|
|
|
106
|
+
ctx.reply("pong! 🏓")
|
|
107
|
+
end
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
3. Send Media
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
bot.command("photo") do |ctx|
|
|
114
|
+
ctx.photo("path/to/image.jpg", caption: "Nice pic!")
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
bot.command("document") do |ctx|
|
|
118
|
+
ctx.document("file.pdf", caption: "Here's your PDF")
|
|
119
|
+
end
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
⌨️ Keyboards
|
|
123
|
+
|
|
124
|
+
Reply Keyboard (Shown below input)
|
|
125
|
+
|
|
126
|
+
```ruby
|
|
127
|
+
bot.command("menu") do |ctx|
|
|
128
|
+
keyboard = Telegem.keyboard do
|
|
129
|
+
row "Option 1", "Option 2"
|
|
130
|
+
row "Cancel"
|
|
131
|
+
resize true # Fit to screen
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
ctx.reply("Choose option:", reply_markup: keyboard)
|
|
135
|
+
end
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Inline Keyboard (Inside message)
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
bot.command("settings") do |ctx|
|
|
142
|
+
inline = Telegem.inline do
|
|
143
|
+
row do
|
|
144
|
+
callback "Enable", "enable"
|
|
145
|
+
callback "Disable", "disable"
|
|
146
|
+
end
|
|
147
|
+
row do
|
|
148
|
+
url "Documentation", "https://gitlab.com/ruby-telegem/telegem"
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
ctx.reply("Settings:", reply_markup: inline)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Handle button clicks
|
|
156
|
+
bot.on(:callback_query) do |ctx|
|
|
157
|
+
if ctx.data == "enable"
|
|
158
|
+
ctx.answer_callback_query(text: "Enabled!")
|
|
159
|
+
ctx.edit_message_text("✅ Settings enabled!")
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
🎭 Scene System (Multi-step Conversations)
|
|
165
|
+
|
|
166
|
+
```ruby
|
|
167
|
+
# Define a scene
|
|
168
|
+
bot.scene("registration") do
|
|
169
|
+
step :ask_name do |ctx|
|
|
170
|
+
ctx.reply("What's your name?")
|
|
171
|
+
next_step :ask_age
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
step :ask_age do |ctx|
|
|
175
|
+
ctx.state[:name] = ctx.message.text
|
|
176
|
+
ctx.reply("How old are you?")
|
|
177
|
+
next_step :confirm
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
step :confirm do |ctx|
|
|
181
|
+
ctx.state[:age] = ctx.message.text
|
|
182
|
+
ctx.reply("Confirm: Name: #{ctx.state[:name]}, Age: #{ctx.state[:age]}")
|
|
183
|
+
keyboard = Telegem.inline do
|
|
184
|
+
row do
|
|
185
|
+
callback "✅ Confirm", "confirm_registration"
|
|
186
|
+
callback "❌ Cancel", "cancel_registration"
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
ctx.reply("Is this correct?", reply_markup: keyboard)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
on_enter do |ctx|
|
|
193
|
+
ctx.reply("Starting registration...")
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# Start the scene
|
|
198
|
+
bot.command("register") do |ctx|
|
|
199
|
+
ctx.enter_scene("registration")
|
|
200
|
+
end
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
🔧 Middleware (Global Handlers)
|
|
204
|
+
|
|
205
|
+
```ruby
|
|
206
|
+
# Log all updates
|
|
207
|
+
bot.use do |ctx, next_handler|
|
|
208
|
+
puts "📩 Update from #{ctx.from.username}: #{ctx.message&.text}"
|
|
209
|
+
next_handler.call(ctx)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Authentication middleware
|
|
213
|
+
class AuthMiddleware
|
|
214
|
+
def initialize(allowed_users)
|
|
215
|
+
@allowed_users = allowed_users
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def call(ctx, next_handler)
|
|
219
|
+
if @allowed_users.include?(ctx.from.id)
|
|
220
|
+
next_handler.call(ctx)
|
|
221
|
+
else
|
|
222
|
+
ctx.reply("🚫 Access denied!")
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
bot.use(AuthMiddleware.new([123456789]))
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
💾 Session Management
|
|
231
|
+
|
|
232
|
+
```ruby
|
|
233
|
+
# Enable sessions (auto-saves user data)
|
|
234
|
+
bot.command("counter") do |ctx|
|
|
235
|
+
ctx.session[:count] ||= 0
|
|
236
|
+
ctx.session[:count] += 1
|
|
237
|
+
ctx.reply("Count: #{ctx.session[:count]}")
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# Persistent across bot restarts
|
|
241
|
+
bot.command("remember") do |ctx|
|
|
242
|
+
ctx.session[:name] = ctx.message.text
|
|
243
|
+
ctx.reply("I'll remember that!")
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
bot.command("recall") do |ctx|
|
|
247
|
+
name = ctx.session[:name] || "I don't know your name"
|
|
248
|
+
ctx.reply("Your name is: #{name}")
|
|
249
|
+
end
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
☁️ Webhook Setup (Production)
|
|
253
|
+
|
|
254
|
+
For Cloud Platforms (Render, Railway, Heroku):
|
|
255
|
+
|
|
256
|
+
```ruby
|
|
257
|
+
# In config.ru or similar
|
|
258
|
+
require 'telegem'
|
|
259
|
+
|
|
260
|
+
bot = Telegem.new("YOUR_TOKEN")
|
|
261
|
+
bot.on(:message) { |ctx| ctx.reply("Hello from webhook!") }
|
|
262
|
+
|
|
263
|
+
# Auto-starts server and sets webhook
|
|
264
|
+
server = Telegem.webhook(bot)
|
|
265
|
+
|
|
266
|
+
# Or manually:
|
|
267
|
+
# server = bot.webhook
|
|
268
|
+
# server.run
|
|
269
|
+
# server.set_webhook
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
Environment Variables:
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
export TELEGRAM_BOT_TOKEN="your_token"
|
|
276
|
+
export PORT="3000" # Cloud platforms set this
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
🚀 Deployment Quick Start
|
|
280
|
+
|
|
281
|
+
1. Create bot.rb:
|
|
282
|
+
|
|
283
|
+
```ruby
|
|
284
|
+
require 'telegem'
|
|
285
|
+
|
|
286
|
+
bot = Telegem.new(ENV['TELEGRAM_BOT_TOKEN'])
|
|
287
|
+
|
|
288
|
+
bot.command("start") { |ctx| ctx.reply("Bot is running! 🚀") }
|
|
289
|
+
bot.on(:message) { |ctx| ctx.reply("Echo: #{ctx.message.text}") }
|
|
290
|
+
|
|
291
|
+
# For webhook (production)
|
|
292
|
+
if ENV['RACK_ENV'] == 'production'
|
|
293
|
+
Telegem.webhook(bot)
|
|
294
|
+
else
|
|
295
|
+
# For local development
|
|
296
|
+
bot.start_polling
|
|
297
|
+
end
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
2. Create Gemfile:
|
|
301
|
+
|
|
302
|
+
```ruby
|
|
303
|
+
source 'https://rubygems.org'
|
|
304
|
+
gem 'telegem'
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
3. Create config.ru (for webhook):
|
|
308
|
+
|
|
309
|
+
```ruby
|
|
310
|
+
require './bot'
|
|
311
|
+
run ->(env) { [200, {}, ['']] }
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
4. Deploy to Render (Free):
|
|
315
|
+
|
|
316
|
+
1. Push to GitLab/GitHub
|
|
317
|
+
2. Go to render.com
|
|
318
|
+
3. New → Web Service → Connect repo
|
|
319
|
+
4. Set build command: bundle install
|
|
320
|
+
5. Set start command: bundle exec puma -p $PORT
|
|
321
|
+
6. Add env var: TELEGRAM_BOT_TOKEN=your_token
|
|
322
|
+
7. Deploy! 🎉
|
|
323
|
+
|
|
324
|
+
📚 Next Steps
|
|
325
|
+
|
|
326
|
+
Explore Examples:
|
|
327
|
+
|
|
328
|
+
Check the examples/ directory for:
|
|
329
|
+
|
|
330
|
+
· echo_bot.rb - Basic echo bot
|
|
331
|
+
· keyboard_bot.rb - Interactive keyboards
|
|
332
|
+
· scene_bot.rb - Multi-step conversations
|
|
333
|
+
· webhook_bot.rb - Production webhook setup
|
|
334
|
+
|
|
335
|
+
Need Help?
|
|
336
|
+
|
|
337
|
+
· Documentation
|
|
338
|
+
· Issue Tracker
|
|
339
|
+
· Telegram: @sick_phantom
|
|
340
|
+
|
|
341
|
+
🎉 Congratulations!
|
|
342
|
+
|
|
343
|
+
You've built your first Telegram bot with Telegem! Now go build something amazing! 🤖
|
|
344
|
+
|
|
345
|
+
---
|
|
346
|
+
|
|
347
|
+
Telegem v2.0.0 • Made with ❤️ by sick_phantom
|
|
348
|
+
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
|
|
2
|
+
# 🌐 Webhook Setup Guide for Telegem
|
|
3
|
+
|
|
4
|
+
Webhooks are the **recommended way** to run your Telegram bot in production. Instead of your bot constantly asking Telegram for updates ("polling"), Telegram pushes updates directly to your bot's server.
|
|
5
|
+
|
|
6
|
+
**Join the official channel for help and updates!** [](https://t.me/telegem_me2)
|
|
7
|
+
|
|
8
|
+
## 🤔 Polling vs. Webhook: A Quick Analogy
|
|
9
|
+
* **Polling** is like refreshing your email every 5 seconds to check for new messages. It works, but it's inefficient.
|
|
10
|
+
* **Webhook** is like giving Telegram your email address and saying, "Send new messages directly to my inbox as they arrive." It's instant, efficient, and scales beautifully.
|
|
11
|
+
|
|
12
|
+
## 🚀 Quick-Start: One-Line Webhook Setup
|
|
13
|
+
|
|
14
|
+
The easiest way to get a production webhook server running is with our `webhook` helper. It handles **everything**: starting the server, getting your public URL, and configuring Telegram.
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
require 'telegem'
|
|
18
|
+
|
|
19
|
+
bot = Telegem.new("YOUR_BOT_TOKEN")
|
|
20
|
+
|
|
21
|
+
bot.on(:message) do |ctx|
|
|
22
|
+
ctx.reply("Hello from my webhook-powered bot! 🚀")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# This single line does the magic:
|
|
26
|
+
# 1. Starts a production Puma web server
|
|
27
|
+
# 2. Configures a secure webhook endpoint
|
|
28
|
+
# 3. Tells Telegram where to send updates
|
|
29
|
+
server = Telegem.webhook(bot)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
That's it! Your bot is now live with a webhook. The server runs in the background until you call server.stop.
|
|
33
|
+
|
|
34
|
+
🔧 Understanding the Setup (How it Works)
|
|
35
|
+
|
|
36
|
+
Let's break down what the helper does. Here's the equivalent manual setup:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
require 'telegem'
|
|
40
|
+
|
|
41
|
+
bot = Telegem.new("YOUR_BOT_TOKEN")
|
|
42
|
+
bot.on(:message) { |ctx| ctx.reply("Manual setup works!") }
|
|
43
|
+
|
|
44
|
+
# 1. Create a webhook server instance
|
|
45
|
+
server = bot.webhook(port: 3000) # Uses PORT env variable on cloud platforms
|
|
46
|
+
|
|
47
|
+
# 2. Start the server (listens for incoming requests)
|
|
48
|
+
server.run
|
|
49
|
+
|
|
50
|
+
# 3. Tell Telegram your webhook URL with a secret token
|
|
51
|
+
server.set_webhook
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
What's happening behind the scenes?
|
|
55
|
+
|
|
56
|
+
1. Server Creation: Telegem creates a secure, production-ready Puma web server.
|
|
57
|
+
2. Endpoint Setup: It creates a /webhook endpoint that validates incoming requests using a secret_token (for security).
|
|
58
|
+
3. Cloud Detection: It automatically detects if you're running on Render, Railway, Heroku, Fly.io, or Vercel and configures the public URL accordingly.
|
|
59
|
+
4. Telegram Configuration: The set_webhook method registers your bot's public URL with Telegram's servers.
|
|
60
|
+
|
|
61
|
+
☁️ Deployment to Cloud Platforms
|
|
62
|
+
|
|
63
|
+
Telegem's webhook server is optimized for modern cloud platforms. Here's how to deploy:
|
|
64
|
+
|
|
65
|
+
Option A: Deploy to Render (Free Tier Friendly)
|
|
66
|
+
|
|
67
|
+
1. Create these files in your project:
|
|
68
|
+
bot.rb
|
|
69
|
+
```ruby
|
|
70
|
+
require 'telegem'
|
|
71
|
+
|
|
72
|
+
bot = Telegem.new(ENV['TELEGRAM_BOT_TOKEN'])
|
|
73
|
+
|
|
74
|
+
bot.command("start") { |ctx| ctx.reply("Bot is live on Render! ☁️") }
|
|
75
|
+
bot.on(:message) { |ctx| ctx.reply("You said: #{ctx.message.text}") }
|
|
76
|
+
|
|
77
|
+
# This auto-starts the webhook server in production
|
|
78
|
+
Telegem.webhook(bot) if ENV['RACK_ENV'] == 'production'
|
|
79
|
+
```
|
|
80
|
+
Gemfile
|
|
81
|
+
```ruby
|
|
82
|
+
source 'https://rubygems.org'
|
|
83
|
+
gem 'telegem'
|
|
84
|
+
```
|
|
85
|
+
config.ru (required for Render to recognize it as a web service)
|
|
86
|
+
```ruby
|
|
87
|
+
require './bot'
|
|
88
|
+
# The server is started by Telegem.webhook
|
|
89
|
+
run ->(env) { [200, {}, ['Telegem Bot Server']] }
|
|
90
|
+
```
|
|
91
|
+
2. Push to GitLab/GitHub.
|
|
92
|
+
3. On Render.com:
|
|
93
|
+
· Click "New +" → "Web Service"
|
|
94
|
+
· Connect your repository
|
|
95
|
+
· Set the following:
|
|
96
|
+
· Name: telegem-bot (or your choice)
|
|
97
|
+
· Environment: Ruby
|
|
98
|
+
· Build Command: bundle install
|
|
99
|
+
· Start Command: bundle exec puma -p $PORT
|
|
100
|
+
· Click "Advanced" and add an Environment Variable:
|
|
101
|
+
· Key: TELEGRAM_BOT_TOKEN
|
|
102
|
+
· Value: Your bot token from @BotFather
|
|
103
|
+
4. Click "Create Web Service". Your bot will build, deploy, and automatically configure its webhook!
|
|
104
|
+
|
|
105
|
+
Option B: Deploy with Existing Rack Apps (Rails, Sinatra, etc.)
|
|
106
|
+
|
|
107
|
+
If you already have a Rack application, use the Telegem Middleware:
|
|
108
|
+
|
|
109
|
+
```ruby
|
|
110
|
+
# In your config.ru (or similar)
|
|
111
|
+
require 'telegem'
|
|
112
|
+
require './your_main_app'
|
|
113
|
+
|
|
114
|
+
bot = Telegem.new(ENV['TELEGRAM_BOT_TOKEN'])
|
|
115
|
+
bot.on(:message) { |ctx| ctx.reply("Hello from middleware!") }
|
|
116
|
+
|
|
117
|
+
# Insert the middleware into your app's stack
|
|
118
|
+
use Telegem::Webhook::Middleware, bot, secret_token: ENV['TELEGRAM_SECRET_TOKEN']
|
|
119
|
+
|
|
120
|
+
run YourApp
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Then, set the webhook URL manually:
|
|
124
|
+
|
|
125
|
+
```ruby
|
|
126
|
+
# Run this once (e.g., in a script or console)
|
|
127
|
+
bot.set_webhook(
|
|
128
|
+
url: "https://your-app.com/webhook",
|
|
129
|
+
secret_token: ENV['TELEGRAM_SECRET_TOKEN']
|
|
130
|
+
)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
🔐 Security & Configuration
|
|
134
|
+
|
|
135
|
+
Secret Token: Your Webhook's Bouncer
|
|
136
|
+
|
|
137
|
+
The secret_token is a password that ensures only Telegram can talk to your webhook. Telegram sends it in the X-Telegram-Bot-Api-Secret-Token header with every request.
|
|
138
|
+
|
|
139
|
+
· Generate a strong one: Use SecureRandom.hex(32).
|
|
140
|
+
· Set it in production: Pass it when creating the server or via the TELEGRAM_SECRET_TOKEN environment variable.
|
|
141
|
+
· Never commit it to git!
|
|
142
|
+
|
|
143
|
+
Manual Webhook Management
|
|
144
|
+
|
|
145
|
+
Use these methods for fine-grained control:
|
|
146
|
+
|
|
147
|
+
```ruby
|
|
148
|
+
# Check your current webhook status
|
|
149
|
+
info = bot.get_webhook_info
|
|
150
|
+
puts info.url # => "https://your-app.com/webhook"
|
|
151
|
+
|
|
152
|
+
# Delete the webhook (switch back to polling)
|
|
153
|
+
bot.delete_webhook
|
|
154
|
+
|
|
155
|
+
# Set a custom webhook with specific options
|
|
156
|
+
bot.set_webhook(
|
|
157
|
+
url: "https://api.example.com/custom-path",
|
|
158
|
+
secret_token: "your_super_secret_token_here",
|
|
159
|
+
max_connections: 40,
|
|
160
|
+
allowed_updates: ["message", "callback_query"], # Only receive these updates
|
|
161
|
+
drop_pending_updates: true # Clears old updates when setting
|
|
162
|
+
)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
🚨 Common Issues & Troubleshooting
|
|
166
|
+
|
|
167
|
+
· "My bot works locally but not on Render!"
|
|
168
|
+
· Check the Render logs for errors.
|
|
169
|
+
· Ensure the TELEGRAM_BOT_TOKEN environment variable is set correctly in Render's dashboard.
|
|
170
|
+
· Verify your bot's code doesn't have any syntax errors by running ruby bot.rb locally.
|
|
171
|
+
· "I'm getting duplicate messages or missed updates!"
|
|
172
|
+
· You likely have both polling and webhook active. Call bot.delete_webhook if you used start_polling before, or ensure you're not accidentally running start_polling in your webhook deployment.
|
|
173
|
+
· "How do I update my bot's code?"
|
|
174
|
+
· Simply push your changes to git. Your cloud platform (like Render) will automatically rebuild and redeploy. The webhook URL stays the same, so no reconfiguration is needed.
|
|
175
|
+
· "I need to stop the webhook server."
|
|
176
|
+
· Call server.stop on the server object, or send a SIGTERM signal to the process.
|
|
177
|
+
|
|
178
|
+
🧪 Testing Webhooks Locally
|
|
179
|
+
|
|
180
|
+
For local development, you can still use polling. But if you want to test the webhook flow:
|
|
181
|
+
|
|
182
|
+
1. Use a tunneling service like ngrok:
|
|
183
|
+
```bash
|
|
184
|
+
ngrok http 3000
|
|
185
|
+
```
|
|
186
|
+
2. Copy the HTTPS URL ngrok provides (e.g., https://abc123.ngrok.io).
|
|
187
|
+
3. Set your webhook manually:
|
|
188
|
+
```ruby
|
|
189
|
+
bot.set_webhook(url: "https://abc123.ngrok.io/webhook")
|
|
190
|
+
```
|
|
191
|
+
4. Telegram will now send updates to your local machine through the tunnel!
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
Pro Tip: The one-liner Telegem.webhook(bot) is perfect for 95% of use cases. It embodies the Telegem philosophy: powerful functionality with a simple, elegant API.
|
|
196
|
+
|
|
197
|
+
For more help and community discussion, remember to join our official channel: https://img.shields.io/badge/💬-Join_t.me/telegem__me2-blue?style=flat&logo=telegram.
|
|
198
|
+
|
|
199
|
+
Happy Building! The future of your Telegram bot is just a webhook away. 🚀
|