Class: MaxBotApi::Builders::MessageBuilder
- Inherits:
-
Object
- Object
- MaxBotApi::Builders::MessageBuilder
- Defined in:
- lib/max_bot_api/builders/message_builder.rb
Overview
Builder for message payloads.
Constant Summary collapse
- FORMAT_HTML =
'html'- FORMAT_MARKDOWN =
'markdown'
Instance Attribute Summary collapse
-
#chat_id ⇒ Object
readonly
Returns the value of attribute chat_id.
-
#disable_link_preview ⇒ Object
readonly
Returns the value of attribute disable_link_preview.
-
#reset ⇒ Object
readonly
Returns the value of attribute reset.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Class Method Summary collapse
-
.from_hash(hash) ⇒ MessageBuilder
Create a builder from an existing hash payload.
Instance Method Summary collapse
-
#add_audio(uploaded_info) ⇒ Object
Attach audio payload.
-
#add_contact(name:, contact_id:, vcf_info: nil, vcf_phone: nil) ⇒ Object
Attach a contact card.
-
#add_file(uploaded_info) ⇒ Object
Attach file payload.
-
#add_keyboard(keyboard) ⇒ Object
Attach a keyboard.
-
#add_location(lat, lon) ⇒ Object
Attach a location.
-
#add_markup(user_id, from, length) ⇒ Object
Add user mention markup.
-
#add_photo(photo_tokens) ⇒ Object
Attach a photo payload.
-
#add_photo_by_token(token) ⇒ Object
Attach a photo payload by token.
-
#add_sticker(code) ⇒ Object
Attach a sticker.
-
#add_video(uploaded_info) ⇒ Object
Attach video payload.
-
#bot_token ⇒ Object
Return bot token used for reset mode.
-
#disable_link_preview? ⇒ Boolean
Whether link previews are disabled for message delivery.
-
#initialize ⇒ MessageBuilder
constructor
Initialize a new builder with empty payload.
-
#phone_numbers ⇒ Object
Return phone numbers used for notify/exists.
-
#reply(text, reply_message) ⇒ Object
Reply to a message hash with inferred recipient.
-
#reset? ⇒ Boolean
Whether reset mode is enabled.
-
#set_bot_token(token) ⇒ Object
Set bot token for reset mode.
-
#set_chat(chat_id) ⇒ Object
Set recipient chat ID.
-
#set_disable_link_preview(disable_link_preview) ⇒ Object
Toggle link previews in sent messages.
-
#set_format(format) ⇒ Object
Set message format (markdown/html).
-
#set_notify(notify) ⇒ Object
Toggle notification flag.
-
#set_phone_numbers(numbers) ⇒ Object
Set phone numbers for notify/exists.
-
#set_reply(text, message_id) ⇒ Object
Set reply with explicit message ID.
-
#set_reset(reset) ⇒ Object
Toggle reset mode (skip Authorization header).
-
#set_text(text) ⇒ Object
Set message text.
-
#set_user(user_id) ⇒ Object
Set recipient user ID.
-
#to_h ⇒ Object
Return the message payload hash.
Constructor Details
#initialize ⇒ MessageBuilder
Initialize a new builder with empty payload.
32 33 34 35 36 37 38 39 40 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 32 def initialize @user_id = nil @chat_id = nil @reset = false @disable_link_preview = false @message = { attachments: [] } end |
Instance Attribute Details
#chat_id ⇒ Object (readonly)
Returns the value of attribute chat_id.
10 11 12 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 10 def chat_id @chat_id end |
#disable_link_preview ⇒ Object (readonly)
Returns the value of attribute disable_link_preview.
10 11 12 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 10 def disable_link_preview @disable_link_preview end |
#reset ⇒ Object (readonly)
Returns the value of attribute reset.
10 11 12 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 10 def reset @reset end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
10 11 12 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 10 def user_id @user_id end |
Class Method Details
.from_hash(hash) ⇒ MessageBuilder
Create a builder from an existing hash payload.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 15 def self.from_hash(hash) builder = new return builder if hash.nil? builder.set_user(hash[:user_id] || hash['user_id']) if hash.key?(:user_id) || hash.key?('user_id') builder.set_chat(hash[:chat_id] || hash['chat_id']) if hash.key?(:chat_id) || hash.key?('chat_id') builder.set_reset(hash[:reset] || hash['reset']) if hash.key?(:reset) || hash.key?('reset') if hash.key?(:disable_link_preview) || hash.key?('disable_link_preview') builder.set_disable_link_preview(hash[:disable_link_preview] || hash['disable_link_preview']) end payload = hash[:message] || hash['message'] || hash builder.apply_payload(payload) builder end |
Instance Method Details
#add_audio(uploaded_info) ⇒ Object
Attach audio payload.
128 129 130 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 128 def add_audio(uploaded_info) (type: 'audio', payload: uploaded_info) end |
#add_contact(name:, contact_id:, vcf_info: nil, vcf_phone: nil) ⇒ Object
Attach a contact card.
148 149 150 151 152 153 154 155 156 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 148 def add_contact(name:, contact_id:, vcf_info: nil, vcf_phone: nil) payload = { name: name, contact_id: contact_id, vcf_info: vcf_info, vcf_phone: vcf_phone }.compact (type: 'contact', payload: payload) end |
#add_file(uploaded_info) ⇒ Object
Attach file payload.
138 139 140 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 138 def add_file(uploaded_info) (type: 'file', payload: uploaded_info) end |
#add_keyboard(keyboard) ⇒ Object
Attach a keyboard.
111 112 113 114 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 111 def add_keyboard(keyboard) payload = keyboard.is_a?(Builders::KeyboardBuilder) ? keyboard.build : keyboard (type: 'inline_keyboard', payload: payload) end |
#add_location(lat, lon) ⇒ Object
Attach a location.
143 144 145 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 143 def add_location(lat, lon) (type: 'location', latitude: lat, longitude: lon) end |
#add_markup(user_id, from, length) ⇒ Object
Add user mention markup.
104 105 106 107 108 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 104 def add_markup(user_id, from, length) @message[:markup] ||= [] @message[:markup] << { user_id: user_id, from: from, length: length, type: 'user_mention' } self end |
#add_photo(photo_tokens) ⇒ Object
Attach a photo payload.
117 118 119 120 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 117 def add_photo(photo_tokens) payload = photo_tokens.is_a?(Hash) ? photo_tokens : { photos: photo_tokens } (type: 'image', payload: { photos: payload[:photos] || payload['photos'] }) end |
#add_photo_by_token(token) ⇒ Object
Attach a photo payload by token.
123 124 125 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 123 def add_photo_by_token(token) (type: 'image', payload: { token: token }) end |
#add_sticker(code) ⇒ Object
Attach a sticker.
159 160 161 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 159 def add_sticker(code) (type: 'sticker', payload: { code: code }) end |
#add_video(uploaded_info) ⇒ Object
Attach video payload.
133 134 135 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 133 def add_video(uploaded_info) (type: 'video', payload: uploaded_info) end |
#bot_token ⇒ Object
Return bot token used for reset mode.
176 177 178 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 176 def bot_token @message[:bot_token] end |
#disable_link_preview? ⇒ Boolean
Whether link previews are disabled for message delivery.
191 192 193 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 191 def disable_link_preview? @disable_link_preview end |
#phone_numbers ⇒ Object
Return phone numbers used for notify/exists.
181 182 183 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 181 def phone_numbers @message[:phone_numbers] end |
#reply(text, reply_message) ⇒ Object
Reply to a message hash with inferred recipient.
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 92 def reply(text, ) recipient = [:recipient] || ['recipient'] || {} set_user(recipient[:user_id] || recipient['user_id']) if recipient[:user_id] || recipient['user_id'] set_chat(recipient[:chat_id] || recipient['chat_id']) if recipient[:chat_id] || recipient['chat_id'] body = [:body] || ['body'] || {} @message[:text] = text @message[:link] = { type: 'reply', mid: body[:mid] || body['mid'] } self end |
#reset? ⇒ Boolean
Whether reset mode is enabled.
186 187 188 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 186 def reset? @reset end |
#set_bot_token(token) ⇒ Object
Set bot token for reset mode.
164 165 166 167 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 164 def set_bot_token(token) @message[:bot_token] = token self end |
#set_chat(chat_id) ⇒ Object
Set recipient chat ID.
49 50 51 52 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 49 def set_chat(chat_id) @chat_id = chat_id self end |
#set_disable_link_preview(disable_link_preview) ⇒ Object
Toggle link previews in sent messages.
61 62 63 64 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 61 def set_disable_link_preview(disable_link_preview) @disable_link_preview = !!disable_link_preview self end |
#set_format(format) ⇒ Object
Set message format (markdown/html).
73 74 75 76 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 73 def set_format(format) @message[:format] = format self end |
#set_notify(notify) ⇒ Object
Toggle notification flag.
79 80 81 82 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 79 def set_notify(notify) @message[:notify] = notify self end |
#set_phone_numbers(numbers) ⇒ Object
Set phone numbers for notify/exists.
170 171 172 173 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 170 def set_phone_numbers(numbers) @message[:phone_numbers] = Array(numbers) self end |
#set_reply(text, message_id) ⇒ Object
Set reply with explicit message ID.
85 86 87 88 89 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 85 def set_reply(text, ) @message[:text] = text @message[:link] = { type: 'reply', mid: } self end |
#set_reset(reset) ⇒ Object
Toggle reset mode (skip Authorization header).
55 56 57 58 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 55 def set_reset(reset) @reset = !!reset self end |
#set_text(text) ⇒ Object
Set message text.
67 68 69 70 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 67 def set_text(text) @message[:text] = text self end |
#set_user(user_id) ⇒ Object
Set recipient user ID.
43 44 45 46 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 43 def set_user(user_id) @user_id = user_id self end |
#to_h ⇒ Object
Return the message payload hash.
196 197 198 |
# File 'lib/max_bot_api/builders/message_builder.rb', line 196 def to_h @message end |