Class: Plivo::Resources::MessagesInterface

Inherits:
Base::ResourceInterface show all
Defined in:
lib/plivo/resources/messages.rb

Constant Summary

Constants included from Utils

Utils::TYPE_WHITELIST

Instance Method Summary collapse

Methods included from Utils

GetSortedQueryParamString?, compute_signatureV3?, expected_type?, expected_value?, generate_url?, getMapFromQueryString?, is_one_among_string_url?, multi_valid_param?, raise_invalid_request, valid_account?, valid_date_format?, valid_mainaccount?, valid_multiple_destination_integers?, valid_multiple_destination_nos?, valid_param?, valid_range?, valid_signature?, valid_signatureV3?, valid_subaccount?, valid_url?

Constructor Details

#initialize(client, resource_list_json = nil) ⇒ MessagesInterface

Returns a new instance of MessagesInterface.



45
46
47
48
49
50
# File 'lib/plivo/resources/messages.rb', line 45

def initialize(client, resource_list_json = nil)
  @_name = 'Message'
  @_resource_type = Message
  @_identifier_string = 'message_uuid'
  super
end

Instance Method Details

#create(src = nil, dst = nil, text = nil, options = nil, powerpack_uuid = nil) ⇒ Object

Parameters:

  • src (String) (defaults to: nil)
  • dst (Array) (defaults to: nil)
  • text (String) (defaults to: nil)
  • options (Hash) (defaults to: nil)
  • options[Int]: (Hash)

    a customizable set of options

  • options[List]: (Hash)

    a customizable set of options

