Class: MaxBotApi::Resources::Messages
- Inherits:
-
Object
- Object
- MaxBotApi::Resources::Messages
- Defined in:
- lib/max_bot_api/resources/messages.rb
Overview
Messages API methods.
Instance Method Summary collapse
-
#answer_on_callback(callback_id:, answer:) ⇒ Object
Answer a callback button press.
-
#check(message) ⇒ Object
Check if a message can be sent to the provided phone numbers.
-
#delete_message(message_id:) ⇒ Object
Delete a message by ID.
-
#edit_message(message_id:, message:) ⇒ Object
Edit a message by ID.
-
#get_message(message_id:) ⇒ Object
Fetch a single message by ID.
-
#get_messages(chat_id: nil, message_ids: nil, from: nil, to: nil, count: nil) ⇒ Object
List messages with filters.
-
#initialize(client) ⇒ Messages
constructor
A new instance of Messages.
-
#list_exist(message) ⇒ Object
List phone numbers that exist in MAX.
-
#new_keyboard_builder ⇒ Object
Build a keyboard using the helper.
-
#send(message) ⇒ Object
Send a message builder without returning the created message.
-
#send_with_result(message) ⇒ Object
Send a message builder and return the created message hash.
Constructor Details
#initialize(client) ⇒ Messages
Returns a new instance of Messages.
9 10 11 |
# File 'lib/max_bot_api/resources/messages.rb', line 9 def initialize(client) @client = client end |
Instance Method Details
#answer_on_callback(callback_id:, answer:) ⇒ Object
Answer a callback button press.
59 60 61 |
# File 'lib/max_bot_api/resources/messages.rb', line 59 def answer_on_callback(callback_id:, answer:) @client.request(:post, 'answers', query: { 'callback_id' => callback_id }, body: answer) end |
#check(message) ⇒ Object
Check if a message can be sent to the provided phone numbers.
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/max_bot_api/resources/messages.rb', line 82 def check() = ensure_builder() query = {} query['access_token'] = .bot_token if .reset? query['phone_numbers'] = Array(.phone_numbers).join(',') if .phone_numbers result = @client.request(:get, 'notify/exists', query: query, reset: .reset?) numbers = Array(result[:existing_phone_numbers]) [!numbers.empty?, result] end |
#delete_message(message_id:) ⇒ Object
Delete a message by ID.
52 53 54 |
# File 'lib/max_bot_api/resources/messages.rb', line 52 def (message_id:) @client.request(:delete, 'messages', query: { 'message_id' => }) end |
#edit_message(message_id:, message:) ⇒ Object
Edit a message by ID.
40 41 42 43 44 45 46 47 48 |
# File 'lib/max_bot_api/resources/messages.rb', line 40 def (message_id:, message:) do body = () result = @client.request(:put, 'messages', query: { 'message_id' => }, body: body) return true if result.is_a?(Hash) && result[:success] raise Error, (result[:message] || 'message update failed') end end |
#get_message(message_id:) ⇒ Object
Fetch a single message by ID.
32 33 34 35 |
# File 'lib/max_bot_api/resources/messages.rb', line 32 def (message_id:) path = "messages/#{CGI.escape(.to_s)}" @client.request(:get, path) end |
#get_messages(chat_id: nil, message_ids: nil, from: nil, to: nil, count: nil) ⇒ Object
List messages with filters.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/max_bot_api/resources/messages.rb', line 14 def (chat_id: nil, message_ids: nil, from: nil, to: nil, count: nil) query = {} query['chat_id'] = chat_id if chat_id && chat_id.to_i != 0 query['message_ids'] = Array().join(',') if && !Array().empty? if from && to from_i = from.to_i to_i = to.to_i from, to = to, from if from_i > to_i end query['from'] = from if from && from.to_i != 0 query['to'] = to if to && to.to_i != 0 query['count'] = count if count && count.to_i > 0 @client.request(:get, 'messages', query: normalize_array_query(query)) end |
#list_exist(message) ⇒ Object
List phone numbers that exist in MAX.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/max_bot_api/resources/messages.rb', line 95 def list_exist() = ensure_builder() query = {} query['access_token'] = .bot_token if .reset? query['phone_numbers'] = Array(.phone_numbers).join(',') if .phone_numbers result = @client.request(:get, 'notify/exists', query: query, reset: .reset?) numbers = Array(result[:existing_phone_numbers]) [numbers.empty? ? nil : numbers, result] end |
#new_keyboard_builder ⇒ Object
Build a keyboard using the helper.
64 65 66 |
# File 'lib/max_bot_api/resources/messages.rb', line 64 def new_keyboard_builder Builders::KeyboardBuilder.new end |
#send(message) ⇒ Object
Send a message builder without returning the created message.
70 71 72 |
# File 'lib/max_bot_api/resources/messages.rb', line 70 def send() { (, with_result: false) } end |
#send_with_result(message) ⇒ Object
Send a message builder and return the created message hash.
76 77 78 |
# File 'lib/max_bot_api/resources/messages.rb', line 76 def send_with_result() { (, with_result: true) } end |