Class: PostProxy::Resources::Messages

Inherits:
Object
  • Object
show all
Defined in:
lib/postproxy/resources/messages.rb

Instance Method Summary collapse

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, idempotency_key: nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/postproxy/resources/messages.rb', line 88

def edit(message_id, body: nil, reply_markup: nil, profile_group_id: nil, idempotency_key: nil)
  json_body = {}
  json_body[:body] = body if body
  json_body[:reply_markup] = reply_markup if reply_markup

  result = @client.request(:patch, "/messages/#{message_id}",
    json: json_body,
    profile_group_id: profile_group_id,
    idempotency_key: idempotency_key
  )
  Message.new(**result)
end

#get(message_id, profile_group_id: nil) ⇒ Object



83
84
85
86
# File 'lib/postproxy/resources/messages.rb', line 83

def get(message_id, profile_group_id: nil)
  result = @client.request(:get, "/messages/#{message_id}", 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)
  messages = (result[:data] || []).map { |m| Message.new(**m) }
  PaginatedResponse.new(
    data: messages,
    total: result[:total],
    page: result[:page],
    per_page: result[:per_page]
  )
end

#react(message_id, reaction: nil, emoji: nil, profile_group_id: nil, idempotency_key: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/postproxy/resources/messages.rb', line 101

def react(message_id, reaction: nil, emoji: nil, profile_group_id: nil, idempotency_key: nil)
  json_body = {}
  json_body[:reaction] = reaction if reaction
  json_body[:emoji] = emoji if emoji

  result = @client.request(:post, "/messages/#{message_id}/react",
    json: json_body.empty? ? nil : json_body,
    profile_group_id: profile_group_id,
    idempotency_key: idempotency_key
  )
  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, quick_replies: nil, buttons: nil, card: nil, profile_group_id: nil, idempotency_key: nil) ⇒ Object Also known as: send

quick_replies, buttons, and card are Facebook and Instagram only — they return 422 on Telegram and Bluesky, where reply_markup is the equivalent. They are sent on the JSON path only, so pass media as hosted URLs rather than media_files when combining with an attachment.

Each accepts model instances or plain hashes.



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
# File 'lib/postproxy/resources/messages.rb', line 31

def send_message(chat_id, body: nil, media: nil, media_files: nil, tag: nil,
                 reply_to_external_id: nil, reply_markup: nil,
                 quick_replies: nil, buttons: nil, card: nil,
                 profile_group_id: nil, idempotency_key: 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,
      idempotency_key: idempotency_key
    )
  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
    json_body[:quick_replies] = quick_replies.map { |q| serialize_interactive(q) } if quick_replies
    json_body[:buttons] = buttons.map { |b| serialize_interactive(b) } if buttons
    json_body[:card] = serialize_interactive(card) if card

    result = @client.request(:post, "/chats/#{chat_id}/messages",
      json: json_body,
      profile_group_id: profile_group_id,
      idempotency_key: idempotency_key
    )
  end

  Message.new(**result)
end

#unreact(message_id, profile_group_id: nil, idempotency_key: nil) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/postproxy/resources/messages.rb', line 114

def unreact(message_id, profile_group_id: nil, idempotency_key: nil)
  result = @client.request(:delete, "/messages/#{message_id}/unreact",
    profile_group_id: profile_group_id,
    idempotency_key: idempotency_key
  )
  Message.new(**result)
end