Class: XNM::Telegram::KeyboardLayout

Inherits:
Object
  • Object
show all
Defined in:
lib/xnm/telegram/KeyboardLayout.rb

Instance Method Summary collapse

Constructor Details

#initialize(start_config = nil) ⇒ KeyboardLayout

Returns a new instance of KeyboardLayout.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/xnm/telegram/KeyboardLayout.rb', line 5

def initialize(start_config = nil)
	@rows = Array.new()

	if start_config.is_a? Hash
		start_config = [start_config]
	end

	if start_config.is_a? Array
		start_config.each_index do |i|
			j = 0
			start_config[i].each do |k, v|
				set_button(k, v, r: i, c: j)
				j += 1
			end
		end
	end
end

Instance Method Details

#[](i) ⇒ Object



23
24
25
# File 'lib/xnm/telegram/KeyboardLayout.rb', line 23

def [](i)
	@rows[i]
end

#ilk_reply_markupObject



41
42
43
# File 'lib/xnm/telegram/KeyboardLayout.rb', line 41

def ilk_reply_markup()
	return { inline_keyboard: to_ilk_layout }
end

#set_button(name, command = nil, c: 0, r: 0) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/xnm/telegram/KeyboardLayout.rb', line 27

def set_button(name, command = nil, c: 0, r: 0)
	command ||= name

	@rows[r] ||= [];
	out_button = {text: name}
	if command =~ /^(http|tg)/
		out_button[:url] = command
	else
		out_button[:callback_data] = command
	end

	@rows[r][c] = out_button
end

#to_ilk_layoutObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xnm/telegram/KeyboardLayout.rb', line 45

def to_ilk_layout()
	out_data = [];

	@rows.each do |row|
		next if row.nil?
		next if row.empty?

		formatted_row = []

		row.each do |button|
			next if button.nil?

			formatted_row << button
		end

		out_data << formatted_row unless formatted_row.empty?
	end

	out_data
end