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
data/lib/markup/keyboard.rb
CHANGED
|
@@ -12,37 +12,43 @@ module Telegem
|
|
|
12
12
|
}.merge(options)
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
# Create from array
|
|
16
15
|
def self.[](*rows)
|
|
17
16
|
new(rows)
|
|
18
17
|
end
|
|
19
18
|
|
|
20
|
-
# Builder pattern
|
|
21
19
|
def self.build(&block)
|
|
22
20
|
builder = Builder.new
|
|
23
21
|
builder.instance_eval(&block) if block_given?
|
|
24
22
|
builder.keyboard
|
|
25
23
|
end
|
|
26
24
|
|
|
27
|
-
# Add a row
|
|
28
25
|
def row(*buttons)
|
|
29
26
|
@buttons << buttons.flatten
|
|
30
27
|
self
|
|
31
28
|
end
|
|
32
29
|
|
|
33
|
-
# Add a button
|
|
34
30
|
def button(text, **options)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if last_row.is_a?(Array)
|
|
38
|
-
last_row << { text: text }.merge(options)
|
|
39
|
-
else
|
|
31
|
+
if @buttons.empty? || !@buttons.last.is_a?(Array)
|
|
40
32
|
@buttons << [{ text: text }.merge(options)]
|
|
33
|
+
else
|
|
34
|
+
@buttons.last << { text: text }.merge(options)
|
|
41
35
|
end
|
|
42
36
|
self
|
|
43
37
|
end
|
|
44
38
|
|
|
45
|
-
|
|
39
|
+
def request_contact(text)
|
|
40
|
+
button(text, request_contact: true)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def request_location(text)
|
|
44
|
+
button(text, request_location: true)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def request_poll(text, type = nil)
|
|
48
|
+
opts = type ? { request_poll: { type: type } } : { request_poll: {} }
|
|
49
|
+
button(text, opts)
|
|
50
|
+
end
|
|
51
|
+
|
|
46
52
|
def resize(resize = true)
|
|
47
53
|
@options[:resize_keyboard] = resize
|
|
48
54
|
self
|
|
@@ -58,7 +64,6 @@ module Telegem
|
|
|
58
64
|
self
|
|
59
65
|
end
|
|
60
66
|
|
|
61
|
-
# Convert to Telegram format
|
|
62
67
|
def to_h
|
|
63
68
|
{
|
|
64
69
|
keyboard: @buttons.map { |row| row.is_a?(Array) ? row : [row] },
|
|
@@ -70,7 +75,6 @@ module Telegem
|
|
|
70
75
|
to_h.to_json(*args)
|
|
71
76
|
end
|
|
72
77
|
|
|
73
|
-
# Remove keyboard
|
|
74
78
|
def self.remove(selective: false)
|
|
75
79
|
{
|
|
76
80
|
remove_keyboard: true,
|
|
@@ -78,7 +82,6 @@ module Telegem
|
|
|
78
82
|
}
|
|
79
83
|
end
|
|
80
84
|
|
|
81
|
-
# Force reply
|
|
82
85
|
def self.force_reply(selective: false, input_field_placeholder: nil)
|
|
83
86
|
markup = {
|
|
84
87
|
force_reply: true,
|
|
@@ -96,67 +99,58 @@ module Telegem
|
|
|
96
99
|
@buttons = buttons
|
|
97
100
|
end
|
|
98
101
|
|
|
99
|
-
# Create from array
|
|
100
102
|
def self.[](*rows)
|
|
101
103
|
new(rows)
|
|
102
104
|
end
|
|
103
105
|
|
|
104
|
-
# Builder pattern
|
|
105
106
|
def self.build(&block)
|
|
106
107
|
builder = InlineBuilder.new
|
|
107
108
|
builder.instance_eval(&block) if block_given?
|
|
108
109
|
builder.keyboard
|
|
109
110
|
end
|
|
110
111
|
|
|
111
|
-
# Add a row
|
|
112
112
|
def row(*buttons)
|
|
113
113
|
@buttons << buttons.flatten
|
|
114
114
|
self
|
|
115
115
|
end
|
|
116
116
|
|
|
117
|
-
# Add a button
|
|
118
117
|
def button(text, **options)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if last_row.is_a?(Array)
|
|
122
|
-
last_row << { text: text }.merge(options)
|
|
123
|
-
else
|
|
118
|
+
if @buttons.empty? || !@buttons.last.is_a?(Array)
|
|
124
119
|
@buttons << [{ text: text }.merge(options)]
|
|
120
|
+
else
|
|
121
|
+
@buttons.last << { text: text }.merge(options)
|
|
125
122
|
end
|
|
126
123
|
self
|
|
127
124
|
end
|
|
128
125
|
|
|
129
|
-
# URL button
|
|
130
126
|
def url(text, url)
|
|
131
127
|
button(text, url: url)
|
|
132
128
|
end
|
|
133
129
|
|
|
134
|
-
# Callback button
|
|
135
130
|
def callback(text, data)
|
|
136
131
|
button(text, callback_data: data)
|
|
137
132
|
end
|
|
138
133
|
|
|
139
|
-
# Web app button
|
|
140
134
|
def web_app(text, url)
|
|
141
135
|
button(text, web_app: { url: url })
|
|
142
136
|
end
|
|
143
137
|
|
|
144
|
-
# Login button
|
|
145
138
|
def login(text, url, **options)
|
|
146
139
|
button(text, login_url: { url: url, **options })
|
|
147
140
|
end
|
|
148
141
|
|
|
149
|
-
# Switch inline query button
|
|
150
142
|
def switch_inline(text, query = "")
|
|
151
143
|
button(text, switch_inline_query: query)
|
|
152
144
|
end
|
|
153
145
|
|
|
154
|
-
# Switch inline query current chat button
|
|
155
146
|
def switch_inline_current(text, query = "")
|
|
156
147
|
button(text, switch_inline_query_current_chat: query)
|
|
157
148
|
end
|
|
158
149
|
|
|
159
|
-
|
|
150
|
+
def pay(text)
|
|
151
|
+
button(text, pay: true)
|
|
152
|
+
end
|
|
153
|
+
|
|
160
154
|
def to_h
|
|
161
155
|
{
|
|
162
156
|
inline_keyboard: @buttons.map { |row| row.is_a?(Array) ? row : [row] }
|
|
@@ -168,7 +162,6 @@ module Telegem
|
|
|
168
162
|
end
|
|
169
163
|
end
|
|
170
164
|
|
|
171
|
-
# Builder DSL for keyboards
|
|
172
165
|
class Builder
|
|
173
166
|
attr_reader :keyboard
|
|
174
167
|
|
|
@@ -181,8 +174,10 @@ module Telegem
|
|
|
181
174
|
sub_builder = Builder.new
|
|
182
175
|
sub_builder.instance_eval(&block)
|
|
183
176
|
@keyboard.row(*sub_builder.keyboard.buttons.flatten(1))
|
|
184
|
-
|
|
177
|
+
elsif buttons.any?
|
|
185
178
|
@keyboard.row(*buttons)
|
|
179
|
+
else
|
|
180
|
+
@keyboard.row
|
|
186
181
|
end
|
|
187
182
|
self
|
|
188
183
|
end
|
|
@@ -192,8 +187,23 @@ module Telegem
|
|
|
192
187
|
self
|
|
193
188
|
end
|
|
194
189
|
|
|
190
|
+
def request_contact(text)
|
|
191
|
+
@keyboard.request_contact(text)
|
|
192
|
+
self
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def request_location(text)
|
|
196
|
+
@keyboard.request_location(text)
|
|
197
|
+
self
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def request_poll(text, type = nil)
|
|
201
|
+
@keyboard.request_poll(text, type)
|
|
202
|
+
self
|
|
203
|
+
end
|
|
204
|
+
|
|
195
205
|
def method_missing(name, *args, &block)
|
|
196
|
-
if @keyboard.respond_to?(name)
|
|
206
|
+
if @keyboard && @keyboard.respond_to?(name)
|
|
197
207
|
@keyboard.send(name, *args, &block)
|
|
198
208
|
else
|
|
199
209
|
super
|
|
@@ -201,11 +211,10 @@ module Telegem
|
|
|
201
211
|
end
|
|
202
212
|
|
|
203
213
|
def respond_to_missing?(name, include_private = false)
|
|
204
|
-
@keyboard.respond_to?(name) || super
|
|
214
|
+
@keyboard && @keyboard.respond_to?(name) || super
|
|
205
215
|
end
|
|
206
216
|
end
|
|
207
217
|
|
|
208
|
-
# Builder DSL for inline keyboards
|
|
209
218
|
class InlineBuilder
|
|
210
219
|
attr_reader :keyboard
|
|
211
220
|
|
|
@@ -218,8 +227,10 @@ module Telegem
|
|
|
218
227
|
sub_builder = InlineBuilder.new
|
|
219
228
|
sub_builder.instance_eval(&block)
|
|
220
229
|
@keyboard.row(*sub_builder.keyboard.buttons.flatten(1))
|
|
221
|
-
|
|
230
|
+
elsif buttons.any?
|
|
222
231
|
@keyboard.row(*buttons)
|
|
232
|
+
else
|
|
233
|
+
@keyboard.row
|
|
223
234
|
end
|
|
224
235
|
self
|
|
225
236
|
end
|
|
@@ -259,8 +270,13 @@ module Telegem
|
|
|
259
270
|
self
|
|
260
271
|
end
|
|
261
272
|
|
|
273
|
+
def pay(text)
|
|
274
|
+
@keyboard.pay(text)
|
|
275
|
+
self
|
|
276
|
+
end
|
|
277
|
+
|
|
262
278
|
def method_missing(name, *args, &block)
|
|
263
|
-
if @keyboard.respond_to?(name)
|
|
279
|
+
if @keyboard && @keyboard.respond_to?(name)
|
|
264
280
|
@keyboard.send(name, *args, &block)
|
|
265
281
|
else
|
|
266
282
|
super
|
|
@@ -268,11 +284,10 @@ module Telegem
|
|
|
268
284
|
end
|
|
269
285
|
|
|
270
286
|
def respond_to_missing?(name, include_private = false)
|
|
271
|
-
@keyboard.respond_to?(name) || super
|
|
287
|
+
@keyboard && @keyboard.respond_to?(name) || super
|
|
272
288
|
end
|
|
273
289
|
end
|
|
274
290
|
|
|
275
|
-
# Shortcuts for common use
|
|
276
291
|
class << self
|
|
277
292
|
def keyboard(&block)
|
|
278
293
|
Keyboard.build(&block)
|
data/lib/telegem.rb
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# lib/telegem.rb - MAIN ENTRY POINT
|
|
2
2
|
require 'logger'
|
|
3
3
|
require 'json'
|
|
4
|
+
|
|
4
5
|
module Telegem
|
|
5
|
-
VERSION = "
|
|
6
|
+
VERSION = "2.0.0".freeze
|
|
6
7
|
end
|
|
7
8
|
|
|
8
9
|
# Load core components
|
|
@@ -15,7 +16,7 @@ require_relative 'core/scene'
|
|
|
15
16
|
require_relative 'session/middleware'
|
|
16
17
|
require_relative 'session/memory_store'
|
|
17
18
|
require_relative 'markup/keyboard'
|
|
18
|
-
|
|
19
|
+
# Webhook is loaded lazily when needed
|
|
19
20
|
|
|
20
21
|
module Telegem
|
|
21
22
|
# Main entry point: Telegem.new(token)
|
|
@@ -47,6 +48,12 @@ module Telegem
|
|
|
47
48
|
VERSION
|
|
48
49
|
end
|
|
49
50
|
|
|
51
|
+
# Quick webhook setup
|
|
52
|
+
def self.webhook(bot, **options)
|
|
53
|
+
require_relative 'webhook/server'
|
|
54
|
+
Webhook::Server.setup(bot, **options)
|
|
55
|
+
end
|
|
56
|
+
|
|
50
57
|
# Framework information
|
|
51
58
|
def self.info
|
|
52
59
|
<<~INFO
|
|
@@ -61,13 +68,16 @@ module Telegem
|
|
|
61
68
|
• Webhook and polling support
|
|
62
69
|
• Built-in session management
|
|
63
70
|
• Fluent keyboard DSL
|
|
71
|
+
• Cloud-ready webhook server
|
|
64
72
|
|
|
65
73
|
Website: https://gitlab.com/ruby-telegem/telegem
|
|
66
74
|
INFO
|
|
67
75
|
end
|
|
68
76
|
end
|
|
69
77
|
|
|
70
|
-
#
|
|
71
|
-
|
|
72
|
-
Telegem
|
|
78
|
+
# Optional global shortcut (enabled by env var)
|
|
79
|
+
if ENV['TELEGEM_GLOBAL'] == 'true'
|
|
80
|
+
def Telegem(token, **options)
|
|
81
|
+
Telegem.new(token, **options)
|
|
82
|
+
end
|
|
73
83
|
end
|