Class: InfobipApi::SmsClient

Inherits:
InfobipApiClient show all
Defined in:
lib/infobipapi/client.rb

Instance Method Summary collapse

Methods inherited from InfobipApiClient

#convert_from_json, #execute_GET, #execute_POST, #execute_request, #fill_infobipapi_authentication, #get_or_create_client_correlator, #get_rest_url, #is_success, #login, #prepare_headers, #urlencode

Constructor Details

#initialize(username, password, base_url = nil) ⇒ SmsClient

Returns a new instance of SmsClient.



167
168
169
# File 'lib/infobipapi/client.rb', line 167

def initialize(username, password, base_url=nil)
    super(username, password, base_url)
end

Instance Method Details

#compute_sms_usage(str) ⇒ Object

.codepoints.map { |c| “%02x %02x” % [c / 256,c % 256] }.join “ ”



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
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/infobipapi/client.rb', line 223

def compute_sms_usage(str)
  # single SMS length per SMS (GSM7): 160
  # multiple SMS length per SMS (GSM7): 153
  # single SMS length per SMS (UCS-2): 70
  # multiple SMS length per SMS (UCS-2): 67
  sms_lengths = Hash.new
  # ! has_unicode_char
  sms_lengths[false] = Hash.new
  sms_lengths[false][true] = 153 # need_more_than_one_sms
  sms_lengths[false][false] = 160 # ! need_more_than_one_sms
  # has_unicode_char
  sms_lengths[true] = Hash.new
  sms_lengths[true][true] = 67 # need_more_than_one_sms
  sms_lengths[true][false] = 70 # ! need_more_than_one_sms
  {
      :single_gsm7 => 160,
      :multi_gsm7 => 153,
      :single_ucs2 => 70,
      :multi_ucs2 => 67
    }
    has_unicode_char = false
    need_more_than_one_sms = false
    str.each_char { |c|
      if not Utils.in_gsm7_set?(c) then
        has_unicode_char = true
        break
      end
    }
    if has_unicode_char then
      need_more_than_one_sms = str.length > 70
      format = :unicode
    else
      need_more_than_one_sms = str.length > 160
      format = :gsm7
    end
    return {
      :format => format,
      :length => str.length,
      :length_by_sms => sms_lengths[has_unicode_char][need_more_than_one_sms],
      :number_of_sms => (str.length.to_f / sms_lengths[has_unicode_char][need_more_than_one_sms].to_f).ceil
    }
end

#delivery_reports(params) ⇒ Object

Get delivery reports cf: dev.infobip.com/docs/delivery-reports parm fields names :

  • bulkId: string The ID that uniquely identifies the request. Bulk ID will be received only when you send a message to more than one destination address.

  • messageId: string

The ID that uniquely identifies the message sent.

  • limit: string Number of returned delivery reports. Default value is 50. Max number per request is 10000.



383
384
385
386
387
388
389
390
# File 'lib/infobipapi/client.rb', line 383

def delivery_reports(params)
    req = {} # safety parameters treatment
    req[:bulkId] = params[:bulkId] if params.has_key?:bulkId
    req[:messageId] = params[:messageId] if params.has_key?:messageId
    req[:limit] = params[:limit] if params.has_key?:limit
    is_success, result = execute_GET("/sms/1/reports", req)
    convert_from_json(DeliveryReportList,result, !is_success)
end

#multiple_text_sms(smss) ⇒ Object

send multiple sms message to one or many destination addresses. cf: dev.infobip.com/docs/send-multiple-sms param fields names, array of :

  • from: string Represents sender ID and it can be alphanumeric or numeric. Alphanumeric sender ID length should be between 3 and 11 characters (Example: CompanyName). Numeric sender ID length should be between 3 and 14 characters.

  • to: ‘required` array of strings Array of message destination addresses. If you want to send a message to one destination, a single String is supported instead of an Array. Destination addresses must be in international format (Example: 41793026727).

  • text: string Text of the message that will be sent. (Developper comment: chars must be 7bits or comportment is not predictable on the receiving phones)



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/infobipapi/client.rb', line 204

def multiple_text_sms(smss)
    params = {
      :messages => []
    }
    smss.each { |sms|
      params[:messages].push({
        :from => sms.from,
        :to => sms.to,
        :text => sms.text
      })
    }

    is_success, result = execute_POST( "/sms/1/text/multi", params )

    convert_from_json(SimpleSMSAnswer, result, !is_success)
end

#multiple_utf8_sms(smss) ⇒ Object

send multiple sms message to one or many destination addresses. TEXT in UTF-8 format if all chars are gsm7 encoding compatible then the SMS is sent with gsm7 encoding. Else it will be sent in Unicode binary format. cf: dev.infobip.com/docs/send-multiple-sms param fields names, array of :

  • from: string Represents sender ID and it can be alphanumeric or numeric. Alphanumeric sender ID length should be between 3 and 11 characters (Example: CompanyName). Numeric sender ID length should be between 3 and 14 characters.

  • to: ‘required` array of strings Array of message destination addresses. If you want to send a message to one destination, a single String is supported instead of an Array. Destination addresses must be in international format (Example: 41793026727).

  • text: string utf-8 encoded Text of the message that will be sent. (Developper comment: chars must be 7bits or comportment is not predictable on the receiving phones)

