Class: MaxBotApi::Builders::MessageBuilder

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessageBuilder

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_idObject (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

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

#resetObject (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_idObject (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.

Parameters:

  • hash (Hash)

Returns:



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)
  add_attachment(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
  add_attachment(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)
  add_attachment(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
  add_attachment(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)
  add_attachment(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 }
  add_attachment(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)
  add_attachment(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)
  add_attachment(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)
  add_attachment(type: 'video', payload: uploaded_info)
end

#bot_tokenObject

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

Whether link previews are disabled for message delivery.

Returns:

  • (Boolean)


191
192
193
# File 'lib/max_bot_api/builders/message_builder.rb', line 191

def disable_link_preview?
  @disable_link_preview
end

#phone_numbersObject

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, reply_message)
  recipient = reply_message[:recipient] || reply_message['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 = reply_message[:body] || reply_message['body'] || {}
  @message[:text] = text
  @message[:link] = { type: 'reply', mid: body[:mid] || body['mid'] }
  self
end

#reset?Boolean

Whether reset mode is enabled.

Returns:

  • (Boolean)


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

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_id)
  @message[:text] = text
  @message[:link] = { type: 'reply', mid: message_id }
  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_hObject

Return the message payload hash.



196
197
198
# File 'lib/max_bot_api/builders/message_builder.rb', line 196

def to_h
  @message
end