/
reply_keyboard_markup.cr
107 lines (77 loc) · 2.71 KB
/
reply_keyboard_markup.cr
1
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
require "json"
require "../keyboard_builder"
module Tourmaline
alias Button = KeyboardButton | InlineKeyboardButton
class KeyboardButton
include JSON::Serializable
property text : String
property request_contact : Bool = false
property request_location : Bool = false
property request_poll : KeyboardButtonPollType?
property web_app : WebAppInfo?
def initialize(@text : String, @request_contact = false, @request_location = false, @request_poll = nil, @web_app = nil)
end
end
# This object represents type of a poll, which is allowed to be
# created and sent when the corresponding button is pressed.
class KeyboardButtonPollType
include JSON::Serializable
@[JSON::Field(converter: Tourmaline::Poll::PollTypeConverter)]
property type : Poll::Type
def initialize(@type)
end
end
class ReplyKeyboardMarkup
include JSON::Serializable
property keyboard : Array(Array(KeyboardButton))
property resize_keyboard : Bool = false
property one_time_keyboard : Bool = false
property input_field_placeholder : String? = nil
property selective : Bool = false
def initialize(@keyboard = [] of Array(KeyboardButton), @resize_keyboard = false, @one_time_keyboard = false, @input_field_placeholder = nil, @selective = false)
end
def <<(row : Int32, key : KeyboardButton)
keyboard[row] << key
end
def <<(keys : Array(KeyboardButton))
keyboard << keys
end
def swap_row(row : Int32, keys : Array(KeyboardButton))
keyboard[row] = keys
end
def delete_row(row)
keyboard[row].delete
end
def size
keyboard.size
end
def self.build(*args, columns = nil, **options)
builder = Builder.new(*args, **options)
with builder yield builder
builder.keyboard(columns)
end
class Builder < KeyboardBuilder(Tourmaline::KeyboardButton, Tourmaline::ReplyKeyboardMarkup)
def keyboard(columns = nil) : G
buttons = KeyboardBuilder(T, G).build_keyboard(@keyboard, columns: columns || 1)
ReplyKeyboardMarkup.new(buttons, @resize, @one_time, @input_field_placeholder, @selective)
end
def text_button(text)
button(text)
end
def contact_request_button(text)
button(text, request_contact: true)
end
def location_request_button(text)
button(text, request_location: true)
end
def poll_request_button(text, type : Poll::Type)
type = KeyboardButtonPollType.new(type)
button(text, request_poll: type)
end
def web_app_button(app : String | WebAppInfo)
web_app = app.is_a?(String) ? WebAppInfo.new(app) : app
button(url, web_app: web_app)
end
end
end
end