Class: PostProxy::Resources::Messages
- Inherits:
-
Object
- Object
- PostProxy::Resources::Messages
- Defined in:
- lib/postproxy/resources/messages.rb
Instance Method Summary collapse
- #edit(message_id, body: nil, reply_markup: nil, profile_group_id: nil) ⇒ Object
- #get(message_id, profile_group_id: nil) ⇒ Object
-
#initialize(client) ⇒ Messages
constructor
A new instance of Messages.
- #list(chat_id, page: nil, per_page: nil, direction: nil, status: nil, profile_group_id: nil) ⇒ Object
- #react(message_id, reaction: nil, emoji: nil, profile_group_id: nil) ⇒ Object
- #send_message(chat_id, body: nil, media: nil, media_files: nil, tag: nil, reply_to_external_id: nil, reply_markup: nil, profile_group_id: nil) ⇒ Object (also: #send)
- #unreact(message_id, profile_group_id: nil) ⇒ Object
Constructor Details
#initialize(client) ⇒ Messages
Returns a new instance of Messages.
4 5 6 |
# File 'lib/postproxy/resources/messages.rb', line 4 def initialize(client) @client = client end |
Instance Method Details
#edit(message_id, body: nil, reply_markup: nil, profile_group_id: nil) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/postproxy/resources/messages.rb', line 72 def edit(, body: nil, reply_markup: nil, profile_group_id: nil) json_body = {} json_body[:body] = body if body json_body[:reply_markup] = reply_markup if reply_markup result = @client.request(:patch, "/messages/#{}", json: json_body, profile_group_id: profile_group_id) Message.new(**result) end |
#get(message_id, profile_group_id: nil) ⇒ Object
67 68 69 70 |
# File 'lib/postproxy/resources/messages.rb', line 67 def get(, profile_group_id: nil) result = @client.request(:get, "/messages/#{}", profile_group_id: profile_group_id) Message.new(**result) end |
#list(chat_id, page: nil, per_page: nil, direction: nil, status: nil, profile_group_id: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/postproxy/resources/messages.rb', line 8 def list(chat_id, page: nil, per_page: nil, direction: nil, status: nil, profile_group_id: nil) params = {} params[:page] = page if page params[:per_page] = per_page if per_page params[:direction] = direction if direction params[:status] = status if status result = @client.request(:get, "/chats/#{chat_id}/messages", params: params, profile_group_id: profile_group_id) = (result[:data] || []).map { |m| Message.new(**m) } PaginatedResponse.new( data: , total: result[:total], page: result[:page], per_page: result[:per_page] ) end |
#react(message_id, reaction: nil, emoji: nil, profile_group_id: nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/postproxy/resources/messages.rb', line 81 def react(, reaction: nil, emoji: nil, profile_group_id: nil) json_body = {} json_body[:reaction] = reaction if reaction json_body[:emoji] = emoji if emoji result = @client.request(:post, "/messages/#{}/react", json: json_body.empty? ? nil : json_body, profile_group_id: profile_group_id ) Message.new(**result) end |
#send_message(chat_id, body: nil, media: nil, media_files: nil, tag: nil, reply_to_external_id: nil, reply_markup: nil, profile_group_id: nil) ⇒ Object Also known as: send
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 |
# File 'lib/postproxy/resources/messages.rb', line 25 def (chat_id, body: nil, media: nil, media_files: nil, tag: nil, reply_to_external_id: nil, reply_markup: nil, profile_group_id: nil) has_files = media_files && !media_files.empty? if has_files form_data = {} form_data["body"] = body if body form_data["tag"] = tag if tag form_data["reply_to_external_id"] = reply_to_external_id if reply_to_external_id files = [] media&.each do |m| files << ["media[]", nil, m, "text/plain"] end media_files.each do |path| path = path.to_s filename = File.basename(path) content_type = mime_type_for(filename) io = File.open(path, "rb") files << ["media[]", filename, io, content_type] end result = @client.request(:post, "/chats/#{chat_id}/messages", data: form_data, files: files, profile_group_id: profile_group_id ) else json_body = {} json_body[:body] = body if body json_body[:media] = media if media json_body[:tag] = tag if tag json_body[:reply_to_external_id] = reply_to_external_id if reply_to_external_id json_body[:reply_markup] = reply_markup if reply_markup result = @client.request(:post, "/chats/#{chat_id}/messages", json: json_body, profile_group_id: profile_group_id) end Message.new(**result) end |