Class: VoiceML::MessagesResource

Inherits:
BaseResource show all
Defined in:
lib/voiceml/resources/messages.rb

Overview

Operations on ‘/Messages` — VoiceTel’s Twilio-compatible SMS surface, backed by the SDK 2.2 gateway. Outbound-only today (no MMS, no inbound webhook delivery).

All methods accept idiomatic snake_case keyword arguments — they’re translated to the PascalCase wire names internally.

Constant Summary collapse

CREATE_FIELDS =
{
  'To'                   => :to,
  'Body'                 => :body,
  'From'                 => :from,
  'MessagingServiceSid'  => :messaging_service_sid,
  'StatusCallback'       => :status_callback
}.freeze
LIST_FIELDS =
{
  'To'         => :to,
  'From'       => :from,
  'DateSent'   => :date_sent,
  'DateSent<'  => :date_sent_lt,
  'DateSent>'  => :date_sent_gt,
  'Page'       => :page,
  'PageSize'   => :page_size,
  'PageToken'  => :page_token
}.freeze
UPDATE_FIELDS =
{
  'Body'   => :body,
  'Status' => :status
}.freeze

Instance Method Summary collapse

Methods inherited from BaseResource

#initialize

Constructor Details

This class inherits a constructor from VoiceML::BaseResource

Instance Method Details

#create(**kwargs) ⇒ VoiceML::Message

Dispatch an outbound SMS. ‘to:` and `body:` are required; `from:` falls back to the tenant’s configured default sender when omitted.

Returns:



42
43
44
45
# File 'lib/voiceml/resources/messages.rb', line 42

def create(**kwargs)
  data = @transport.request(:post, path('Messages'), form: form_params(CREATE_FIELDS, kwargs))
  Message.from_hash(data)
end

#delete(sid) ⇒ nil

Remove a Message resource from the account’s store.

Returns:

  • (nil)


96
97
98
99
# File 'lib/voiceml/resources/messages.rb', line 96

def delete(sid)
  @transport.request(:delete, path('Messages', sid))
  nil
end

#each(**kwargs) {|VoiceML::Message| ... } ⇒ Enumerator<VoiceML::Message>

Walk every page of /Messages and yield each Message. Returns an Enumerator when called without a block.

Yields:

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/voiceml/resources/messages.rb', line 69

def each(**kwargs, &block)
  return enum_for(:each, **kwargs) unless block

  page_num = kwargs.delete(:page) || 0
  loop do
    chunk = list(**kwargs, page: page_num)
    chunk.messages.each(&block)
    break if chunk.next_page_uri.nil? || chunk.next_page_uri.empty? || chunk.messages.empty?

    page_num += 1
  end
end

#fetch(sid) ⇒ VoiceML::Message

Retrieve a previously-sent Message by sid.

Returns:



50
51
52
# File 'lib/voiceml/resources/messages.rb', line 50

def fetch(sid)
  Message.from_hash(@transport.request(:get, path('Messages', sid)))
end

#list(**kwargs) ⇒ VoiceML::MessageList

Return a single page of Messages, narrowed by the Twilio-documented filter set (To, From, DateSent eq/gt/lt) plus pagination.



58
59
60
61
62
# File 'lib/voiceml/resources/messages.rb', line 58

def list(**kwargs)
  MessageList.from_hash(
    @transport.request(:get, path('Messages'), params: form_params(LIST_FIELDS, kwargs))
  )
end

#update(sid, **kwargs) ⇒ VoiceML::Message

Mutate an existing Message — redact ‘body:` to empty string, or attempt `status: “canceled”`. Cancellation returns 21610 today because the gateway is fire-and-forget.

Returns:



87
88
89
90
91
# File 'lib/voiceml/resources/messages.rb', line 87

def update(sid, **kwargs)
  data = @transport.request(:post, path('Messages', sid),
                            form: form_params(UPDATE_FIELDS, kwargs))
  Message.from_hash(data)
end