Class: Blueticks::Resources::ScheduledMessagesResource

Inherits:
BaseResource
  • Object
show all
Defined in:
lib/blueticks/resources/scheduled_messages.rb

Instance Attribute Summary

Attributes inherited from BaseResource

#client

Instance Method Summary collapse

Methods inherited from BaseResource

#initialize

Constructor Details

This class inherits a constructor from Blueticks::BaseResource

Instance Method Details

#create(chat_id, type:, text: nil, media_url: nil, media_base64: nil, media_kind: nil, media_filename: nil, poll_question: nil, poll_options: nil, poll_allow_multiple: nil, reply_to: nil, secret: nil, send_at: nil, idempotency_key: nil) ⇒ Object

Send / schedule a message to a chat.

Queue a message for delivery to chat_id (a WhatsApp JID or phone). The body is a discriminated union keyed on type (text, media, poll). Omit send_at to dispatch as soon as possible, or pass an ISO-8601 timestamp to schedule it.

  • type: "text" — requires text.
  • type: "media" — requires media_url or media_base64; optional media_kind, media_filename.
  • type: "poll" — requires poll_question and poll_options; optional poll_allow_multiple.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/blueticks/resources/scheduled_messages.rb', line 23

def create(chat_id, type:, text: nil, media_url: nil, media_base64: nil,
           media_kind: nil, media_filename: nil, poll_question: nil,
           poll_options: nil, poll_allow_multiple: nil, reply_to: nil,
           secret: nil, send_at: nil, idempotency_key: nil)
  body = { "type" => type }
  body["text"] = text unless text.nil?
  body["mediaUrl"] = media_url unless media_url.nil?
  body["mediaBase64"] = media_base64 unless media_base64.nil?
  body["mediaKind"] = media_kind unless media_kind.nil?
  body["mediaFilename"] = media_filename unless media_filename.nil?
  body["pollQuestion"] = poll_question unless poll_question.nil?
  body["pollOptions"] = poll_options unless poll_options.nil?
  body["pollAllowMultiple"] = poll_allow_multiple unless poll_allow_multiple.nil?
  body["replyTo"] = reply_to unless reply_to.nil?
  body["secret"] = secret unless secret.nil?
  body["sendAt"] = send_at unless send_at.nil?
  env = client.request("POST", "/v1/scheduled-messages/#{chat_id}", body: body,
                                                                    idempotency_key: idempotency_key)
  Types::ScheduledMessage.from_hash(env && env["data"])
end

#delete(message_id) ⇒ Object

Delete (cancel) a queued/scheduled message by id.



89
90
91
92
# File 'lib/blueticks/resources/scheduled_messages.rb', line 89

def delete(message_id)
  env = client.request("DELETE", "/v1/scheduled-messages/#{message_id}")
  Types::DeletedResource.from_hash(env && env["data"])
end

#list(chat_id: nil, search_token: nil, status: nil, order: nil, skip: nil, limit: nil) ⇒ Object

List messages in the user-messages queue (all sources), newest first. Offset-paginated. Optionally filter by chat_id and/or lifecycle status.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/blueticks/resources/scheduled_messages.rb', line 53

def list(chat_id: nil, search_token: nil, status: nil, order: nil, skip: nil, limit: nil)
  params = {}
  params["chatId"] = chat_id unless chat_id.nil?
  params["searchToken"] = search_token unless search_token.nil?
  params["status"] = status unless status.nil?
  params["order"] = order unless order.nil?
  params["skip"] = skip unless skip.nil?
  params["limit"] = limit unless limit.nil?
  env = client.request("GET", "/v1/scheduled-messages", params: params.empty? ? nil : params)
  Types::PaginatedPage.from_hash(env, item_type: Types::ScheduledMessage)
end

#retrieve(message_id) ⇒ Object

Get the current status of a queued/scheduled message by id.



45
46
47
48
# File 'lib/blueticks/resources/scheduled_messages.rb', line 45

def retrieve(message_id)
  env = client.request("GET", "/v1/scheduled-messages/#{message_id}")
  Types::ScheduledMessage.from_hash(env && env["data"])
end

#update(message_id, type: nil, text: nil, media_url: nil, media_base64: nil, media_kind: nil, media_filename: nil, poll_question: nil, poll_options: nil, poll_allow_multiple: nil, reply_to: nil, secret: nil, send_at: nil) ⇒ Object

Edit a previously-queued message that has not dispatched yet. Pass any subset of the editable fields.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/blueticks/resources/scheduled_messages.rb', line 67

def update(message_id, type: nil, text: nil, media_url: nil, media_base64: nil,
           media_kind: nil, media_filename: nil, poll_question: nil,
           poll_options: nil, poll_allow_multiple: nil, reply_to: nil,
           secret: nil, send_at: nil)
  body = {}
  body["type"] = type unless type.nil?
  body["text"] = text unless text.nil?
  body["mediaUrl"] = media_url unless media_url.nil?
  body["mediaBase64"] = media_base64 unless media_base64.nil?
  body["mediaKind"] = media_kind unless media_kind.nil?
  body["mediaFilename"] = media_filename unless media_filename.nil?
  body["pollQuestion"] = poll_question unless poll_question.nil?
  body["pollOptions"] = poll_options unless poll_options.nil?
  body["pollAllowMultiple"] = poll_allow_multiple unless poll_allow_multiple.nil?
  body["replyTo"] = reply_to unless reply_to.nil?
  body["secret"] = secret unless secret.nil?
  body["sendAt"] = send_at unless send_at.nil?
  env = client.request("PATCH", "/v1/scheduled-messages/#{message_id}", body: body)
  Types::ScheduledMessage.from_hash(env && env["data"])
end