Class: SurgeAPI::Resources::Messages

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

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Messages

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Messages.

Parameters:



115
116
117
# File 'lib/surge_api/resources/messages.rb', line 115

def initialize(client:)
  @client = client
end

Instance Method Details

#create(account_id, message_params:, request_options: {}) ⇒ SurgeAPI::Models::Message

Some parameter documentations has been truncated, see Models::MessageCreateParams for more details.

Creates and enqueues a new message to be sent.

Messages are always sent asynchronously. When you hit this endpoint, the message will be created within Surge’s system and enqueued for sending, and then the id for the new message will be returned. When the message is actually sent, a ‘message.sent` webhook event will be triggered and sent to any webhook endpoints that you have subscribed to this event type. Then a `message.delivered` webhook event will be triggered when the carrier sends us a delivery receipt.

By default all messages will be sent immediately. If you would like to schedule sending for some time up to 60 days in the future, you can do that by providing a value for the ‘send_at` field. This should be formatted as an ISO8601 datetime like `2028-10-14T18:06:00Z`.

You must include either a ‘body` or `attachments` field (or both) in the request body. The `body` field should contain the text of the message you want to send, and the `attachments` field should be an array of objects with a `url` field pointing to the file you want to attach. Surge will download these files and send them as attachments in the message.

You can provide either a ‘conversation` object or a `to` field to specify the intended recipient of the message, but an error will be returned if both fields are provided. Similarly the `from` field cannot be used together with the `conversation` field, and `conversation.phone_number` should be specified instead.

Parameters:

  • account_id (String)

    The account from which the message should be sent.

  • message_params (SurgeAPI::MessageParams)

    Payload for creating a message. Either an attachment or the body must be given.

  • request_options (SurgeAPI::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/surge_api/resources/messages.rb', line 46

def create(, params)
  parsed, options = SurgeAPI::MessageCreateParams.dump_request(params)
  case parsed
  in {message_params: Hash => union, **rest}
    parsed = {**rest, **union}
  else
  end
  @client.request(
    method: :post,
    path: ["accounts/%1$s/messages", ],
    body: parsed,
    model: SurgeAPI::Message,
    options: options
  )
end

#list(account_id, after: nil, before: nil, request_options: {}) ⇒ SurgeAPI::Internal::Cursor<SurgeAPI::Models::Message>

Some parameter documentations has been truncated, see Models::MessageListParams for more details.

List all messages for an account with cursor-based pagination.

Parameters:

  • account_id (String)

    The account ID to list messages for.

  • after (String)

    Cursor for forward pagination. Use the next_cursor from a previous response.

  • before (String)

    Cursor for backward pagination. Use the previous_cursor from a previous response

  • request_options (SurgeAPI::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



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

def list(, params = {})
  parsed, options = SurgeAPI::MessageListParams.dump_request(params)
  @client.request(
    method: :get,
    path: ["accounts/%1$s/messages", ],
    query: parsed,
    page: SurgeAPI::Internal::Cursor,
    model: SurgeAPI::Message,
    options: options
  )
end

#retrieve(id, request_options: {}) ⇒ SurgeAPI::Models::Message

Retrieves a Message object.

Parameters:

  • id (String)

    The ID of the message to retrieve.

  • request_options (SurgeAPI::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



73
74
75
76
77
78
79
80
# File 'lib/surge_api/resources/messages.rb', line 73

def retrieve(id, params = {})
  @client.request(
    method: :get,
    path: ["messages/%1$s", id],
    model: SurgeAPI::Message,
    options: params[:request_options]
  )
end