rbender 0.3.0 → 0.4.92
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/bin/rbender +117 -8
- data/lib/rbender.rb +2 -2
- data/lib/rbender/base.rb +178 -154
- data/lib/rbender/config_handler.rb +35 -0
- data/lib/rbender/keyboard.rb +112 -140
- data/lib/rbender/keyboard_inline.rb +3 -3
- data/lib/rbender/methods.rb +129 -0
- data/lib/rbender/mongo_client.rb +4 -6
- data/lib/rbender/state.rb +202 -285
- data/templates/Gemfile +3 -0
- data/templates/config.yaml +9 -0
- data/templates/locales/en.yaml +3 -0
- data/templates/sample.rb +61 -0
- metadata +8 -2
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
|
|
3
|
+
module RBender
|
|
4
|
+
module ConfigHandler
|
|
5
|
+
@@config_path = 'config.yaml'
|
|
6
|
+
CONFIG_NAME = 'config.yaml'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def self.underscore(str)
|
|
10
|
+
str.gsub(/::/, '/').
|
|
11
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
|
12
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
|
13
|
+
tr("-", "_").
|
|
14
|
+
downcase
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.settings
|
|
18
|
+
begin
|
|
19
|
+
@@settings = YAML.load(File.read(@@config_path))
|
|
20
|
+
rescue
|
|
21
|
+
raise "Config file doesn't exists!"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def @@settings.save
|
|
25
|
+
File.write(@@config_path, @@settings.to_yaml)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
@@settings
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.config_path=(path)
|
|
32
|
+
@@config_path = "#{path}/#{CONFIG_NAME}"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
data/lib/rbender/keyboard.rb
CHANGED
|
@@ -1,143 +1,115 @@
|
|
|
1
1
|
class RBender::Keyboard
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
button = @localizations[@switch_groups[button_id][button_num]].dup
|
|
116
|
-
btn_id = @switch_groups[btn_id][button_num]
|
|
117
|
-
else # if it's normal button
|
|
118
|
-
button = @localizations[btn_id].dup
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
if @switchers.member? btn_id # Code for switcher
|
|
122
|
-
switch_num = 0
|
|
123
|
-
if session[:_keyboard_switchers].member? btn_id # If switcher inside session
|
|
124
|
-
switch_num = session[:_keyboard_switchers][btn_id]
|
|
125
|
-
else
|
|
126
|
-
session[:_keyboard_switchers][btn_id] = 0 # Switcher init
|
|
127
|
-
end
|
|
128
|
-
switcher = @switchers[btn_id][switch_num]
|
|
129
|
-
button << " " << switcher unless switcher.empty?
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
buf_line << button
|
|
133
|
-
@markup_final[btn_id] = button
|
|
134
|
-
end
|
|
135
|
-
markup << buf_line
|
|
136
|
-
end
|
|
137
|
-
# puts @keyboard.markup_final
|
|
138
|
-
@markup_tg = Telegram::Bot::Types::ReplyKeyboardMarkup
|
|
139
|
-
.new(keyboard: markup,
|
|
140
|
-
one_time_keyboard: one_time?,
|
|
141
|
-
resize_keyboard: @resize)
|
|
142
|
-
end
|
|
3
|
+
attr_accessor :markup,
|
|
4
|
+
:localizations,
|
|
5
|
+
:actions,
|
|
6
|
+
:response,
|
|
7
|
+
:switchers,
|
|
8
|
+
:markup_tg,
|
|
9
|
+
:markup_final,
|
|
10
|
+
:switch_groups,
|
|
11
|
+
:session
|
|
12
|
+
|
|
13
|
+
def initialize
|
|
14
|
+
@actions = {}
|
|
15
|
+
@markup = []
|
|
16
|
+
@localizations = {}
|
|
17
|
+
@markup_tg = nil
|
|
18
|
+
@markup_final = {}
|
|
19
|
+
@button_switch_group = {}
|
|
20
|
+
@one_time = false
|
|
21
|
+
@session = nil
|
|
22
|
+
@hide_on_action = false
|
|
23
|
+
@resize = false
|
|
24
|
+
@force_invoke = false
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def hide_on_action
|
|
28
|
+
@hide_on_action = true
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def force_invoked?
|
|
32
|
+
@force_invoke
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Adds button to the keyboard
|
|
36
|
+
#
|
|
37
|
+
# Example:
|
|
38
|
+
# button :btn_test, "Test button" do
|
|
39
|
+
# some_actions
|
|
40
|
+
# end
|
|
41
|
+
#
|
|
42
|
+
# button :btn_with_loc, {ru: "Кнопка", en: "Button"} do
|
|
43
|
+
# some_actions
|
|
44
|
+
# end
|
|
45
|
+
|
|
46
|
+
def button(id, localizations, &action)
|
|
47
|
+
@actions[id] = action
|
|
48
|
+
@localizations[id] = localizations
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Checks keyboard one time or not
|
|
52
|
+
def one_time?
|
|
53
|
+
@one_time
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# makes a keyboard one time
|
|
57
|
+
def one_time
|
|
58
|
+
@one_time = true
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Add a line to markup
|
|
62
|
+
def add_line (*buttons)
|
|
63
|
+
@markup += [buttons]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Set response when keyboard is invoked
|
|
67
|
+
def set_response(new_response)
|
|
68
|
+
@response = new_response
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Adds switcher to buttons
|
|
72
|
+
def switcher(id, *switches)
|
|
73
|
+
@switchers[id] = switches
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Adds switch group
|
|
77
|
+
def switch_group(group_name, *buttons)
|
|
78
|
+
@switch_groups[group_name] = buttons
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Resize telegram buttons
|
|
82
|
+
def resize
|
|
83
|
+
@resize = true
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Adds button switch group
|
|
87
|
+
def button_switch_group(button_id)
|
|
88
|
+
@switch_groups.each do |group, buttons|
|
|
89
|
+
if buttons.include? button_id
|
|
90
|
+
return group
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
nil
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Method to initialize a keyboard
|
|
97
|
+
#
|
|
98
|
+
def build(session)
|
|
99
|
+
markup = []
|
|
100
|
+
@markup.each do |line|
|
|
101
|
+
buf_line = []
|
|
102
|
+
line.each do |button_id|
|
|
103
|
+
button = @localizations[button_id].dup
|
|
104
|
+
|
|
105
|
+
buf_line << button
|
|
106
|
+
@markup_final[btn_id] = button
|
|
107
|
+
end
|
|
108
|
+
markup << buf_line
|
|
109
|
+
end
|
|
110
|
+
@markup_tg = Telegram::Bot::Types::ReplyKeyboardMarkup
|
|
111
|
+
.new(keyboard: markup,
|
|
112
|
+
one_time_keyboard: one_time?,
|
|
113
|
+
resize_keyboard: @resize)
|
|
114
|
+
end
|
|
143
115
|
end
|
|
@@ -45,13 +45,13 @@ class RBender::KeyboardInline
|
|
|
45
45
|
@markup += [buttons]
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
-
def
|
|
48
|
+
def invoke
|
|
49
49
|
instance_eval(&@keyboard_block)
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
-
def
|
|
52
|
+
def build
|
|
53
53
|
@markup = []
|
|
54
|
-
|
|
54
|
+
invoke()
|
|
55
55
|
|
|
56
56
|
buttons = {}
|
|
57
57
|
buttons.merge! @buttons_callback
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
class RBender::Methods
|
|
2
|
+
|
|
3
|
+
def initialize(message, api, session)
|
|
4
|
+
@message = message
|
|
5
|
+
@api = api
|
|
6
|
+
@session = session
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
#--------------
|
|
10
|
+
# User methods
|
|
11
|
+
#--------------
|
|
12
|
+
|
|
13
|
+
# Set message user gets while keyboard has invoked
|
|
14
|
+
def set_response(new_response)
|
|
15
|
+
@keyboard.set_response(new_response)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Returns session hash
|
|
19
|
+
def session
|
|
20
|
+
@session
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Returns message object
|
|
24
|
+
def message
|
|
25
|
+
@message
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Returns Inline keyboard object by name
|
|
29
|
+
def inline_markup(name)
|
|
30
|
+
raise "Keyboard #{name} doesn't exists!" unless @inline_keyboards.member? name
|
|
31
|
+
keyboard = @inline_keyboards[name]
|
|
32
|
+
keyboard.instance_eval(&@helpers_block) unless @helpers_block.nil?
|
|
33
|
+
keyboard.build
|
|
34
|
+
keyboard.markup_tg
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def switch(state_to)
|
|
38
|
+
@session[:state_stack].push(@session[:state])
|
|
39
|
+
@session[:state] = state_to
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def switch_prev
|
|
43
|
+
@session[:state] = @session[:state_stack].pop
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
#--------------
|
|
48
|
+
# API METHODS
|
|
49
|
+
#--------------
|
|
50
|
+
# Hides inline keyboard
|
|
51
|
+
# Must be called from any inline keyboard state
|
|
52
|
+
def hide_inline
|
|
53
|
+
edit_message_reply_markup
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Hides keyboard's markup.
|
|
57
|
+
def hide_keyboard
|
|
58
|
+
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
#
|
|
62
|
+
# @param text [String] string
|
|
63
|
+
#
|
|
64
|
+
def answer_callback_query(text: nil,
|
|
65
|
+
show_alert: nil)
|
|
66
|
+
begin
|
|
67
|
+
@api.answer_callback_query callback_query_id: @message.id,
|
|
68
|
+
text: text,
|
|
69
|
+
show_alert: show_alert
|
|
70
|
+
rescue
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def send_message(text:,
|
|
75
|
+
chat_id: @message.from.id,
|
|
76
|
+
parse_mode: nil,
|
|
77
|
+
disable_web_page_preview: nil,
|
|
78
|
+
disable_notification: nil,
|
|
79
|
+
reply_to_message_id: nil,
|
|
80
|
+
reply_markup: nil)
|
|
81
|
+
|
|
82
|
+
if text.strip.empty?
|
|
83
|
+
raise "A text can't be empty or consists of space symbols only"
|
|
84
|
+
end
|
|
85
|
+
@api.send_message chat_id: chat_id,
|
|
86
|
+
text: text,
|
|
87
|
+
disable_web_page_preview: disable_web_page_preview,
|
|
88
|
+
disable_notification: disable_notification,
|
|
89
|
+
reply_to_message_id: reply_to_message_id,
|
|
90
|
+
parse_mode: parse_mode,
|
|
91
|
+
reply_markup: reply_markup
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def edit_message_text(inline_message_id: nil,
|
|
95
|
+
text:,
|
|
96
|
+
message_id: @message.message.message_id,
|
|
97
|
+
parse_mode: nil,
|
|
98
|
+
disable_web_page_preview: nil,
|
|
99
|
+
reply_markup: nil)
|
|
100
|
+
begin
|
|
101
|
+
@api.edit_message_text chat_id: @message.from.id,
|
|
102
|
+
message_id: message_id,
|
|
103
|
+
text: text,
|
|
104
|
+
inline_message_id: inline_message_id,
|
|
105
|
+
parse_mode: parse_mode,
|
|
106
|
+
disable_web_page_preview: disable_web_page_preview,
|
|
107
|
+
reply_markup: reply_markup
|
|
108
|
+
rescue
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def edit_message_reply_markup(chat_id: @message.from.id,
|
|
113
|
+
message_id: @message.message.message_id,
|
|
114
|
+
inline_message_id: nil,
|
|
115
|
+
reply_markup: nil)
|
|
116
|
+
begin
|
|
117
|
+
@api.edit_message_reply_markup chat_id: chat_id,
|
|
118
|
+
message_id: message_id,
|
|
119
|
+
inline_message_id: inline_message_id,
|
|
120
|
+
reply_markup: reply_markup
|
|
121
|
+
rescue
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def get_file(file_id:)
|
|
126
|
+
@api.get_file file_id: file_id
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
data/lib/rbender/mongo_client.rb
CHANGED
|
@@ -10,15 +10,13 @@ class RBender::MongoClient
|
|
|
10
10
|
instance.mongo_client
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
def self.setup(bot_name,
|
|
14
|
-
instance.setup(bot_name,
|
|
13
|
+
def self.setup(bot_name, mongo_string)
|
|
14
|
+
instance.setup(bot_name, mongo_string)
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def setup(bot_name,
|
|
17
|
+
def setup(bot_name, mongo_string)
|
|
18
18
|
@bot_name = bot_name
|
|
19
|
-
@
|
|
20
|
-
@mongo_server_port = mongo_server_port
|
|
21
|
-
@mongo_client = Mongo::Client.new(["#{mongo_server_ip}:#{mongo_server_port}"], :database => bot_name)
|
|
19
|
+
@mongo_client = Mongo::Client.new(mongo_string, :database => bot_name)
|
|
22
20
|
|
|
23
21
|
Mongo::Logger.logger.level = ::Logger::FATAL
|
|
24
22
|
end
|