Return an array of 2 results :

one for a /sms/1/text/multi query (nil if none is sent as text)
second one for a /sms/1/binary/multi query (nil if none is sent as binary)


324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/infobipapi/client.rb', line 324

def multiple_utf8_sms(smss)
    params = {
      :text => {
        :uri => "/sms/1/text/multi",
        :messages => []
      },
      :binary => {
        :uri => "/sms/1/binary/multi",
        :messages => []
      }
    }
    smss.each { |sms|
      usage = self.compute_sms_usage(sms.text)
      if usage[:format] == :gsm7 then
        params[:text][:messages].push({
          :from => sms.from,
          :to => sms.to,
          :text => sms.text
        })
      else
        text_array = sms.text.force_encoding('utf-8').codepoints.map { |c| sprintf('%02x', c) }
        params[:binary][:messages].push({
          :from => sms.from,
          :to => sms.to,
          :binary => {
            :hex => text_array.join(' '),
            :dataCoding => 8,
            :esmClass => 0
          }
        })
      end
    }

    results = []
    if params[:text][:messages].length > 0 then
      is_success, result = execute_POST( params[:text][:uri] , params[:text][:messages] )
      results.push(convert_from_json(SimpleSMSAnswer, result, !is_success))
    else
      results.push(nil)
    end

    if params[:binary][:messages].length > 0 then
      is_success, result = execute_POST( params[:binary][:uri] , params[:binary][:messages] )
      results.push(convert_from_json(SimpleSMSAnswer, result, !is_success))
    else
      results.push(nil)
    end
    return results
end

#single_text_sms(sms) ⇒ Object

send single sms message to one or many destination addresses. cf: dev.infobip.com/docs/send-single-sms param fields names:

  • from: string Represents sender ID and it can be alphanumeric or numeric. Alphanumeric sender ID length should be between 3 and 11 characters (Example: CompanyName). Numeric sender ID length should be between 3 and 14 characters.

  • to: ‘required` array of strings Array of message destination addresses. If you want to send a message to one destination, a single String is supported instead of an Array. Destination addresses must be in international format (Example: 41793026727).

  • text: string Text of the message that will be sent. (Developper comment: chars must be 7bits or comportment is not predictable on the receiving phones)



182
183
184
185
186
187
188
189
190
191
# File 'lib/infobipapi/client.rb', line 182

def single_text_sms(sms)
    params = {
      :from => sms.from,
      :to => sms.to,
      :text => sms.text
    }
    is_success, result = execute_POST( "/sms/1/text/single", params )

    convert_from_json(SimpleSMSAnswer, result, !is_success)
end

#single_utf8_sms(sms) ⇒ Object

send single sms message to one or many destination addresses. TEXT in UTF-8 format if all chars are gsm7 encoding compatible then the SMS is sent with gsm7 encoding. Else it will be sent in Unicode binary format. cf: dev.infobip.com/docs/send-single-sms cf: dev.infobip.com/docs/send-single-binary-sms param fields names:

  • from: string Represents sender ID and it can be alphanumeric or numeric. Alphanumeric sender ID length should be between 3 and 11 characters (Example: CompanyName). Numeric sender ID length should be between 3 and 14 characters.

  • to: ‘required` array of strings Array of message destination addresses. If you want to send a message to one destination, a single String is supported instead of an Array. Destination addresses must be in international format (Example: 41793026727).

  • text: string utf-8 encoded Text of the message that will be sent. (Developper comment: chars must be 7bits or comportment is not predictable on the receiving phones)



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
# File 'lib/infobipapi/client.rb', line 281

def single_utf8_sms(sms)
  usage = self.compute_sms_usage(sms.text)
  if usage[:format] == :gsm7 then
    params = {
      :from => sms.from,
      :to => sms.to,
      :text => sms.text
    }
    uri = "/sms/1/text/single"
  else
    text_array = sms.text.force_encoding('utf-8').codepoints.map { |c| sprintf('%02x', c) }
    params = {
      :from => sms.from,
      :to => sms.to,
      :binary => {
        :hex => text_array.join(' '),
        :dataCoding => 8,
        :esmClass => 0
      }
    }
    uri = "/sms/1/binary/single"
  end
  is_success, result = execute_POST(uri , params )
  convert_from_json(SimpleSMSAnswer, result, !is_success)
end