Options Hash (options):

  • :type (String)

    The type of message. Should be ‘sms` or `mms`. Defaults to `sms`.

  • :url (String)

    The URL to which with the status of the message is sent. The following parameters are sent to the URL:

    • To - Phone number of the recipient

    • From - Phone number of the sender

    • Status - Status of the message including “queued”, “sent”, “failed”, “delivered”, “undelivered” or “rejected”

    • MessageUUID - The unique ID for the message

    • ParentMessageUUID - ID of the parent message (see notes about long SMS below)

    • PartInfo - Specifies the sequence of the message (useful for long messages split into multiple text messages; see notes about long SMS below)

    • TotalRate - Total rate per sms

    • TotalAmount - Total cost of sending the sms (TotalRate * Units)

    • Units - Number of units into which a long SMS was split

    • MCC - Mobile Country Code (see here https://en.wikipedia.org/wiki/Mobile_country_code for more details)

    • MNC - Mobile Network Code (see here https://en.wikipedia.org/wiki/Mobile_country_code for more details)

    • ErrorCode - Delivery Response code returned by the carrier attempting the delivery. See Supported error codes https://www.plivo.com/docs/api/message/#standard-plivo-error-codes.

  • :method (String)

    The method used to call the url. Defaults to POST.

  • :log (String)

    If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true.

  • :trackable (String)

    set to false



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/plivo/resources/messages.rb', line 82

def create(src = nil, dst = nil, text = nil, options = nil, powerpack_uuid = nil)
  #All params in One HASH
  value = src
  if(value.is_a?(Hash))
    valid_param?(:src, value[:src], [Integer, String, Symbol], false)
    valid_param?(:text, value[:text], [String, Symbol], true)
    valid_param?(:dst, value[:dst], [String, Array], true)
    valid_param?(:powerpack_uuid, value[:powerpack_uuid], [String, Symbol], false)

    if (value[:dst] == value[:src])
      raise InvalidRequestError, 'src and dst cannot be same'
    end

    if value.key?(:value).nil? && value.key(:powerpack_uuid).nil?
      raise InvalidRequestError, 'value and powerpack uuid both cannot be nil'
    end

    if !value.key?(:value).nil? && !value.key(:powerpack_uuid).nil?
      raise InvalidRequestError, 'value and powerpack uuid both cannot be present'
    end

    if !value.key?(:dst).nil? && !value.key(:powerpack_uuid).nil?
      raise InvalidRequestError, 'dst is a required parameter'
    end

    params = {
      src: value[:src],
      text: value[:text],
      powerpack_uuid: value[:powerpack_uuid]
    }
    if (value[:dst].is_a?(Array))
      value[:dst].each do |dst_num|
         valid_param?(:dst_num, dst_num, [Integer, String, Symbol], true)
         params[:dst] = value[:dst].join('<')
      end
    else
      params[:dst] = value[:dst]
    end

    #Handling optional params in One HASH
    if value.key?(:type) && valid_param?(:type, value[:type],String, true, %w[sms mms])
      params[:type] = value[:type]
    end

    if value.key?(:url) && valid_param?(:url, value[:url], String, true)
       params[:url] = value[:url]
       if value.key?(:method) &&
        valid_param?(:method, value[:method], String, true, %w[POST GET])
        params[:method] = value[:method]
       else
         params[:method] = 'POST'
       end
    end         
    
    if value.key?(:log) &&
      valid_param?(:log, value[:log], [TrueClass, FalseClass], true)
        params[:log] = value[:log]
    end

    if value.key?(:message_expiry) &&
      valid_param?(:message_expiry, value[:message_expiry], [Integer, Integer], true)
        params[:message_expiry] = value[:message_expiry]
    end         

    if value.key?(:trackable) &&
        valid_param?(:trackable, value[:trackable], [TrueClass, FalseClass], true)
        params[:trackable] = value[:trackable]
    end

    if value.key?(:media_urls) &&
      valid_param?(:media_urls, value[:media_urls], Array, true)
     params[:media_urls] = value[:media_urls]
    end

    if value.key?(:media_ids) &&
      valid_param?(:media_ids, value[:media_ids], Array, true)
     params[:media_ids] = value[:media_ids]
    end

  #legacy code compatibility
  else
    valid_param?(:src, src, [Integer, String, Symbol], false)
    valid_param?(:text, text, [String, Symbol], true)
    valid_param?(:dst, dst, [String, Array], true)
    valid_param?(:powerpack_uuid, powerpack_uuid, [String, Symbol], false)
    dst.each do |dst_num|
      valid_param?(:dst_num, dst_num, [Integer, String, Symbol], true)
    end
  
    if dst.include? src
      raise InvalidRequestError, 'src and dst cannot be same'
    end

    if src.nil? && powerpack_uuid.nil?
      raise InvalidRequestError, 'src and powerpack uuid both cannot be nil'
    end

    if !src.nil? && !powerpack_uuid.nil?
      raise InvalidRequestError, 'src and powerpack uuid both cannot be present'
    end

    params = {
      src: src,
      text: text,
      powerpack_uuid: powerpack_uuid
    }

    if (dst.is_a?(Array))
      dst.each do |dst_num|
        valid_param?(:dst_num, dst_num, [Integer, String, Symbol], true)
        params[:dst] = dst.join('<')
      end
    else
      params[:dst] = dst
    end

    return perform_create(params) if options.nil?
    valid_param?(:options, options, Hash, true)

    if options.key?(:type) &&
       valid_param?(:type, options[:type], String, true, %w[sms mms])
      params[:type] = options[:type]
    end

    if options.key?(:url) && valid_param?(:url, options[:url], String, true)
      params[:url] = options[:url]
      if options.key?(:method) &&
         valid_param?(:method, options[:method], String, true, %w[POST GET])
        params[:method] = options[:method]
      else
        params[:method] = 'POST'
      end
    end

    if options.key?(:media_urls) &&
      valid_param?(:media_urls, options[:media_urls], Array, true)
     params[:media_urls] = options[:media_urls]
    end
    
    if options.key?(:media_ids) &&
      valid_param?(:media_ids, options[:media_ids], Array, true)
     params[:media_ids] = options[:media_ids]
    end

    if options.key?(:log) &&
       valid_param?(:log, options[:log], [TrueClass, FalseClass], true)
      params[:log] = options[:log]
    end

    if options.key?(:media_urls) &&
      valid_param?(:media_urls, options[:media_urls], Array, true)
     params[:media_urls] = options[:media_urls]
    end
  
    if options.key?(:media_ids) &&
      valid_param?(:media_ids, options[:media_ids], Array, true)
     params[:media_ids] = options[:media_ids]
    end

    if options.key?(:message_expiry) &&
      valid_param?(:message_expiry, options[:message_expiry], [Integer, Integer], true)
        params[:message_expiry] = options[:message_expiry]
    end

    if options.key?(:trackable) &&
      valid_param?(:trackable, options[:trackable], [TrueClass, FalseClass], true)
     params[:trackable] = options[:trackable]
    end
  end
  perform_create(params)
end

#eachObject



321
322
323
324
325
326
327
328
329
# File 'lib/plivo/resources/messages.rb', line 321

def each
  offset = 0
  loop do
    message_list = list(offset: offset)
    message_list[:objects].each { |message| yield message }
    offset += 20
    return unless message_list.length == 20
  end
end

#get(message_uuid) ⇒ Object

Parameters:

  • message_uuid (String)


53
54
55
# File 'lib/plivo/resources/messages.rb', line 53

def get(message_uuid)
  perform_get(message_uuid)
end

#list(options = nil) ⇒ Object

Parameters:

  • options (Hash) (defaults to: nil)
  • options[List]: (Hash)

    a customizable set of options

Options Hash (options):

  • :subaccount (String)

    The id of the subaccount, if message details of the subaccount is needed.

  • :message_direction (String)

    Filter the results by message direction. The valid inputs are inbound and outbound.

  • :message_time (String)

    Filter out messages according to the time of completion. The filter can be used in the following five forms:

    • message_time: The format expected is YYYY-MM-DD HH:MM[:ss]. Eg:- To get all messages that were sent/received at 2012-03-21 11:47, use message_time=2012-03-21 11:47

    • message_time__gt: gt stands for greater than. The format expected is YYYY-MM-DD HH:MM[:ss]. Eg:- To get all messages that were sent/received after 2012-03-21 11:47, use message_time__gt=2012-03-21 11:47

    • message_time__gte: gte stands for greater than or equal. The format expected is YYYY-MM-DD HH:MM[:ss]. Eg:- To get all messages that were sent/received after or exactly at 2012-03-21 11:47, use message_time__gte=2012-03-21 11:47

    • message_time__lt: lt stands for lesser than. The format expected is YYYY-MM-DD HH:MM[:ss]. Eg:- To get all messages that were sent/received before 2012-03-21 11:47, use message_time__lt=2012-03-21 11:47

    • message_time__lte: lte stands for lesser than or equal. The format expected is YYYY-MM-DD HH:MM[:ss]. Eg:- To get all messages that were sent/received before or exactly at 2012-03-21 11:47, use message_time__lte=2012-03-21 11:47

    • Note: The above filters can be combined to get messages that were sent/received in a particular time range. The timestamps need to be UTC timestamps.

  • :message_state (String)

    Status value of the message, is one of “queued”, “sent”, “failed”, “delivered”, “undelivered” or “rejected”

  • :limit (Int)

    Used to display the number of results per page. The maximum number of results that can be fetched is 20.

  • :offset (Int)

    Denotes the number of value items by which the results should be offset. Eg:- If the result contains a 1000 values and limit is set to 10 and offset is set to 705, then values 706 through 715 are displayed in the results. This parameter is also used for pagination of the results.

  • :error_code (String)

    Delivery Response code returned by the carrier attempting the delivery. See Supported error codes https://www.plivo.com/docs/api/message/#standard-plivo-error-codes.

  • :powerpack_id (String)

    Filter the results by powerpack id

  • : (string)

    tendlc_campaign_id - exact tendlc campaign id search

  • :destination_country_iso2 (string)
    • valid 2 character country_iso2

  • : (string)

    tendlc_registration_status - registered or unregistered enum allowed



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/plivo/resources/messages.rb', line 274

def list(options = nil)
  return perform_list if options.nil?
  valid_param?(:options, options, Hash, true)

  params = {}
  params_expected = %i[
    subaccount message_time message_time__gt message_time__gte
    message_time__lt message_time__lte error_code powerpack_id tendlc_campaign_id tendlc_registration_status destination_country_iso2
  ]
  params_expected.each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [String, Symbol], true)
      params[param] = options[param]
    end
  end

  %i[offset limit].each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [Integer, Integer], true)
      params[param] = options[param]
    end
  end

  if options.key?(:message_direction) &&
     valid_param?(:message_direction, options[:message_direction],
                  [String, Symbol], true, %w[inbound outbound])
    params[:message_direction] = options[:message_direction]
  end

  if options.key?(:message_state) &&
     valid_param?(:message_state, options[:message_state],
                  [String, Symbol], true, %w[queued sent failed delivered
                                             undelivered rejected])
    params[:message_state] = options[:message_state]
  end

  if options.key?(:limit) &&
     (options[:limit] > 20 || options[:limit] <= 0)
    raise_invalid_request('The maximum number of results that can be '\
    "fetched is 20. limit can't be more than 20 or less than 1")
  end

  raise_invalid_request("Offset can't be negative") if options.key?(:offset) && options[:offset] < 0

  perform_list(params)
end